- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I make one Python file run another?
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')
- Related Articles
- How can I source a Python file from another Python file?
- How can I sort one list by values from another list in Python?
- How can I copy a file from one folder to another folder within a container in Docker?
- How can I make money with Python?
- How can I make Python interesting for me?
- How can I pass optional or keyword parameters from one function to another in Python?
- How to make loops run faster using Python?
- How to move a file from one folder to another using Python?
- How many Python classes should I put in one file?
- How can I make a time delay in Python?
- How do I include a php.ini file in another php.ini file?
- How can I position JButtons vertically one after another in Java Swing?
- How we can copy Python modules from one system to another?
- How can I append a tuple into another tuple in Python?
- How do I run two python loops concurrently?

Advertisements