python ImportError: No module named primes

2019-09-01 00:41发布

问题:

I'm really new to Python. I'm trying to import a third party module called primes.py. I have placed this module in C:\Python26\Lib (the location where I installed Python). I then have another file which is trying to import this module. The file attempting to import primes is located at C:\Python26.

In my Python file I have the following two lines:

import primes
import sys

When I run this file, I get the following error:

ImportError: No module named primes

Can anyone help me out?

回答1:

Put primes.py in the lib/site-packages/ directory.

Also: no need to put your own Python files under the installation directory: I'd advise you to put them somewhere else (where it makes sense).



回答2:

The module needs to be on your PYTHONPATH or in the same directory as the script, app, or module that is trying to import the module.

I'm not a Windows programmer but if you have placed the module in 'C:\Python26\Lib' and your path is set to 'C:\Python26' you need to add '\Python26\Lib' to your PYTHONPATH. I'm not certain on what the syntax would be but it should be something like 'C:\Python26;C:\Python26\Lib'. Assuming everything is the same on Windows, the subdirectories are not searched automatically.

I think a more appropriate place to put the module is to place it in 'site-packages', I don't know how this is accomplished on Windows. On *nix systems there is a script 'setup.py' that comes with the package/module, and uses 'setuptools' to build and install the package/module for you.



回答3:

you probably should located this under site-packages directory or a private folder instead. Check your sys.path to understand your import paths.