Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to call one python script from another python script. #75

Open
manjarigoyal opened this issue Sep 13, 2016 · 1 comment
Open

how to call one python script from another python script. #75

manjarigoyal opened this issue Sep 13, 2016 · 1 comment

Comments

@manjarigoyal
Copy link

I have 5 python scripts and right now I run first script then after it finishes then I run 2 script and son on. Can anyone tell me how to add all these script so that If I run first script it should call the 2 script once the first one is done and then call the third script when second script is done. I am still learning working with Python. Any help is appreciated. Attached are two scripts for the reference.

Python.zip

Script1.py
Script2.py
Script3.py
Script4.py
Script5.py

Thanks

@Danny4096
Copy link

There are more than a few ways. I'll list them in order of inverted preference (i.e., best first, worst last):

  1. Treat it like a module: import file. This is good because it's secure, fast, and maintainable. Code gets reused as it's supposed to be done. Most Python libraries run using multiple methods stretched over lots of files. Highly recommended. Note that if your file is called file.py, your import should not include the .py extension at the end.

  2. The infamous (and unsafe) exec command: Insecure, hacky, usually the wrong answer. Avoid where possible.
    execfile('file.py') in Python 2
    exec(open('file.py').read()) in Python 3

  3. Spawn a shell process: os.system('python file.py'). Use when desperate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants