16

I'm trying to run my project from terminal but I keep on getting ModuleNotFoundError: No module named 'config'. The structure of my project is:

Project folder
   -config
      -settings.py
   -folder1
     -folder2
        -pythonfile.py

While in folder1/folder2/ I run the script --> python3 -m pythonfile.py but I get the No module named config. The Run button from PyCharm works like charm but I want to run the script from terminal. Also I've checked the sys.path and I've got the root path of the project /home/name/Desktop/Project and the /home/name/Desktop/Project/folder1/folder2/.

Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib/python3.6/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/home/name/Desktop/Project/folder1/folder2/pythonfile.py", line 4, in <module>
    from config import settings as CONFIG
ModuleNotFoundError: No module named 'config'

Uponn
  • 199
  • 2
  • 3
  • 15
  • 1
    I think you're trying to do a relative import in a non-package, but correct me if I'm wrong. See https://stackoverflow.com/a/14132912/4518341 – wjandrea Dec 07 '19 at 16:56

5 Answers5

10

This issue occurs because, the path to the file app_one is not in the current working path, you have to add it to the path using sys.path.append function, Check this code :

import sys
sys.path.append('../../')
import config
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
9

Installing it worked for me: pip3 install config

9

Try adding current dir to PYTHONPATH. PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. This helped for me.

export PYTHONPATH=.
ky_aaaa
  • 290
  • 3
  • 10
1

If you are using Python 3, run the following to install config.

sudo pip3 install config

Trey Copeland
  • 3,387
  • 7
  • 29
  • 46
0

after pip3 install config:

from config import config
      ImportError: cannot import name 'config' from 'config' (C:\Python310\lib\site-packages\config\__init__.py)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 07 '23 at 23:54