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.

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.

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.

Answer from Tarun Lalwani on Stack Overflow
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 115000058690-Module-not-found-in-PyCharm-but-externally-in-Python
Module not found in PyCharm but externally in Python – IDEs Support (IntelliJ Platform) | JetBrains
If I run the code from the terminal it's all good but PyCharm does not see it. ... PyDev console: starting. Python 2.7.10 (default, Jul 15 2017, 17:16:57) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin import psutil Traceback (most recent call last): File "<input>", line 1, in <module> File "/Applications/PyCharm Edu.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) ImportError: No module named psutil
Discussions

python - ModuleNotFoundError error with PyCharm project folder recs - Stack Overflow
Running them from pycharm works ... produce below error · File "somecode.py", line XX, in 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 ... More on stackoverflow.com
🌐 stackoverflow.com
PyCharm error: 'No Module' when trying to import own module (python script) - Stack Overflow
I have written a module (a file my_mod.py file residing in the folder my_module). Currently, I am working in the file cool_script.py that resides in the folder cur_proj. I have opened the folder in More on stackoverflow.com
🌐 stackoverflow.com
Can't run python script from terminal but can from PyCharm: ModuleNotFoundError
I have never run python from a terminal before so I don’t know what is involved, I have always run my project from PyCharm. My project runs fine from PyCharm. I am running on Linux. My file structure is as follows: BackTesting - This is my project BackTestPkg - One of my packages __init__py ... More on discuss.python.org
🌐 discuss.python.org
19
0
October 29, 2024
pycharm gives me "ModuleNotFoundError" when ever i import a package and does not recognize all the built in functions
When you installed Pycharm did you tick the “add to PATH” option? This might not be the cause of the problem, but it sounds to me like your IDE isn’t finding the python directory properly More on reddit.com
🌐 r/pythontips
18
15
August 16, 2022
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360009406559-module-not-found-error-in-pycharm
module not found error (in pycharm) – IDEs Support (IntelliJ Platform) | JetBrains
Modules are typically installed for specific interpreters, be it the virtual environment interpreter or global system interpreter. You need to find out which interpreter you have used in Jupyter Notebook web app, and setup this interpreter in PyCharm.
Top answer
1 of 4
50

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.

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.

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.

2 of 4
0

You can use utils package inside folder1 folder:

Then the code will work either way:

from utils.myutils1 import myClass
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 10379280392978-Pycharm-module-not-found-even-when-installed-Linux
Pycharm module not found even when installed (Linux) – IDEs Support (IntelliJ Platform) | JetBrains
I am using pycharm on manjaro. My main.py file contains only 1 line: ``` import pygame ``` I have python 3.10 installed at path /usr/bin/python3.10 I have checked and pycharm is also using this as ...
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360000407910-Can-Import-Modules-in-Pycharm-ModuleNotFoundError-Problem-Fixed
Can Import Modules in Pycharm - ModuleNotFoundError: - Problem Fixed – IDEs Support (IntelliJ Platform) | JetBrains
May 1, 2018 - C:\Users\Scott\venv\code-play\Scripts\python.exe C:/Users/Scott/PycharmProjects/code-play/fun-np Traceback (most recent call last): File "C:/Users/Scott/PycharmProjects/code-play/fun-np", line 3, in <module> import numpy as np ModuleNotFoundError: No module named 'numpy' Process finished with exit code 1 ... Here is the project interpreter setting, and numpy is there. Also here are my environmental variables. I've also tried uninstalling and re-installing both python and pycharm.
Find elsewhere
🌐
StudyMite
studymite.com › python › module-not-found-error-in-pycharm
"Module Not Found Error" in Pycharm | StudyMite
March 1, 2023 - What Pycharm does is that while creating a new project, it by default creates a new virtual environment, without even inheriting the globally installed packages. Thus, we will be getting a "Module Not Found Error" even after the module is installed globally.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360008168000-PyCharm-doesn-t-recognize-installed-modules
PyCharm doesn't recognize installed modules – IDEs Support (IntelliJ Platform) | JetBrains
I found out, that `__init__.py` file were not shown as python files in project tree. The issue was that `__init__.py` was included in Settings --> File Types --> "File type auto-detected by file content".
🌐
Python.org
discuss.python.org › python help
Can't run python script from terminal but can from PyCharm: ModuleNotFoundError - Python Help - Discussions on Python.org
October 29, 2024 - I have never run python from a terminal before so I don’t know what is involved, I have always run my project from PyCharm. My project runs fine from PyCharm. I am running on Linux. My file structure is as follows: BackTesting - This is my project BackTestPkg - One of my packages __init__py ...
🌐
Reddit
reddit.com › r/pythontips › pycharm gives me "modulenotfounderror" when ever i import a package and does not recognize all the built in functions
pycharm gives me "ModuleNotFoundError" when ever i import a package and does not recognize all the built in functions : r/pythontips
August 16, 2022 - First make sure you have a valid python installation, then create a venv using pycharm UI 👍 my experience with pycharm is that it's superior in many cases over other ide's but it also takes more time to understand it ... I think you are installing the packages globally and are trying to access the packages from within the virtual environment. When you elect a python interpreter when starting your project, the virtual environment option is checked by default. I built Aquilia, a modular backend framework for Python.
🌐
I <3 CODE
ilovecodesite.wordpress.com › 2017 › 10 › 22 › module-not-found-in-pycharmpythonerror
Module not found in PyCharm/Python[Error]: – I <3 CODE
October 22, 2017 - Module not found in PyCharm/Python[Error]: Have you been getting the error “module not found ” or that kind of stuff when you run your code in pycharm. The error says that the module that you imported at the top of your code if not available. ...
🌐
DEV Community
dev.to › dmitryzub › debugging-pycharmintellij-idea-no-module-named-error-inside-virtual-environment-a7g
Debugging PyCharm/Intellij IDEA no module named error inside virtual environment - DEV Community
November 20, 2021 - Pycharm is unable to recognize installed local modules, since python interpreter selected is wrong. It should be the one, where your pip packages are installed i.e. virtual environment.
🌐
YouTube
youtube.com › the code city
Fix Module Not Found Error in PyCharm (ModuleNotFound Error) [2024] - YouTube
In this video, I'll show you how you can fix ModuleNotFoundError in PyCharm. This error occurs when the module that you are trying to use in your pycharm pro...
Published: January 11, 2024
Views: 5K
🌐
YouTube
youtube.com › watch
How to FIX "Module Not Found Error" on Pycharm for Pygame, Pandas, numpy, etc. - 2023 - YouTube
This video assesses the comments people have left about having the module not found error. Hopefully this helps someone!
Published: January 28, 2023
🌐
Reddit
reddit.com › r/learnpython › program works on pycharm but gives modulenotfound error when run from command.
r/learnpython on Reddit: Program works on PyCharm but gives ModuleNotFound error when run from command.
May 29, 2020 -

It is for the requests-html module. I have done a lot of searching online and all I really find are suggestions saying to use "pip3" (I am using Python 3.7) instead of "pip" when installing the module, reinstalling it, using the same interpreter, and checking the path. I have done all of these things and none of them work. I even tried installing it via PyCharm since it works when run from there to no avail.

I did some digging in the paths and found that, while all the other libraries have a folder and an info folder (e.g. pip/pip-20.1.1.dist-info), requests-html has only the dist-info file but I don't really know what to do about it. There is also a requests_html.py file but nothing seems to happen when it is executed. I am really frustrated and don't know what else to try.

Requests-html seems like an awesome library and I would love to be able to use it, any help resolving this issue would be appreciated!

🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 115000410510-PyCharm-won-t-recognize-installed-module
PyCharm won't recognize installed module – IDEs Support (IntelliJ Platform) | JetBrains
Could you please try a virtualenv with a name not starting with a digit? ... Solved! The error had to do with the fact that there are two redis modules available: redis and redis-py.
🌐
YouTube
youtube.com › ue casts
Fix PyCharm's "ModuleNotFoundError: No module named" | pip packages & Virtual Environments - YouTube
Fixing PyCharm's "ModuleNotFoundError: No module named" while using virtual environments even though it shows a package is installed. This video shows how to...
Published: November 4, 2022
Views: 2K
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 7296477046034-Module-not-found-error-even-though-it-shows-as-installed-in-python-interpreter-settings
Module not found error even though it shows as installed in python interpreter settings – IDEs Support (IntelliJ Platform) | JetBrains
August 24, 2022 - So you were right that the problem is with the imported module "LinkedList" itself. Now I know where to look for troubleshooting :) ... PyCharm contacts the remote repository to download and install packages. More info about the repository -- https://en.wikipedia.org/wiki/Python_Package_Index ... I found this issue that packages were not missing when running my script at the command line, yet missing when trying to debug with the IDE.