sssom-py icon indicating copy to clipboard operation
sssom-py copied to clipboard

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

Open joeflack4 opened this issue 3 years ago • 7 comments

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 avatar Feb 25 '22 00:02 joeflack4

@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 avatar Feb 25 '22 00:02 joeflack4

@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!

matentzn avatar Feb 25 '22 05:02 matentzn

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.

hrshdhgd avatar Feb 25 '22 15:02 hrshdhgd

@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)

joeflack4 avatar Feb 25 '22 17:02 joeflack4

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

image

matentzn avatar Feb 25 '22 18:02 matentzn

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.

joeflack4 avatar Feb 25 '22 19:02 joeflack4

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

matentzn avatar Feb 25 '22 20:02 matentzn