1

I have tried every thing from

if __name__ == "__main__": 

to

os.system() 

I have looked through all the other similar questions on here and read official Python documentation.

I can't get this

import os ask1 = raw_input("Create bid? ") create = "createbid.py %s" % () def questions(): if ask1 == "yes": os.system(create) if ask1 == "no": quit() question() 

to run the ceatebid.py file reliably. I got it to work with

if __name__ == "__main__": 

but what if I also want to call another script?

I want to call different scripts based on how the questions are answered.

3
  • docs.python.org/3/tutorial/modules.html Commented Nov 16, 2016 at 2:49
  • have you try create = "python createbid.py %s" % () instead of create = "createbid.py %s" % () Commented Nov 16, 2016 at 2:50
  • I tried that. Both have the same results. Commented Nov 16, 2016 at 3:13

6 Answers 6

3

I'm not sure exactly what you're trying to do, but in general you should be able to do something like this.

import foo import bar ask = raw_input("Do something?") if ask.lower() in ('yes', 'y'): foo.do_something() else: bar.do_other() 
Sign up to request clarification or add additional context in comments.

Comments

2

The key to using os.system("python createbid.py") is to pass in a shell command in string format.

If you want to communicate with that script you probably want subprocess. See the answer from this question: running bash commands in python

Comments

2

This was probably answered here: What is the best way to call a Python script from another Python script?

So, you need to define some method in you createbid.py (and other scripts):

def run() print 'running' 

then in your main script,

import createbid def questions(): if ask1 == "yes": createbid.run() if ask1 == "no": quit() if __name__ == '__main__': questions() 

2 Comments

This worked when I used one other script. How do I choose from multiple other scripts?
You can import as many modules as you need, exactly how @batman suggests
2

Nowadays, the recommended way to launch other processes is to use the subprocess module.

It's relatively easy to do. Here's a simple way to apply it to your problem:

import subprocess import sys create = [sys.executable, 'createbid.py'] def question(ans): if ans == 'yes': subprocess.call(create) elif ans == 'no': quit() ask1 = raw_input('Create bid? ') question(ask1) print('done') 

Note: When createbid.py (or some other script) is executed this way,
__name__ == '__main__' will be True, unlike it would be if it had been imported.

Comments

0

Alternatively, you could use exec (statement in Python2, function in Python3).

Suppose your script scriptA is stored in a file named scriptA.py. Then:

scriptContent = open("scriptA.py", 'r').read() exec(scriptContent) 

The merit of this is that exec allows you to define variables before, and use them inside of the script.

So if you were to define some parameters before running the script, you could still call them in your solution:

#Main script param1 = 12 param2 = 23 scriptContent = open("scriptA.py", 'r').read() exec(scriptContent) #scriptA.py print(param1 + param2) 

Still, this approach is more like a fun trick, and depending on the situation, there should be several ways to do better.

Comments

0

Thanks for the help! I combined a few answers to get it working.

This works:

import seebid import createbid ask1 = raw_input("Create bid? ") ask2 = raw_input("View bid? ") create = createbid see = seebid def questions(): if ask1.lower() in ('yes', 'y'): create.createbd() elif ask1.lower() in ('no', 'n'): ask2 if ask2.lower() in ('yes', 'y'): see.seebd() if __name__ == "__main__": questions() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.