Python error “ImportError: No module named”

后端 未结 29 2617
野的像风
野的像风 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:11

    Yup. You need the directory to contain the __init__.py file, which is the file that initializes the package. Here, have a look at this.

    The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

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

    After just suffering the same issue I found my resolution was to delete all pyc files from my project, it seems like these cached files were somehow causing this error.

    Easiest way I found to do this was to navigate to my project folder in Windows explorer and searching for *.pyc, then selecting all (Ctrl+A) and deleting them (Ctrl+X).

    Its possible I could have resolved my issues by just deleting the specific pyc file but I never tried this

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

    In linux server try dos2unix script_name

    (remove all (if there is any) pyc files with command find . -name '*.pyc' -delete)

    and re run in the case if you worked on script on windows

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

    In my case, I was using sys.path.insert() to import a local module and was getting module not found from a different library. I had to put sys.path.insert() below the imports that reported module not found. I guess the best practice is to put sys.path.insert() at the bottom of your imports.

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

    an easy solution is to install the module using python -m pip install <library-name> instead of pip install <library-name> you may use sudo in case of admin restrictions

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

    I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:

    (NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)

    1) Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the mountain.py file, and trying to call the toolkit.interface.py module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit directory.

    2) When you are in the tookit directory, enter this line of code on your command line:

    export PYTHONPATH=.

    This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).

    3) After you've set your PYTHONPATH in the step above, run your module from your current directory (the toolkit directory). Python should now find and load the modules you specified.

    Hope this helps. I was quite frustrated with this myself.

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