2

I had a python project with a lot of imports like:

from src.main.fr.some.module import someclass

and it was working good but my colleagues wanted the imports to be like:

from fr.some.module import someclass

Then I changed the PYTHONPATH in the activate script of my virtualenv like:

export PYTHONPATH="/home/giffon/Documents/wopmars/src/main:/home/giffon/Documents/wopmars/src/test"

and replaced all the src.main.fr.some.module with fr.some.module.

Then I tried my code in the console and the output was good (note that I am printing the PYTHONPATH at the beginning of my code and /home/giffon/Documents/wopmars/src/main appears like expected).

(WopMars)giffon@CZC0507G5C-HP-Z400:~/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing$ python3 Parser.py 
PRINTING THE PYTHONPATH
/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/pyparsing-2.1.4-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/cycler-0.10.0-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/pytz-2016.4-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/python_dateutil-2.5.3-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/six-1.10.0-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/numpy-1.11.0-py3.4-linux-x86_64.egg
/home/giffon/Documents/wopmars/src/main
/home/giffon/Documents/wopmars/src/test
/home/giffon/virtualenvs/WopMars/lib/python3.4
/home/giffon/virtualenvs/WopMars/lib/python3.4/plat-x86_64-linux-gnu
/home/giffon/virtualenvs/WopMars/lib/python3.4/lib-dynload
/usr/lib/python3.4
/usr/lib/python3.4/plat-x86_64-linux-gnu
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages



Reading the definition file... -> done.
Checking whether the file is well formed... -> done.
Building the execution DAG... -> done.
Writing the dot file... -> done.

But, since I am working with pycharm, I wanted the IDE to take my changes into account. And here comes the issues.

I read somewhere that I should modify the interpreter Python Path by doing the following:

File > Settings > Projet:wopmars > Projet Interpreter > "wheel" > More... > "Show path for the selected interpreter (WopMars interpreter selected)" > + > "Browse to /home/giffon/Documents/wopmars/src/main" > Ok > Ok > Apply > Ok

And then I execute the same code than above:

/home/giffon/virtualenvs/WopMars/bin/python3 /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing/Parser.py
PRINTING THE PYTHONPATH
/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/pyparsing-2.1.4-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/cycler-0.10.0-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/pytz-2016.4-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/python_dateutil-2.5.3-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/six-1.10.0-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/numpy-1.11.0-py3.4-linux-x86_64.egg
/home/giffon/Documents/wopmars/src/test
/home/giffon/Documents/wopmars/src/main
/home/giffon/virtualenvs/WopMars/lib/python3.4
/home/giffon/virtualenvs/WopMars/lib/python3.4/plat-x86_64-linux-gnu
/home/giffon/virtualenvs/WopMars/lib/python3.4/lib-dynload
/usr/lib/python3.4
/usr/lib/python3.4/plat-x86_64-linux-gnu
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages



Traceback (most recent call last):
  File "/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing/Parser.py", line 12, in <module>
    from fr.tagc.wopmars.framework.management.DAG import DAG
ImportError: No module named 'fr.tagc.wopmars.framework.management.DAG'

You probably noticed that the /home/giffon/Documents/wopmars/src/main appears in the PYTHONPATH meaning that the configuration of pycharm interpreter's path has been taken into account. So, the interpreter knows where to find modules but can't see fr... any idea for solving this issue?

It is probably not interesting for solving this issue but the error-raising code is:

print("PRINTING THE PYTHONPATH")                          
for p in sys.path:                                        
    print(p)                                              
print("\n\n")                                             


from fr.tagc.wopmars.framework.management.DAG import DAG  

Note: changing PYTHONPATH in .profile or .bashrc gave me the same results

Note2: if I don't export PYTHONPATH, the console give me the same error than pycharm

Luc Giffon
  • 173
  • 14

1 Answers1

0

I solved my issue thanks to Zulan's comment.

TLDR:

Put the src/main/ directory before src/test/ one in pycharm interpreter PYTHONPATH.


Using the -v option for python interpreter in both pycharm and console, I have got the following outputs:

Console output:

# /home/giffon/Documents/wopmars/src/main/fr/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/__pycache__/__init__.cpython-34.pyc'
import 'fr' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b160>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b358>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b400>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b4a8>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework.management' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b550>

Pycharm output:

# /home/giffon/Documents/wopmars/src/test/fr/tagc/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc' # <_frozen_importlib.SourceFileLoader object at 0x7fb1532422e8>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars' # <_frozen_importlib.SourceFileLoader object at 0x7fb153242390>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework' # <_frozen_importlib.SourceFileLoader object at 0x7fb153242438>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework.management' # <_frozen_importlib.SourceFileLoader object at 0x7fb1532424e0>
Traceback (most recent call last):
  File "/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing/Parser.py", line 12, in <module>
    from fr.tagc.wopmars.framework.management.DAG import DAG
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'fr.tagc.wopmars.framework.management.DAG'

You may have noticed the slight difference between the two output: console interpreter search in wopmars/src/main/ whereas pycharm interpreter search in wopmars/src/test/.

Actually, if I look carefully at the printed PYTHONPATH, they are not the same:

  • In the console, /home/giffon/Documents/wopmars/src/main appears before /home/giffon/Documents/wopmars/src/test in the list sys.path
  • In Pycharm, it's the opposite

Then, I put /home/giffon/Documents/wopmars/src/test after /home/giffon/Documents/wopmars/src/main in pycharm and it worked perfectly. I Think that python interpreter start finding the first part of the module name in src/test/ then he don't find the end and raise an error without looking in other paths.

Luc Giffon
  • 173
  • 14