Is it possible to call one python script from another Python Script?

0 votes
I have a script call abc.py in which I have some code. Now i want to run this script by calling it from another script called pqr.py. How to do it in Python?
Jul 2, 2019 in Python by Neel
• 3,010 points
4,149 views

2 answers to this question.

0 votes

Yes it is possible.

Suppose your script abc.py has the following code.

def my_func():
    print 'I am in abc'

if __name__ == '__main__':
    my_func()

Now you can call this script from another script pqr.py by following code

import abc

def service_func():
    print 'service func'

if __name__ == '__main__':
    service_func()
    abc.my_func()
answered Jul 2, 2019 by Arvind
• 2,980 points

this method doesn't work, since my_func() in if __name__=='__main__' when my_func() has arguments and also for command line arguments


0 votes

Hello, 

To call one python script from another Python Script

Step 1: Place the Python Scripts in the Same Folder

To start, you’ll need to place your Python scripts in the same folder.

For example, I placed two Python scripts (called python_1 and python_2) in the same folder as below:

Place 2 scripts in a folder

The ultimate goal is to run the python_2 script from the python_1 script.

Step 2: Add the Syntax

Next, add the syntax to each of your scripts.

For instance, I added the following syntax under the python_1 script:

import python_2

print ('what are you up to?')

Where:

  • The first line of ‘import python_2’ in the python_1 script, would call the second python_2 script. Whenever you want to run one Python script from another, you’ll need to import the exact name of the Python script that you’d like to call
  • The second part of the code simply prints the expression of ‘what are you up to?’

How to Run One Python Script From Another

Now let’s add the syntax under the python_2 script:

print ('hello world')

In this case, the expression of ‘hello world’ would be printed when running the second script.

Print in Python

Note that you must first save the syntax that was captured in the python_2 script before calling it from another script:

Save script

Step 3: Run One Python Script From Another

Now you’ll need to run the script from the python_1 box in order to call the second script:

How to Run One Python Script From Another

Notice that the results of the python_2 script would be displayed first, and only then the results of the python_1 script would be displayed:

hello world

what are you up to?

Hope it helps!!

answered Jun 30 by Niroj
• 45,880 points

Related Questions In Python

0 votes
1 answer

Is it possible to print all the modules imported in a python script?

Since I am using Python 3.6, I ...READ MORE

answered Jul 3, 2019 in Python by Neel
• 3,010 points
119 views
0 votes
2 answers

Is it possible to launch python idle from a virtual environment?

Try running the following code. #!/usr/bin/env python """Simple script ...READ MORE

answered Jun 25 in Python by Sirajul
• 55,750 points
1,041 views
0 votes
1 answer

Is it possible in Python requests to print entire HTTP request?

A better idea is to use the ...READ MORE

answered Nov 26, 2018 in Python by Nymeria
• 3,520 points
4,690 views
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,219 views
0 votes
0 answers
+4 votes
6 answers
0 votes
1 answer

Is it possible to access .NET code written in C# from Python?

If you want to mainly base your ...READ MORE

answered Jul 18, 2019 in Python by Arvind
• 2,980 points
247 views
0 votes
1 answer

is it possible to create zip of a directory in Python?

Please go through this code. This should ...READ MORE

answered Jul 10, 2019 in Python by Arvind
• 2,980 points
78 views