ModuleNotFoundError error with PyCharm project folder recs

Question:

I am working on a project in PyCharm. The project has the following structure:

/projectRoot/
   folder1/
       somecode.py
   utils/
       __init__.py
       myutils1.py

I’d want to know how I can do an import such that the import works when running the code in the pyCharm console in an interactive manner, as well as when running the code using the

python somecode.py 

command in the terminal.

Currently I do:

from utils.myutils1.py import myClass

But command line I get the error:

File “somecode.py”, line 10, in
from utils.myutils1 import myClass ModuleNotFoundError: No module named ‘utils’

and on PyCharm:

Traceback (most recent call last): File
“/home/ubuntu/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py”,
line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns) File “”, line 1, in
from utils.myutils1 import myClass ModuleNotFoundError: No module named ‘utils’

Any recommendations on the proper folder structure for modules within a project, and how to import them properly?

Thanks!

Asked By: Dnaiel

||

Answers:

You can use utils package inside folder1 folder:

enter image description here

Then the code will work either way:

from utils.myutils1 import myClass
Answered By: y.luis.rojo

To explain the answer I recreated that project structure you had

/projectRoot/
   folder1/
       somecode.py
   utils/
       __init__.py
       myutils1.py

somecode.py

from utils.myutils1 import myclass

if __name__ == "__main__":
   print(myclass)

myutils1.py

myclass="tarun"

Running them from pycharm works without any issues, but running them from terminal will produce below error

  File "somecode.py", line XX, in <module>
    from utils.myutils1 import myclass
ModuleNotFoundError: No module named 'utils'

The issue is that Pycharm does few things for you, which is why it is not working in the terminal. So before telling you what you need to, I will tell you two things that PyCharm does on its own.

Python Console

When you launch a Python Console from Pycharm, there is some code that gets executed, using preferences.

Python Console

As you can see there are two options

[X] Add content roots to PYTHONPATH
[ ] Add source roots to PYTHONPATH

And then a starting script as well. So what this does is that it adds the root of your project to python’s path. Which is controlled by two main ways sys.path and PYTHONPATH environment variable

If I run the below code in Python Console

>>> import sys
>>> sys.path
['/Applications/PyCharm.app/Contents/helpers/pydev', 
'/Applications/PyCharm.app/Contents/helpers/pydev', 
'/Users/tarun.lalwani/.virtualenvs/folderstructure27/lib/python27.zip', 
'/Users/tarun.lalwani/.virtualenvs/folderstructure27/lib/python2.7', ....
'/Users/tarun.lalwani/.virtualenvs/folderstructure27/lib/python2.7/site-packages', 
'/Users/tarun.lalwani/Desktop/payu/projects/folderstructure27']

As you can see '/Users/tarun.lalwani/Desktop/payu/projects/folderstructure27' is added to the Python terminal.

Python Configurations

When you configure to RUN in code using Pycharm, you have similar two options.

Run configurations

We can change the code of our somecode.py to below

import os
print (os.environ['PYTHONPATH'])

import sys
print (sys.path)

/Users/tarun.lalwani/Desktop/payu/projects/folderstructure27
['/Users/tarun.lalwani/Desktop/payu/projects/folderstructure27/folder1', 
'/Users/tarun.lalwani/Desktop/payu/projects/folderstructure27', ....,
'/Users/tarun.lalwani/.virtualenvs/folderstructure27/lib/python2.7/site-packages']

From the output we can see that PYTHONPATH is set to current project folder.

Running from terminal

Now let’s run the somecode.py from terminal with the modifications we made.

$ python somecode.py
Traceback (most recent call last):
  File "somecode.py", line 2, in <module>
    print (os.environ['PYTHONPATH'])
  File "/Users/tarun.lalwani/.virtualenvs/folderstructure27/bin/../lib/python2.7/UserDict.py", line 40, in __getitem__
    raise KeyError(key)
KeyError: 'PYTHONPATH'

So that indicates there is no PYTHONPATH when we ran it in terminal. Let us run it again by removing the print(os.environ['PYTHONPATH']) code. You will get the below output

['/Users/tarun.lalwani/Desktop/payu/projects/folderstructure27/folder1', ...
'/Users/tarun.lalwani/.virtualenvs/folderstructure27/lib/python2.7/site-packages']
Traceback (most recent call last):
  File "somecode.py", line 7, in <module>
    from utils.myutils1 import myclass
ImportError: No module named utils.myutils1

As you can see folder1 is added to sys.path because it is the folder containing somecode.py, but the root folder has not been added. The fix in terminal is simple, which is to set the root directory path in PYTHONPATH.

PYTHONPATH=`pwd`/.. python somcode.py

And now the code will work from terminal also.

But the way they work are different from Python Console.

IMPORTANT NOTE:
Python Console using PyCharm on remote interpreter.

If running the python console using the remote interpreter option pycharm will fail. This is because it will append the path of the local PC and not the path of the remote server.
In order to fix this problem one has to add a mapping between the local PC directory and the remote server path.
pycharmRemoteConfig
pycharmRemoteConfig2

Answered By: Tarun Lalwani

Similar error here and this appears to be working for me:

Make sure Project Interpreter is set to, for example: C:Python36python.exe (in my case) and not a copy somewhere or another.

‘File > Settings > Project ____ > Project Interpreter’

Or, long story short, if that route isn’t cooperating, can also try finding workspace.xml and manually change SDK_HOME before starting PyCharm:

<option name="SDK_HOME" value="C:Python36python.exe" />
Answered By: gseattle

Working on these brilliant and very welcome explanations, I ended up updating my shell (git-bash) .bashrc file (the config script launched when opening the bash) adding the following line wherever :

alias python="PYTHONPATH=$(pwd)/.. python",

i.e creating an alias for a linux command to have a shortcut and getting free from rewriting the python path assignment all the time.

The additional thing to say is that opening your terminal in PyCharm brings you in your project folder. Then you cd to your .py folder you want to run. So actually after this cd, you have to reload the bash to reload the pwd, i.e the path.

Reloading the bash is done by typing the bash command. (which reruns your .bashrc script.)

Answered By: RodKLV
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.