【问题标题】:Python search path - python can't find my modulesPython 搜索路径 - python 找不到我的模块
【发布时间】:2011-08-16 16:28:02
【问题描述】:

我正在尝试从命令行运行脚本,而 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.6 中,我可以 export PYTHONPATH="/a/b/c:/a/b/d" 并在这些文件夹中导入模块而不会出现问题。除了在我家里的一台特定机器上。在那个盒子上,我只能在 PYTHONPATH 中有一个路径,否则它不会导入任何东西。我的结论是那个盒子上的解释器没有正确编译。也许你也面临着类似的问题......
  • 我看不到您将模块的父目录的路径添加到sys.path

标签: python environment-variables


【解决方案1】:

您在oneclickcos/oneclickcos/mainapp/ 中是否有__init__.py?如果没有,请放入并重试 - 这可能会导致您看到的问题。

欲了解更多信息,请参阅http://docs.python.org/tutorial/modules.html

【讨论】:

    【解决方案2】:

    为了从这样的文件夹导入,它们必须设置为packagesoneclickcosmainapp 中是否有 __init__.py 文件?

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      相关资源
      最近更新 更多