两个Python venv环境的连接在哪里
Where is the connection between the two Python venv environments
我有两个 python 环境,它们之间不知何故存在某种 link。
/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 的
之间找不到任何软 links
(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
您需要检查您的 sys.path
并找到异常的来源(如果有)。请参阅 for a way to track changes to sys.path
and 了解其构造方式。
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
(<venv\Scripts
in Windows),以及 pyvenv.cfg
被放入 <venv>
以供 site.py
在 Python 启动时查找。 activate
将 <venv>/bin
添加到 PATH
前面,以便在您键入“python
”时启动本地可执行文件而不是系统可执行文件。
最终,这会导致 sys.path
将系统范围的标准库与特定于 venv 的第 3 方模块结合在一起。它看起来像这样:
>>> sys.path
['', '<venv>/bin/python36.zip', <system Python platlib>, <system Python purelib>, '<venv>', '<venv>/lib/site-packages']
因此,通常情况下,sys.path
中不应该有来自另一个 venv 的文件夹直接来自 venv 逻辑。它们可能来自 PYTHONPATH,或来自某些 .pth 文件,甚至来自您自己的代码。上面的诊断应该显示它们的来源。
我有两个 python 环境,它们之间不知何故存在某种 link。
/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 的
之间找不到任何软 links(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
您需要检查您的 sys.path
并找到异常的来源(如果有)。请参阅 sys.path
and
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
(<venv\Scripts
in Windows),以及 pyvenv.cfg
被放入 <venv>
以供 site.py
在 Python 启动时查找。 activate
将 <venv>/bin
添加到 PATH
前面,以便在您键入“python
”时启动本地可执行文件而不是系统可执行文件。
最终,这会导致 sys.path
将系统范围的标准库与特定于 venv 的第 3 方模块结合在一起。它看起来像这样:
>>> sys.path
['', '<venv>/bin/python36.zip', <system Python platlib>, <system Python purelib>, '<venv>', '<venv>/lib/site-packages']
因此,通常情况下,sys.path
中不应该有来自另一个 venv 的文件夹直接来自 venv 逻辑。它们可能来自 PYTHONPATH,或来自某些 .pth 文件,甚至来自您自己的代码。上面的诊断应该显示它们的来源。