Python error “ImportError: No module named”

后端 未结 29 2615
野的像风
野的像风 2020-11-22 07:46

Python is installed in a local directory.

My directory tree looks like this:

(local directory)/site-packages/toolkit/interface.py

相关标签:
29条回答
  • 2020-11-22 08:00

    You are reading this answer says that your __init__.py is in the right place, you have installed all the dependencies and you are still getting the ImportError.

    I was facing a similar issue except that my program would run fine when ran using PyCharm but the above error when I would run it from the terminal. After digging further, I found out that PYTHONPATH didn't have the entry for the project directory. So, I set PYTHONPATH per Import statement works on PyCharm but not from terminal:

    export PYTHONPATH=$PYTHONPATH:`pwd`  (OR your project root directory)
    

    There's another way to do this using sys.path as:

    import sys
    sys.path.insert(0,'<project directory>') OR
    sys.path.append('<project directory>')
    

    You can use insert/append based on the order in which you want your project to be searched.

    0 讨论(0)
  • 2020-11-22 08:04

    I faced the same problem: Import error. In addition the library've been installed 100% correctly. The source of the problem was that on my PC 3 version of python (anaconda packet) have been installed). This is why the library was installed no to the right place. After that I just changed to the proper version of python in the my IDE PyCharm.

    0 讨论(0)
  • 2020-11-22 08:07

    Linux: Imported modules are located in /usr/local/lib/python2.7/dist-packages

    If you're using a module compiled in C, don't forget to chmod the .so file after sudo setup.py install.

    sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so
    
    0 讨论(0)
  • 2020-11-22 08:08

    To mark a directory as a package you need a file named __init__.py, does this help?

    0 讨论(0)
  • 2020-11-22 08:09

    Does

    (local directory)/site-packages/toolkit
    

    have a __init__.py?

    To make import walk through your directories every directory must have a __init__.py file.

    0 讨论(0)
  • 2020-11-22 08:10

    If you are using a setup script/utility (e.g. setuptools) to deploy your package, don't forget to add the respective files/modules to the installer.


    When supported, use find_packages() or similar to automatically add new packages to the setup script. This will absolutely save you from a headache, especially if you put your project aside for some time and then add something later on.

    import setuptools
    
    setuptools.setup(
        name="example-pkg",
        version="0.0.1",
        author="Example Author",
        author_email="author@example.com",
        description="A small example package",
        packages=setuptools.find_packages(),
        classifiers=[
            "Programming Language :: Python :: 3",
            "Operating System :: OS Independent",
        ],
        python_requires='>=3.6',
    )
    

    (Example taken from setuptools documentation)

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