ModuleNotFoundError error with PyCharm project folder recs

前端 未结 3 1969
别跟我提以往
别跟我提以往 2020-12-23 18:02

I am working on a project in PyCharm. The project has the following structure:

/projectRoot/
   folder1/
       somecode.py
   utils/
       __init__.py
            


        
相关标签:
3条回答
  • 2020-12-23 18:19

    You can use utils package inside folder1 folder:

    Then the code will work either way:

    from utils.myutils1 import myClass
    
    0 讨论(0)
  • 2020-12-23 18:22

    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 change SDK_HOME before starting PyCharm:

    <option name="SDK_HOME" value="C:\Python36\python.exe" />
    
    0 讨论(0)
  • 2020-12-23 18:30

    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.

    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.

    0 讨论(0)
提交回复
热议问题