garvarma
garvarma

Reputation: 13

Manage modules between virtual environments in python

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> 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'

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

Upvotes: 0

Views: 99

Answers (1)

CryptoFool
CryptoFool

Reputation: 23079

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.

Upvotes: 2

Related Questions