How can I activate the virtual environment from a python script and execute further instructions while inside of it?
There is a number of things wrong here. When you run a subprocess, the environment it creates disappears when the subprocess exits.
What you can do is wrap Python inside Python, something like
import subprocess
env_name = "env_new"
subprocess.run(["py", "-m", "venv", env_name])
subprocess.run(["%s/bin/python" % env_name, "-c", """
the rest of your Python code here
"""])
which of course is just a rather pointless complication, and better written as a shell script.
#!/bin/bash
py -m venv env_new
. ./env_new/Scripts/activate
pip install -r requirements.txt
python ./your_real_script_here.py
Activate virtual environment from a python script
Looks like this answer I linked doesn't work for Python 3, but in the comments for the answer in that post I found the following from @Calimo which worked for me:
activate_this_file = "/path/to/venv/bin/activate_this.py"
exec(compile(open(activate_this_file, "rb").read(), activate_this_file, 'exec'), dict(__file__=activate_this_file))
Edit: After some discussion it looks like the real issue was using subprocess
without specifying the correct environment. By default subprocess
spawns a new process using the global environment. Specify the desired virtual environment by providing the env
arg when calling subprocess
, ex after activating the virtual environment with the code above:
venv= os.environ.copy()
subprocess.check_all(["pip3", "install", "flask"], env=venv)
Activating a Python virtual environment and calling python script inside another python script
Each virtualenv has its own python
executable which you can use directly to execute the script.
Using subprocess (more versatile than os.system
):
import subprocess
venv_python = '/path/to/other/venv/bin/python'
args = [venv_python, 'my_script.py', 'arg1', 'arg2', 'arg3']
subprocess.run(args)
Activating virtualenv from within Python script
The very 1st line of activate (note that VEnv is installed on Win, but this shouldn't be a problem):
# This file must be used with "source bin/activate" *from bash*
That, and the lines below should tell you that activate is a (Bourne) shell file.
[Python 3]: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) on the other hand, works with Python source code.
So, in order to execute the file, you'd need to use other ways, e.g. [Python 3]: subprocess - Subprocess management.
You can check how I've used it: [SO]: How to effectively convert a POSIX path to Windows path with Python in Cygwin? (@CristiFati's answer).
But, I really don't see the point of doing all this, you probably misunderstood your colleague's suggestion.
Also note that even if you do manage to do it this way, all the environment variables will only be set in the calling process, so it will pretty much be unusable (well, unless you also execute your script from there too).
You should go the recommended way ([PyPA]: Virtualenv - User Guide), and that is (from bash):
source /path/to/Django/ENV/bin/activate
python your_project_startup_script.py # (as I recall, it's manage.py)
How to create Python Virtual environment within a python script
To create a virtual env from inside a python script you can use the virtualenv
python module.
It pretty much comes down to a single line of code.
import virtualenv
import os
venv_dir = os.path.join(os.path.expanduser("~"), ".venv")
virtualenv.create_environment(venv_dir)
You can then activate this environment by accessing the activate_this.py
file in your .venv
folder, and install custom packages using pip
module.
Related Topics
Comprehensive Beginner's Virtualenv Tutorial
Main() Function Doesn't Run When Running Script
Importerror: No Module Named 'Encodings'
Where Is Python's Sys.Path Initialized From
Attributeerror: 'Module' Object Has No Attribute
How to Change the Font Size on a Matplotlib Plot
How to Turn a Python Datetime into a String, with Readable Format Date
Django. Override Save for Model
Download File from Web in Python 3
How to Lowercase a String in Python
Pandas Read_Csv: Low_Memory and Dtype Options
Convert Pandas Timezone-Aware Datetimeindex to Naive Timestamp, But in Certain Timezone
Python Try...Except Comma VS 'As' in Except