gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:08 25 4
gpt4 key购买 nike

我有两个 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 之间的任何软链接(soft link)

(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并找到异常的来源,如果有的话。参见 Debugging modifications of sys.path跟踪对 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被放入 <venv>对于 site.py在 Python 启动时查找。 activate前置<venv>/binPATH以便在您键入“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 - 两个Python venv环境之间的连接在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54523408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com