Reputation: 383
Situation: on the linux PC, the global package version installed: x.y.z In the project directory, requirements.txt specifies a.b.c version for package. a.b.c > x.y.z there is a bash script in the directory that sets up a virtual environment, installs the packages from requirements.txt in that virtual environment, and then runs pytest in the virtual environment.
the virtual environment is set up like so in the bash script:
#!/usr/bin/env bash
set -x
python3 -m pip install --user virtualenv
python3 -m virtualenv .env
source .env/bin/activate
After this, pytest is run in the script which runs a bunch of test scripts. In one of these test scripts, a python script is called like so:
command=["/usr/bin/python", "/path/to/script/script.py", ...(bunch of args)]
process = subprocess.Popen(command)
When I run the bash script, I get an output that specifies that the requirement for package==a.b.c is satisfied in the virtual environment:
Requirement already satisfied: package==a.b.c in ./.env/lib/python3.8/site-packages (from -r requirements.txt (line 42)) (a.b.c)
However, when I get to the point in the test script that calls the above python script.py, I get an error related to the global package version x.y.z unable to find a hardware device. This error is specific to version x.y.z and is fixed by using an updated version a.b.c as specified in requirements.txt and is what I thought we were using in the virtual environment.
The error references the global package as well:
File "/path/to/script/script.py", line 116, in <module>
run()
File "/path/to/script/script.py", line 82, in run
device = scan_require_one(config='auto')
File "**/home/jenkins/.local/lib/python3.8/site-packages/package/driver.py**", line 1353, in scan_require_one
raise RuntimeError("no devices found")
RuntimeError: no devices found
System information
whereas it should use the driver.py that's in .env (or so I thought). How should I get the test script to use the package from the virtual environment?
Upvotes: 1
Views: 302
Reputation: 383
The issue was that I was passing global version of python into subprocess. I used sys.executable instead, and this time it used the version of python inside the virtual environment, and used the version installed inside the virtual environment, and not the global version. I followed this question to solve, so full credit: python subprocess doesn't inherit virtual environment
Upvotes: 0
Reputation: 1
Maybe you are trying to run the script from an IDE where the default path is selected. Try to run the program from cmd after virtual environment activation. Or select the venv as the preferred path of your IDE.
Upvotes: -1