ModuleNotFoundError error with PyCharm project folder recs

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