两个Python venv环境之间的连接在哪里

时间:2019-02-04 19:53:34

标签: python linux pytz python-venv

我有两个python环境,并且它们之间有某种联系。

/home/testapi/API25/env是原始的venv /home/preprodapi/API25/env是通过第一个cpio副本创建的。这个工作找到了好几个月。但是现在出现了一个问题。

症状是,在preprodapi中,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 / preprodapi / ....切换到/ 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

出于记录,我找不到venv之间的任何软链接

(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:此主机上的另一个虚拟磁盘不会发生相同情况

[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

1 个答案:

答案 0 :(得分:1)

您需要检查sys.path并查找异常源(如果有)。有关跟踪sys.pathDebugging modifications of sys.path的更改方式的方法,请参见Can 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放入{{1 }},以便<venv>在Python启动时查找。 site.pyactivate之前加上<venv>/bin,以便在您键入“ PATH”时启动本地可执行文件而不是系统文件。

最终,这将导致python结合了系统范围的标准库和特定于venv的第三方模块。看起来像这样:

sys.path

因此,通常情况下,>>> sys.path ['', '<venv>/bin/python36.zip', <system Python platlib>, <system Python purelib>, '<venv>', '<venv>/lib/site-packages'] 中不应存在来自venv逻辑直接产生的另一个venv的文件夹。它们可能是由于PYTHONPATH,某些.pth文件甚至您自己的代码引起的。以上诊断信息应显示它们的来源。