Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module 'posixpath' has no attribute 'sep' #1697

Closed
tilman19 opened this issue Jul 28, 2022 · 3 comments · Fixed by #1698
Closed

AttributeError: module 'posixpath' has no attribute 'sep' #1697

tilman19 opened this issue Jul 28, 2022 · 3 comments · Fixed by #1698
Milestone

Comments

@tilman19
Copy link
Contributor

On one of my systems the loader command fails with "AttributeError: module 'posixpath' has no attribute 'sep'.
It can actually run the code but not use the debugger within PyCharm 2022.1.3. This will trigger the exception.

The full stack trace looks like the following:

Traceback (most recent call last):
  File "/mycode/venv/lib/python3.10/site-packages/typer/main.py", line 328, in __call__
    raise e
  File "/mycode/venv/lib/python3.10/site-packages/typer/main.py", line 311, in __call__
    return get_command(self)(*args, **kwargs)
  File "/mycode/venv/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/mycode/venv/lib/python3.10/site-packages/typer/core.py", line 778, in main
    return _main(
  File "/mycode/venv/lib/python3.10/site-packages/typer/core.py", line 216, in _main
    rv = self.invoke(ctx)
  File "/mycode/venv/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/mycode/venv/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/mycode/venv/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/mycode/venv/lib/python3.10/site-packages/typer/main.py", line 683, in wrapper
    return callback(**use_params)  # type: ignore
  File "/mycode/src/myproject/cli/commands/build.py", line 106, in build
    _create_all_docker_files(project_config, build_mode=mode, services=services)
  File "/mycode/src/myproject/cli/commands/build.py", line 118, in _create_all_docker_files
    _create_docker_file(service, project_config, build_mode)
  File "/mycode/src/myproject/cli/commands/build.py", line 135, in _create_docker_file
    dockerfile_template = get_jinja_env().get_template('Dockerfile.jinja')
  File "/mycode/venv/lib/python3.10/site-packages/jinja2/environment.py", line 1010, in get_template
    return self._load_template(name, globals)
  File "/mycode/venv/lib/python3.10/site-packages/jinja2/environment.py", line 969, in _load_template
    template = self.loader.load(self, name, self.make_globals(globals))
  File "/mycode/venv/lib/python3.10/site-packages/jinja2/loaders.py", line 126, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/mycode/venv/lib/python3.10/site-packages/jinja2/loaders.py", line 195, in get_source
    pieces = split_template_path(template)
  File "/mycode/venv/lib/python3.10/site-packages/jinja2/loaders.py", line 32, in split_template_path
    os.path.sep in piece
AttributeError: module 'posixpath' has no attribute 'sep'

Expected result: No error when running the code in the debugger just as when running without debugger.

It seems os.path.sep is the problem here. According to the official Python documentation os.sep is the primary source (https://docs.python.org/3/library/os.html#os.sep). Chaning from os.path.sep to os.sep fixes this issue.

Environment used:

  • Python 3.10.5
  • Jinja2 3.1.2
  • PyCharm 2022.1 on Arch Linux
@davidism
Copy link
Member

davidism commented Jul 28, 2022

I can't reproduce this issue with the information provided. Be sure to provide a minimal reproducible example when reporting an issue.

$ python3.10 --version
3.10.5
$ python
>>> import os.path
>>> os.path.sep
'/'

I also stepped through the following with the PyCharm 2022.1.3 debugger on Linux and did not get the reported error.

from jinja2.loaders import FileSystemLoader
loader = FileSystemLoader(".")
# the following code accesses os.path.sep
print(loader.list_templates())

@davidism davidism changed the title Bug in loaders.py: AttributeError: module 'posixpath' has no attribute 'sep' AttributeError: module 'posixpath' has no attribute 'sep' Jul 28, 2022
@kevin-brown
Copy link
Member

Highly suspect this ticket is related. It suggests this is system-specific and likely tied to newer Python versions.

stub42/pytz#72

I saw the related PR which supposedly fixes the issue.

@tilman19
Copy link
Contributor Author

Reproducing this bug is indeed tricky. It occurs on one system and not on another very similar system. But it also seems this does not only happen here with jinja, as @kevin-brown mentioned.
PyTest has had the same bug as well: pytest-dev/pytest#9791

Using os.sep instead of os.path.sep is closer to the Python documentation as well, so I don’t see a showstopper here. But it’s also not upon me to decide if the merge request is fine.

@davidism davidism added this to the 3.2.0 milestone Aug 2, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants