管理Python虚拟环境中的模块

huangapple go评论62阅读模式
英文:

Manage modules between virtual environments in python

问题

也许这个问题已经存在,或者我试图找到更好的解决方案。

我有一个使用Python的Linux服务器。在该服务器上,我有多个不同脚本的虚拟环境,为了不生成或重复信息,我有一个名为"General"的文件夹,其中包含我通常在不同脚本(虚拟环境)中使用的所有Python脚本,如一些类、用于发送SNMP陷阱的脚本、与摄像头一起使用的axis_api等。结构如下:

home
|- General/
    |- venv/
    |- axis_operations.py
    |- readfile.py
    |- remote_operations.py
    |- snmptrap.py
    |- homeclass.py
|- AxisCamera/
    |- venv/
    |- axis_cameras_status.py
|- Stats/
    |- venv/
    |- getstatsfromremoteserver.py

在axis_cameras_status.py脚本中,我导入了axis_operations.py。同时,在其他路径的axis_operations.py中,使用了不同的虚拟环境,而且在该虚拟环境中安装了(requests)的requests包。

当我尝试在其虚拟环境中执行axis_cameras_status.py脚本时,我遇到了以下错误(requests包仅安装在"General"虚拟环境文件夹中)。

(venv) user@server:~/AxisCamera> python axis_cameras_status.py
Traceback (most recent call last):
  File "axis_cameras_status.py", line 28, in <module>
    from axis_operations import AxisCamera
  File "/home/General/axis_operations.py", line 16, in <module>
    import requests     #https://www.dataquest.io/blog/python-api-tutorial/
ModuleNotFoundError: No module named 'requests'

最好的管理选项是什么?是否可以在每个虚拟环境中导入这些通用脚本的文件夹(及其虚拟环境)?

谢谢。

英文:

Maybe this question was already or I try to get the better solution.

I have a linux server that uses python. In that server I have severals virtual environments with differents scripts. To no generate or duplicate info, i have a folder called General, where I have all python scripts that usually I will use in the differents scripts (virtual environments) like some clases, script to send snmp traps, axis_api with cameras, etc. The structure is that one:

home
|- General/
    |- venv/
    |- axis_operations.py
    |- readfile.py
    |- remote_operations.py
    |- snmptrap.py
    |- homeclass.py
|- AxisCamera/
    |- venv/
    |- axis_cameras_status.py
|- Stats/
    |- venv/
    |- getstatsfromremoteserver.py

In the axis_cameras_status.py script, I import the axis_operations.py. At the same time, the axis_operations.py that is in other path with different virtual environment and in that venv is insatlled (requests) I import requests.

When I try to execute the script axis_cameras_status.py in it's virtual environment i have this error (the requests package in only installed in the virtual environment folder General).

(venv) user@server:~/AxisCamera&gt; python axis_cameras_status.py Traceback (most recent call last):   File &quot;axis_cameras_status.py&quot;, line 28, in &lt;module&gt;
    from axis_operations import AxisCamera   File &quot;/home/General/axis_operations.py&quot;, line 16, in &lt;module&gt;
    import requests     #https://www.dataquest.io/blog/python-api-tutorial/ ModuleNotFoundError: No module named &#39;requests&#39;

What will be the best option to manage all this? It's possible, have a folder (with their virtual environment) where I have all general scripts, then in every virtual environment, import these general scripts?

Thanks

答案1

得分: 2

听起来你想在运行Python代码时以某种方式利用两个虚拟环境,或者可能你希望一个虚拟环境中包含另一个虚拟环境的模块。这两者你都不能做到。无论你试图运行什么样的代码,你运行代码的Python环境都必须包含代码所依赖的所有模块,无论这些代码来自何处。如果你有4个用于运行4个脚本的虚拟环境,并且每个脚本都包含一些需要requests库的通用代码,那么这4个环境都必须安装了requests库。

英文:

It sounds like you want to somehow make use of two virtual environments when you run your Python code, or maybe you want to have the modules from one virtual environment included by another. You can't do either of these things. For whatever code you are trying to run, the Python environment you run your code in must contain all of the modules that your code depends on, no matter where the code came from. If you have 4 virtual environments used to run 4 scripts, and each script includes some common code that requires the requests library, then all 4 environments must have the requests library installed in it.

huangapple
  • 本文由 发表于 2023年1月9日 03:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定