Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError error with PyCharm project folder recs

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!

like image 242
Dnaiel Avatar asked Sep 21 '17 23:09

Dnaiel


People also ask

Why import is not working in PyCharm?

Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.


1 Answers

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 because which you don't realize 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

like image 179
Tarun Lalwani Avatar answered Oct 25 '22 02:10

Tarun Lalwani