Python:导入模块故障,取决于运行mod

2024-10-03 02:43:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在virtualenv建立一个项目:

py-procr/
  procr/
    bin/
    lib/
    include/
    core/
      pcp.py
      __init__.py
  tests/
    __init__.py
    runner.py

在初级保健医生公司名称:

^{pr2}$

在跑步者.py公司名称:

^{3}$

这是运行模式:

(procr)a@s ~/spaces/python/py-procr $ python procr/core/pcp.py 
Main!
(procr)a@s ~/spaces/python/py-procr $ python tests/runner.py 
Traceback (most recent call last):
  File "tests/runner.py", line 2, in <module>
    from procr.core.pcp import *
ImportError: No module named 'procr'   ;; also 'core' and 'pcp' if you cut the import statement
(procr)a@s ~/spaces/python/py-procr $ python
Python 3.4.2 (default, Oct  8 2014, 13:44:52) 
[GCC 4.9.1 20140903 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import procr.core.pcp
>>> import tests.runner   ;; runner.py: from procr.core.pcp import *
>>> import tests.runner   ;; runner.py: from core.pcp import *
>>> import tests.runner   ;; runner.py: from pcp import *
>>> import core.pcp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'core'

我的Python路径:

>>> sys.path
['', '/home/alexey/spaces/python/py-procr/procr/lib/python34.zip', '/home/alexey/spaces/python/py-procr/procr/lib/python3.4', '/home/alexey/spaces/python/py-procr/procr/lib/python3.4/plat-linux', '/home/alexey/spaces/python/py-procr/procr/lib/python3.4/lib-dynload', '/usr/lib64/python3.4', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-linux', '/home/alexey/spaces/python/py-procr/procr/lib/python3.4/site-packages']
>>>

所以IDE/compiler(Eric)很高兴;pythonrepl也很高兴,但是我不能运行我的测试。在


Tags: frompycoreimporthomelinuxlibusr
1条回答
网友
1楼 · 发布于 2024-10-03 02:43:52

尝试使用-m选项运行代码:

python -m tests.runner

原因在PEP 338中解释:

Python 2.4 adds the command line switch -m to allow modules to be located using the Python module namespace for execution as scripts.

这里还有一些答案:What is the -m switch for in Python?

相关问题 更多 >