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

Dev environment: "ImportError: attempted relative import with no known parent package" #209

Open
joeflack4 opened this issue Feb 25, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@joeflack4
Copy link
Collaborator

joeflack4 commented Feb 25, 2022

Description

Due to use of relative imports, e.g. from .cli instead of absolute imports, e.g. from sssom.cli, I'm getting the error:

ImportError: attempted relative import with no known parent package

I've seen this problem a lot in the past, and unfortunately I've never found a reliable way to get relative imports to work in all situations where I might be running my codebase. On the other hand, anytime I've used absolute imports, my code has worked in all situations/environments.

Errors

Short err:
ImportError: attempted relative import with no known parent package

Long err:

/Users/joeflack4/virtualenvs/sssom-py/bin/python3.9 /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 59782 --file /Users/joeflack4/projects/sssom-py/sssom/cli.py --help
Connected to pydev debugger (build 211.7628.24)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/joeflack4/projects/sssom-py/sssom/cli.py", line 27, in <module>
    from .cliques import split_into_cliques, summarize_cliques
ImportError: attempted relative import with no known parent package
python-BaseException

What I've tried

  1. Googling for a simple fix. Didn't find one (at least not a simple / good one). I got a lot of threads like this (pretty much same as when I've experienced this issue in the past): https://youtrack.jetbrains.com/issue/PY-28509

Additional info

Pycharm debug config:
Screen Shot 2022-02-24 at 7 14 02 PM

Possible solutions

  1. Change all relative imports to absolute imports.
@joeflack4
Copy link
Collaborator Author

@cmungall @matentzn Have you guys ever been able either (a) to get relative imports to work in all situations where the codebase is run, or (b) at least gotten it to work (ideally in a simple way) with a PyCharm debug config?

If you guys like my proposed solution, I can submit a PR.

@joeflack4 joeflack4 added the bug Something isn't working label Feb 25, 2022
@matentzn
Copy link
Collaborator

@hrshdhgd can you advice on this?

Joe I am also using pycharm and I don't have that problem.. it must be something about how your project is set up!

@hrshdhgd
Copy link
Contributor

I had experienced this problem before and not any more. I think this is IDE related. I use VS code. Let me jog my memory to figure out what I did to solve it.

@joeflack4
Copy link
Collaborator Author

@matentzn I think you're right.

Harshad was very helpful and gave some suggestions for alternative setups. I tried that, but no go so far. Very curious to what your set up is, Nico! Mainly:

  1. Where is your virtualenv?
  2. What is an example of one of your debug configs? (like the screenshot I posted earlier)
  3. Did you do any other set up steps? (the above 2 steps are pretty much all I ever need to do when I'm setting up a PyCharm project)

@matentzn
Copy link
Collaborator

I think you need to set "sssom" to "root":

image

@joeflack4
Copy link
Collaborator Author

joeflack4 commented Feb 25, 2022

Thanks Nico. I tried that but I got a different error.

I've tried virtualenv and virtualenvwrapper before this. had the same issue with both.

I just did a fresh install of tox, but and still the same issues:

=================================================== 36 passed, 408 warnings in 66.65s (0:01:06) ===================================================
_____________________________________________________________________ summary _____________________________________________________________________
  lint: commands succeeded
  flake8: commands succeeded
  mypy: commands succeeded
  manifest: commands succeeded
  py: commands succeeded
  congratulations :)

Using sssom/ as "Sources Root" in PyCharm:

/Users/joeflack4/projects/sssom-py/.tox/py/bin/python3 /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 63626 --file /Users/joeflack4/projects/sssom-py/sssom/cli.py parse venv/input/snomed_icd_map/map.tsv --input-format snomed-icd10cm-map-tsv
Fatal Python error: init_sys_streams: can't initialize sys standard streams
Python runtime state: core initialized
Traceback (most recent call last):
  File "/Users/joeflack4/projects/sssom-py/sssom/io.py", line 3, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/__init__.py", line 26, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/traceback.py", line 5, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/linecache.py", line 11, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tokenize.py", line 27, in <module>
ImportError: cannot import name 'open' from 'builtins' (unknown location)

and if i unmark sources as root, I get the error I got before:

/Users/joeflack4/projects/sssom-py/.tox/py/bin/python3 /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 63629 --file /Users/joeflack4/projects/sssom-py/sssom/cli.py parse venv/input/snomed_icd_map/map.tsv --input-format snomed-icd10cm-map-tsv
Connected to pydev debugger (build 211.7628.24)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/joeflack4/projects/sssom-py/sssom/cli.py", line 27, in <module>
    from .cliques import split_into_cliques, summarize_cliques
ImportError: attempted relative import with no known parent package
python-BaseException

There must be something else about your set up.

Anyway, for now I'll probably just use absolute imports for development, then change them back to relative when I submit my final pull request.

@matentzn
Copy link
Collaborator

What is good is that tox at least works now.. but yeah as for the rest, I have waaaaay out of my depths!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants