哪里是两个Python VENV环境之间的连接

问题描述 投票:0回答:1

我有两个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

请帮助!

P.S注/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
python linux pytz python-venv
1个回答
1
投票

你需要检查你的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>site.py找到在Python启动的。 activate预规划<venv>/binPATH,这样当你键入“python”本地可执行文件开始,而不是系统中的一个。

最终,这导致它结合了全系统的标准库的具体VENV-第三方模块sys.path。这将是这个样子:

>>> sys.path
['', '<venv>/bin/python36.zip', <system Python platlib>, <system Python purelib>, '<venv>', '<venv>/lib/site-packages']

因此,通常情况下,不应该有来自另一个sys.path直接VENV从VENV逻辑产生的文件夹。他们可能会导致PYTHONPATH,或者从一些.pth文件,甚至从自己的代码。上述诊断应该显示他们来自哪里。

© www.soinside.com 2019 - 2024. All rights reserved.