I have a python script, and from that I want to run another script in
a subprocess in a venv. What is the best way to do that? I could write
a file that activates the venv then runs the script, then run that
file, but that seems messy. Is there a better way?
I have a python script, and from that I want to run another script inActivating a venv it is practical when you're working in a shell, but
a subprocess in a venv. What is the best way to do that? I could write
a file that activates the venv then runs the script, then run that
file, but that seems messy. Is there a better way?
On Sat, 21 Oct 2023 09:01:18 -0400
Larry Martell via Python-list <python-list@python.org> wrote:
I have a python script, and from that I want to run another script in
a subprocess in a venv. What is the best way to do that? I could write
a file that activates the venv then runs the script, then run that
file, but that seems messy. Is there a better way?
How do you do that?
It sounds messy but not wrong...
I would activate the venv and then run my Python script. In the Python
script you can call another python script in a subprocess like this:
import sys
import subprocess
# https://docs.python.org/3/library/subprocess.html#popen-constructor
proc = subprocess.Popen([sys.executable, "/path/to/an/otherscript.py"])
# https://docs.python.org/3/library/subprocess.html#popen-objects
# Do your process communication/handling... proc.communicate(),
# proc.wait(), proc.terminate(), proc.kill() etc.
Is this the answer you are looking for?
Detailed docs: https://docs.python.org/3/library/subprocess.html
On Sat, Oct 21, 2023 at 9:49 AM Johannes Findeisen
<mailman@hanez.org> wrote:
On Sat, 21 Oct 2023 09:01:18 -0400
Larry Martell via Python-list <python-list@python.org> wrote:
I have a python script, and from that I want to run another
script in a subprocess in a venv. What is the best way to do
that? I could write a file that activates the venv then runs the
script, then run that file, but that seems messy. Is there a
better way?
How do you do that?
How? Open a file and write the commands I need then invoke that.
It sounds messy but not wrong...
I would activate the venv and then run my Python script. In the
Python script you can call another python script in a subprocess
like this:
import sys
import subprocess
#
https://docs.python.org/3/library/subprocess.html#popen-constructor
proc = subprocess.Popen([sys.executable,
"/path/to/an/otherscript.py"])
# https://docs.python.org/3/library/subprocess.html#popen-objects
# Do your process communication/handling... proc.communicate(),
# proc.wait(), proc.terminate(), proc.kill() etc.
Is this the answer you are looking for?
Detailed docs: https://docs.python.org/3/library/subprocess.html
I know how to use Popen. What I was missing was running the script
using sys.executable. Thanks.
On Sat, Oct 21, 2023 at 9:49 AM Johannes Findeisen <mailman@hanez.org> wrote:
On Sat, 21 Oct 2023 09:01:18 -0400
Larry Martell via Python-list <python-list@python.org> wrote:
I have a python script, and from that I want to run another script in
a subprocess in a venv. What is the best way to do that? I could write
a file that activates the venv then runs the script, then run that
file, but that seems messy. Is there a better way?
How do you do that?
How? Open a file and write the commands I need then invoke that.
It sounds messy but not wrong...
I would activate the venv and then run my Python script. In the Python
script you can call another python script in a subprocess like this:
import sys
import subprocess
# https://docs.python.org/3/library/subprocess.html#popen-constructor
proc = subprocess.Popen([sys.executable, "/path/to/an/otherscript.py"])
# https://docs.python.org/3/library/subprocess.html#popen-objects
# Do your process communication/handling... proc.communicate(),
# proc.wait(), proc.terminate(), proc.kill() etc.
Is this the answer you are looking for?
Detailed docs: https://docs.python.org/3/library/subprocess.html
I know how to use Popen. What I was missing was running the script
using sys.executable. Thanks.
On Sat, 21 Oct 2023 11:32:03 -0400
Larry Martell <larry.martell@gmail.com> wrote:
On Sat, Oct 21, 2023 at 9:49 AM Johannes Findeisen
<mailman@hanez.org> wrote:
On Sat, 21 Oct 2023 09:01:18 -0400
Larry Martell via Python-list <python-list@python.org> wrote:
I have a python script, and from that I want to run another
script in a subprocess in a venv. What is the best way to do
that? I could write a file that activates the venv then runs the script, then run that file, but that seems messy. Is there a
better way?
How do you do that?
How? Open a file and write the commands I need then invoke that.
It sounds messy but not wrong...
I would activate the venv and then run my Python script. In the
Python script you can call another python script in a subprocess
like this:
import sys
import subprocess
#
https://docs.python.org/3/library/subprocess.html#popen-constructor
proc = subprocess.Popen([sys.executable,
"/path/to/an/otherscript.py"])
# https://docs.python.org/3/library/subprocess.html#popen-objects
# Do your process communication/handling... proc.communicate(),
# proc.wait(), proc.terminate(), proc.kill() etc.
Is this the answer you are looking for?
Detailed docs: https://docs.python.org/3/library/subprocess.html
I know how to use Popen. What I was missing was running the script
using sys.executable. Thanks.
sys.executable is the path to the actual Python binary, e.g. "/usr/bin/python". You could add "/usr/bin/python" there manually but
this is not portable to Windows for example.
When you add a shebang line to your other script and the file is
executable, you may not need to add sys.executable as first argument to
Popen but using sys.executable is the most reliable way to do this... ;)
I have a python script, and from that I want to run another script in
a subprocess in a venv. What is the best way to do that? I could write
a file that activates the venv then runs the script, then run that
file, but that seems messy. Is there a better way?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 76:25:33 |
Calls: | 6,716 |
Calls today: | 4 |
Files: | 12,247 |
Messages: | 5,357,568 |