Python - Module Not Found

后端 未结 6 518
陌清茗
陌清茗 2020-11-27 17:33

I am a beginner with Python. Before I start, here\'s my Python folder structure

-project
----src
------model
--------order.py
------hello-world.py

相关标签:
6条回答
  • 2020-11-27 17:46

    I had same error. For those who run python scripts on different servers, please check if the python path is correctly specified in shebang. For me on each server it was located in different dirs.

    0 讨论(0)
  • 2020-11-27 17:50

    All modules in Python have to have a certain directory structure. You can find details here.

    Create an empty file called __init__.py under the model directory, such that your directory structure would look something like that:

    .
    └── project
        └── src
            ├── hello-world.py
            └── model
                ├── __init__.py
                └── order.py
    

    Also in your hello-world.py file change the import statement to the following:

    from model.order import SellOrder
    

    That should fix it

    P.S.: If you are placing your model directory in some other location (not in the same directory branch), you will have to modify the python path using sys.path.

    0 讨论(0)
  • 2020-11-27 17:53

    you need to import the function so the program know what that is here is example:

    import os 
    import pyttsx3
    

    i had the same problem first then i import the function and it work so i would really recommend to try it

    0 讨论(0)
  • 2020-11-27 17:59

    you need a file named __init__.py (two underscores on each side) in every folder in the hierarchy, so one in src/ and one in model/. This is what python looks for to know that it should access a particular folder. The files are meant to contain initialization instructions but even if you create them empty this will solve it.

    0 讨论(0)
  • 2020-11-27 18:05

    After trying to add the path using:

    pip show
    

    on command prompt and using

    sys.path.insert(0, "/home/myname/pythonfiles")
    

    and didn't work. Also got SSL error when trying to install the module again using conda this time instead of pip.

    I simply copied the module that wasn't found from the path "Mine was in

    C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages 
    

    so I copied it to 'C:\Users\user\Anaconda3\Lib\site-packages'

    0 讨论(0)
  • 2020-11-27 18:06

    You need to make sure the module is installed for all versions of python

    You can check to see if a module is installed for python by running:

    pip uninstall moduleName

    If it is installed, it will ask you if you want to delete it or not. My issue was that it was installed for python, but not for python3. To check to see if a module is installed for python3, run:

    python3 -m pip uninstall moduleName

    After doing this, if you find that a module is not installed for one or both versions, use these two commands to install the module.

    • pip install moduleName
    • python3 -m pip install moduleName
    0 讨论(0)
提交回复
热议问题