Steve Iron
Steve Iron

Reputation: 261

Python script to run command line which starts python script with specific python version

I need some help. Is there a possibility to let python start the command line in windows and let the command line execute a script in another python version on my pc?

Expample: I have two versions of python on my pc. One is within Anaconda and the other one is pure Python. Now I have some scripts I want to be executed in specific order. My problem is, that the Google Analytics API doesn't work with Anaconda and some other packages (like Simpy) doesn't work with pure Python. So I need to work with two different versions of python for one project.

Now I want to write a litte python file, which opens the command line and executes the scrips in specific order on my different Python-versions.

I know how to run a python file on the command line. It's via

C:\path_to_python\python.exe C:\path_to_file\file.py

But how can I make a python script executing that line above in the command line?

Hope someone can help me.

Thanks.

Upvotes: 0

Views: 4046

Answers (4)

Aymen Alsaadi
Aymen Alsaadi

Reputation: 1517

try this and let me know :

import sys

with open(sys.argv[1], 'r') as my_file:
     exec(my_file.read())

Upvotes: 0

hansn
hansn

Reputation: 2024

import os
os.system("C:\path_to_python\python.exe C:\path_to_file\file.py")

os.system() returns the command's exit value so if you need some output from the script this won't work.

Upvotes: 5

Emma
Emma

Reputation: 470

I suggest you look at subprocess

# this is new to python 3.5
import subprocess
cmd = subprocess.run(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"], stdout=subprocess.PIPE)
return_string = cmd.stdout

# alternative for getting command output in python 3.1 and higher
import subprocess
return_string = subprocess.check_output(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"])

Upvotes: 4

Strik3r
Strik3r

Reputation: 1057

Instead you can try writing a batch file in which you can specify the order how you want to run the files and with which version you have to run the file. lets say first i want to run a file in python2.7 and the later in python3.4 and my files were in d:/pythonfiles

RunningSequence.bat

d:
cd D:\pythonfiles
c:\python27\python.exe python27file.py
c:\python34\python.exe python34file.py 

Upvotes: 1

Related Questions