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.

mcmxl
  • 93
  • 1
  • 2
  • 8

6 Answers6

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()
Batman
  • 8,571
  • 7
  • 41
  • 80
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

Community
  • 1
  • 1
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()
Community
  • 1
  • 1
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.

martineau
  • 119,623
  • 25
  • 170
  • 301
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.

Right leg
  • 16,080
  • 7
  • 48
  • 81
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()
mcmxl
  • 93
  • 1
  • 2
  • 8