我有两个python环境,它们之间有某种联系。
/home/testapi/API25/env是原始的静脉
/home/preprodapi/API25/env是通过第一个cpio副本创建的。这件工作了好几个月才找到。但现在出现了一个问题。
症状是,在prepodapi中,pytz包无法找到时区Africa/Johannesburg(可能还有其他),堆栈跟踪证明了这一点:

 Traceback (most recent call last):
   File "/home/preprodapi/API25.8512/validator/echo.py", line 244, in jsonified_wrapper
    response_obj = request_handler(*args, **kwargs)
   File "/home/preprodapi/API25.8512/validator/echo.py", line 478, in bearer_token_wrapper
    return request_handler(*args, **kwargs)
   File "/home/preprodapi/API25.8512/validator/echo.py", line 1068, in globaldb_connection_wrapper
    return request_handler(*args, **kwargs)
   File "/home/preprodapi/API25.8512/validator/echo.py", line 569, in get_school_wrapper
    return request_handler(*args, **kwargs)
   File "/home/preprodapi/API25.8512/validator/echo.py", line 697, in school_admin_wrapper
    return request_handler(*args, **kwargs)
   File "/home/preprodapi/API25.8512/routehandlers.py", line 4035, in email_report
    do_email_report(kwargs.get('reportid'), json_attrs, getctx_school().get('schoolname'))
   File "/home/preprodapi/API25.8512/validator/echo.py", line 1155, in do_email_report
    tz = pytz.timezone(sender.school.get("local_timezone"))
   File "/home/testapi/API25/env/lib64/python3.5/site-packages/pytz/__init__.py", line 181, in timezone
 pytz.exceptions.UnknownTimeZoneError: 'Africa/Johannesburg'

注意它是如何从/home/premodapi/…切换的。。。。到/home/testapi/。。。在最后一项。
但为什么会这样呢?
(env) [root@ip-172-31-8-200 API25]# deactivate
[root@ip-172-31-8-200 API25]# pwd
/home/preprodapi/API25
[root@ip-172-31-8-200 API25]# . env/bin/activate
(env) [root@ip-172-31-8-200 API25]# pip uninstall pytz
Uninstalling pytz-2017.2:
  Would remove:
    /home/testapi/API25/env/lib/python3.5/site-packages/pytz-2017.2.dist-info/*
    /home/testapi/API25/env/lib/python3.5/site-packages/pytz/*
Proceed (y/n)? n
(env) [root@ip-172-31-8-200 API25]# python
Python 3.5.5 (default, Feb  6 2018, 10:57:32)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytz
>>> print(pytz.timezone('Africa/Johannesburg'))
Africa/Johannesburg

作为记录,我找不到任何文氏
(env) [root@ip-172-31-8-200 API25]# pwd
/home/preprodapi/API25
(env) [root@ip-172-31-8-200 API25]# find env -type l -ls
40549590    0 lrwxrwxrwx   1 root     root            3 Feb  4 12:54 env/lib64 -> lib
40549592    0 lrwxrwxrwx   1 root     root           15 Feb  4 12:54 env/bin/python3.5m -> /bin/python3.5m
40549593    0 lrwxrwxrwx   1 root     root           10 Feb  4 12:54 env/bin/python -> python3.5m
40549594    0 lrwxrwxrwx   1 root     root           10 Feb  4 12:54 env/bin/python3 -> python3.5m

救命啊!
备注/home/preprodapi/API25.8512是/home/preprodapi/API25的cpio副本。当我在API25.8512子目录中测试时,得到了完全相同的结果
注2:此主机上的另一个venv不会发生同样的情况
[root@ip-172-31-8-200 API25.8512]# cd /home/apiuser
[root@ip-172-31-8-200 apiuser]# cd API25
[root@ip-172-31-8-200 API25]# . env/bin/activate
(env) [root@ip-172-31-8-200 API25]# pip uninstall pytz
Uninstalling pytz-2018.9:
  Would remove:
    /home/apiuser/API25/env/lib/python3.5/site-packages/pytz-2018.9.dist-info/*
    /home/apiuser/API25/env/lib/python3.5/site-packages/pytz/*
Proceed (y/n)? n

最佳答案

您需要检查sys.path并找到异常的来源(如果有的话)。参见Debugging modifications of sys.path了解如何跟踪sys.pathCan I zip all the python standard libs and the python still able to import it?的变化。
venv已实现via Py3's stock site.py

If a file named "pyvenv.cfg" exists one directory above sys.executable,
sys.prefix and sys.exec_prefix are set to that directory and
it is also checked for site-packages (sys.base_prefix and
sys.base_exec_prefix will always be the "real" prefixes of the Python
installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
the key "include-system-site-packages" set to anything other than "false"
(case-insensitive), the system-level prefixes will still also be
searched for site-packages; otherwise they won't.

创建venv时,python和许多其他文件被复制到<venv>/bin(在Windows中为<venv\Scripts)中,并将pyvenv.cfg放入<venv>中,以便在Python启动时查找site.pyactivate<venv>/bin前置到PATH以便在键入“python时启动本地可执行文件,而不是系统可执行文件。
最终,这会导致sys.path将系统范围的标准库与特定于venv的第三方模块结合起来。它看起来像这样:
>>> sys.path
['', '<venv>/bin/python36.zip', <system Python platlib>, <system Python purelib>, '<venv>', '<venv>/lib/site-packages']

所以,正常情况下,在sys.path中不应该有来自另一个venv的文件夹,直接由venv逻辑产生。它们可能来自PYTHONPATH或一些.pth文件,甚至来自您自己的代码。上面的诊断应该显示它们来自哪里。

关于python - 两个Python venv环境之间的连接在哪里,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54523408/

10-15 02:02