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

Relative import still doesn't work? #26

Closed
nelsyeung opened this issue Jun 14, 2021 · 4 comments
Closed

Relative import still doesn't work? #26

nelsyeung opened this issue Jun 14, 2021 · 4 comments

Comments

@nelsyeung
Copy link

Reproduction

mkdir -p foo/src
cd foo
touch src/__init__.py

Create the following files:
src/server.py:

from .foo import getab

src/foo.py:

def getab():
    return ''

Run:

stickytape src/server.py --add-python-path . --output-file tmp.py
Traceback (most recent call last):
  File "/Users/nelson.yeung/Documents/python-playground/.venv/bin/stickytape", line 8, in <module>
    sys.exit(main())
  File "/Users/nelson.yeung/Documents/python-playground/.venv/lib/python3.9/site-packages/stickytape/main.py", line 9, in main
    output = stickytape.script(
  File "/Users/nelson.yeung/Documents/python-playground/.venv/lib/python3.9/site-packages/stickytape/__init__.py", line 27, in script
    output.append(_generate_module_writers(
  File "/Users/nelson.yeung/Documents/python-playground/.venv/lib/python3.9/site-packages/stickytape/__init__.py", line 69, in _generate_module_writers
    generator.generate_for_file(path, add_python_modules=add_python_modules)
  File "/Users/nelson.yeung/Documents/python-playground/.venv/lib/python3.9/site-packages/stickytape/__init__.py", line 87, in generate_for_file
    self._generate_for_module(ImportTarget(python_file_path, relative_path=None, is_package=False, module_name=None))
  File "/Users/nelson.yeung/Documents/python-playground/.venv/lib/python3.9/site-packages/stickytape/__init__.py", line 95, in _generate_for_module
    for import_line in import_lines:
  File "/Users/nelson.yeung/Documents/python-playground/.venv/lib/python3.9/site-packages/stickytape/__init__.py", line 173, in _find_imports_in_module
    package_name = ".".join(python_module.module_name.split(".")[:-level])
AttributeError: 'NoneType' object has no attribute 'split'

Info

python --version
Python 3.9.5

OS: macOS 11.4
stickytape: 0.2.1

Related: #22 #24

@mwilliamson
Copy link
Owner

I don't think this works as an ordinary Python script:

$ python src/server.py 
Traceback (most recent call last):
  File "src/server.py", line 1, in <module>
    from .foo import getab
ImportError: attempted relative import with no known parent package

@nelsyeung
Copy link
Author

Ah I see, if I just add an ordinary executable script that imports src.server, it'd work. However, is it not possible to make my example work?

@mwilliamson
Copy link
Owner

As in make stickytape make the example work, even though the example doesn't work when running it as an ordinary Python script? It might be possible, but I think that stickytape should stick to normal Python behaviour where possible.

@nelsyeung
Copy link
Author

Sorry, I mean make stickytape combine scripts that can be imported instead of being executed, but I guess that's beyond the scope of this tool. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants