0

Problem solved.
If you add your path to PYTHONPATH, you can import the packages inside that path. But what I am doing is try to import that path, this is wrong.
So in this case, I make a sub Dir at my path and that dir is now a package can be imported.
Still I have to include my path in file>settings>project structure as source.

==========================================================================
I am using python3.6 in anaconda, Ubuntu16.04.
I have my own package in path /home/gph/pyutils_gph.
Inside this dir are utils.py files. I have include this path in PYTHONPATH.
I can do

Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyutils_gph import utils
>>> 

in terminal. But the same import code shows error in pycharm. It can find my package, indicating it with red lines.
What else should I do to make pycharm know my own package?

==========================================================================

I opened the terminal inside pycharm, get output like below. I have that dir in PYTHONPATH, but I can not import it. What's wrong?

Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/gph/pyutils_gph', '/home/gph/Desktop/BorderSecure/detection_cnn/src', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python36.zip', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6/lib-dynload', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6/site-packages']
>>> from pyutils_gph import utils
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pyutils_gph'
>>> 
ToughMind
  • 987
  • 1
  • 10
  • 28
  • Possible duplicate of [PyCharm error: 'No Module' when trying to import own module (python script)](https://stackoverflow.com/questions/28705029/pycharm-error-no-module-when-trying-to-import-own-module-python-script) – razdi May 10 '19 at 04:39
  • Try invalidate cache and restart pycharm. – Underoos May 10 '19 at 04:46
  • I think [\[SO\]: How PyCharm imports differently than system command prompt (Windows) (@CristiFati's answer)](https://stackoverflow.com/questions/54955891/how-pycharm-imports-differently-than-system-command-prompt-windows/55083046#55083046) might help. – CristiFati May 10 '19 at 05:06
  • 1
    Possible duplicate of [How PyCharm imports differently than system command prompt (Windows)](https://stackoverflow.com/questions/54955891/how-pycharm-imports-differently-than-system-command-prompt-windows) – CristiFati May 10 '19 at 05:10
  • what do you have in folder `'/home/gph/pyutils_gph'` ? Is it `'/home/gph/pyutils_gph/pyutils_gph.py'` ? If not then probaly you have to add `/home/gph/` instead of `/home/gph/pyutils_gph` to `PYTHONPATH`. If you have `/home/gph/pyutils_gph/utils.py` and you add `/home/gph/pyutils_gph` then you have to do `import utils`. – furas May 10 '19 at 05:21

1 Answers1

1

If you have /home/gph/pyutils_gph in PYTHONPATH and you do from pyutils_gph import utils then it is looking for

"/home/gph/pyutils_gph/" + "pyutils_gph/utils.py` 

because from pyutils_gph import utils means pyutils_gph/utils.py and it adds it to every path from PYTHONPATH


You have to add to PYTHONPATH

/home/gph

and then it will give

"/home/gph/" + "pyutils_gph/utils.py` 

so you get correct path


First version can works in terminal if you run Anaconda in folder /home/gph because Python search packages also in current working directory so it finds pyutils_gph/utils.py directly in /home/gph without using PYTHONPATH

If you will go to different folder then it will not find local pyutils_gph/utils.py and you get the same error.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks for ur answer. But I can do `from pyutils_gph import utils` in terminal without any errors. – ToughMind May 10 '19 at 05:34
  • because probably you run Anaconda in `/home/gph` - so it looks for subfolder `pyutils_gph` in current working directory. Go to different folder and it will not work in terminal. – furas May 10 '19 at 05:37