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

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

4 answers to this question.

0 votes

Yes it is possible.

Suppose your script has the following code.

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

if __name__ == '__main__':

Now you can call this script from another script by following code

import abc

def service_func():
    print 'service func'

if __name__ == '__main__':

Hope this helps!!

If you need to know more about Python, join Python online course today.


answered Jul 2, 2019 by Arvind
• 3,040 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


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?')


  • 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, 2020 by Niroj
• 82,880 points
0 votes

There are multiple ways to make one Python file run another.

1. Use it like a module. import the file you want to run and run its functions. For example, say you want to import into, assuming the files are in the same directory, inside fileA you'd write

import fileB

Now in fileA, you can call any function inside fileB like:


2. You can use the exec command. 


 executes the file in the interpreter.

3. You can spawn a new process using the os.system command.

For example

answered Dec 16, 2020 by Roshni
• 10,520 points
0 votes

Try using os.system:

os.system(" 1")

execfile is different because it is designed to run a sequence of Python statements in the current execution context. That's why sys.argv didn't change for you.

answered Dec 16, 2020 by Gitika
• 65,910 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,020 points
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, 2020 in Python by Sirajul
• 59,230 points
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,560 points
0 votes
1 answer
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

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

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
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
• 3,040 points
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
• 3,040 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP