Python搜索路径 - python无法找到我的模块

时间:2011-08-16 16:28:02

标签: python environment-variables

我正在尝试从命令行运行脚本,而python绝对没有:

[graffias:~/testing.tustincommercial.com]$ export PYTHONPATH=`pwd`:$PYTHONPATH                                          
[graffias:~/testing.tustincommercial.com]$  python -c 'import oneclickcos.mainapp; mainapp.mail.worker_loop()'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named oneclickcos.mainapp
[graffias:~/testing.tustincommercial.com]$ python
Python 2.5.2 (r252:60911, Jan 24 2010, 17:44:40)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import oneclickcos
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named oneclickcos
>>>
[graffias:~/testing.tustincommercial.com]$ ls ./oneclickcos/mainapp/mail.py
./oneclickcos/mainapp/mail.py
[graffias:~/testing.tustincommercial.com]$

有什么想法吗?

请注意粗心:以上命令应为$python -c 'import oneclickcos.mainapp.mail; oneclickcos.mainapp.mail.worker_loop()'(另一个有效变体)。

2 个答案:

答案 0 :(得分:6)

__init__.pyoneclickcos/中有oneclickcos/mainapp/吗?如果没有,请将其中一个放入并重试 - 这可能会导致您遇到的问题。

有关详情,请参阅http://docs.python.org/tutorial/modules.html

答案 1 :(得分:3)

要从此类文件夹导入,必须将其设置为packagesoneclickcosmainapp中有__init__.py个文件吗?