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!
You can use
utils
package insidefolder1
folder:Then the code will work either way:
To explain the answer I recreated that project structure you had
somecode.py
myutils1.py
Running them from pycharm works without any issues, but running them from terminal will produce below error
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.
As you can see there are two options
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
andPYTHONPATH
environment variableIf I run the below code in Python Console
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.
We can change the code of our
somecode.py
to belowFrom 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.So that indicates there is no
PYTHONPATH
when we ran it in terminal. Let us run it again by removing theprint(os.environ['PYTHONPATH'])
code. You will get the below outputAs you can see
folder1
is added tosys.path
because it is the folder containingsomecode.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.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.
Similar error here and this appears to be working for me:
Make sure Project Interpreter is set to, for example:
C:\Python36\python.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 changeSDK_HOME
before starting PyCharm: