简体   繁体   中英

Using Subprocess to run another python script

I am a newbie to python and would like to seek some advice. I having a script now where the function of this script can only be executed after I run a command ie python run trial.py . However, I would like to automate this process by using a subprocess function in a new python file called 'run.py' to run the trial.py script.

After that, I would wish to convert run.py file into an exe file to ease the execution for other users.

I have tried to perform the below steps.

1. saved the scripts (trial.py & run.py) in same directory.
2. Execute both of the files in same conda virtual environment.
3. Successfully execute run.py by using ```subprocess.run('xxx run trial.py')```
4. Converted the run.py into an exe file
5. Tried to execute the exe file and it is running, but **failed** to show the output that suppose to be appeared after running trial.py.

Would like to seek advice is any steps on above did wrongly or need to be improvised? I need to deal with confidential data hence the easiest way I can do is by using pyinstaller to allow another user to execute.

Hope to hear some advice

With subprocess, you can try it:

command = 'Your command' # Example: command = 'python trial.py'
process = subprocess.Popen(command, shell=True, stdout=sys.stdout, stderr=sys.stderr)

I would wish... to ease the execution for other users.

As I've understood your case, using subprocess and making an exe file out of your python file (which is not an easy task) is not a good fit for you.

Instead, I recommend you to have a look at make files as they are well-known for simplifying your commands.

for example you can have a make file like this:

run:
  python trial.py

And users can simply run make run , and python trial.py will run instead.

The possibilities are endless.


You can also make a bash file that is an executable,

# !/bin/bash

python trial.py 

And it will simply run like exe files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM