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,020 points
68,310 views

4 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()


Hope this helps!!

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

Thanks!

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

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, 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 fileB.py into fileA.py, 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:

fileB.my_func()

2. You can use the exec command. 

execfile('file.py')

 executes the file.py file in the interpreter.

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

For example

os.system('python my_file.py')
answered Dec 16, 2020 by Roshni
• 10,520 points
0 votes

Try using os.system:

os.system("script2.py 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
2,835 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, 2020 in Python by Sirajul
• 59,230 points
5,328 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,560 points
21,913 views
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,023 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
3,420 views
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
1,230 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
• 3,040 points
605 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP