0

I am trying to make a game, I have made a basic launcher. This is just another version of it. I made it in python 3.

I have tried watching YouTube videos on how to do it but they are all using python 2. I still tried it, it didn't work.

import os
print ("**Test Starting**")
print ("")
import time
time.sleep (0.75)
COMMAND = "SCRIPT_TWO.py"
os.system(COMMAND)

SCRIPT_TWO.py

print ("Hello, it worked")

I still want it to launch the other script but also transfer the variables. I want it to be as simple as possible.

jonrsharpe
  • 99,167
  • 19
  • 183
  • 334
Jared
  • 21
  • 2
  • 2
    you can use [`subprocess`](https://docs.python.org/3.7/library/subprocess.html) module in python to execute another python script. However, you do not know yet that *you don't want to do that.* You should instead be looking at importing 1 python script from another, and doing your work in 1 main script with function calls to other scripts. This sounds like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Are you familiar with something like `import script_two` for example? – Paritosh Singh Feb 02 '19 at 09:24
  • 1
    No i am not, i can look up on it. I have tried this: import subprocess subprocess.call("SCRIPT_TWO.py") – Jared Feb 02 '19 at 09:28
  • @Jared i would very highly recommend completely staying away from subprocess or anything os related for now. you may need to slightly restructure your code but `import` should be the way to go. – Paritosh Singh Feb 02 '19 at 09:37
  • "it didn't work" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Feb 02 '19 at 09:38

1 Answers1

0

If you're sure you don't want to import the first file from the second then the below answer is for you.

os.system(args) is a relatively simple function and does not allow passing of arguments. I suggest you take a look at subprocess.run(...). It allows you to pass arguments as a list which you can then access through the second script as normal command line arguments.

For example:

script1.py

import subprocess

subprocess.run(["python", "script2.py", "var1", "var2"])

script2.py

import sys

print(sys.argv) # prints ['script2.py', 'var1', 'var2']

In this way the subprocess module is very helpful, I suggest you take a look at its documentation here.

Faraaz Ahmad
  • 99
  • 2
  • 7