Python包在virtualen中不工作

2024-05-16 20:55:22 发布

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

我正在尝试创建一个包,并遵循教程here。当我使用Mac上安装的原生Python时,代码工作得很好,但是当我激活virtualenv时,它就不工作了。你能告诉我为什么和怎样才能做到这一点吗?谢谢!在

# python -V
Python 2.7.10
# source env/bin/activate
(env) # python -V
Python 3.6.0
(env) # python test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    from Animals import Mammals
  File "/private/tmp/test/Animals/__init__.py", line 1, in <module>
    from Mammals import Mammals
ModuleNotFoundError: No module named 'Mammals'
(env) # deactivate
# python -V
Python 2.7.10
# python test.py
Printing members of the Mammals class
    Tiger
    Elephant
    Wild Cat
Printing members of the Birds class
    Sparrow
    Robin
    Duck
# ls -lrtah
total 8
-rw-r--r--  1 btm   wheel   324B Feb 12 02:29 test.py
drwxr-xr-x  5 btm   wheel   170B Feb 12 02:32 .
drwxr-xr-x  7 btm   wheel   238B Feb 12 02:32 env
drwxr-xr-x  9 btm   wheel   306B Feb 12 02:38 Animals
drwxrwxrwt  9 root  wheel   306B Feb 12 02:44 ..
# tree Animals
Animals
├── Birds.py
├── Birds.pyc
├── Mammals.py
├── Mammals.pyc
├── __init__.py
├── __init__.pyc
└── __pycache__
    └── __init__.cpython-36.pyc

# cat Animals/__init__.py
from Mammals import Mammals
from Birds import Birds
# virtualenv --version
15.0.3

Tags: frompytestimportenvinitfebwheel