python ImportError: No module named primes

会有一股神秘感。 提交于 2019-12-12 01:56:01

问题


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.



来源:https://stackoverflow.com/questions/2355953/python-importerror-no-module-named-primes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!