- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 source a Python file from another Python file?
In order to source a Python file from another python file, you have to 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
Now in fileA, you can call any function inside fileB like:
In file B you have to define a function called my_func before you can use it in any other file.
def my_func(): print("Hello from B")
Now when you run fileA.py, you will get the output:
Hello from B
- How can I make one Python file run another?
- How to convert a Kotlin source file to a Java source file?
- How do I include a php.ini file in another php.ini file?
- How to move a file from one folder to another using Python?
- How do I copy a file in python?
- How can I get a file's permission mask using Python?
- How do I copy a binary file in Python?
- How do I check whether a file exists using Python?
- How to create a Python dictionary from text file?
- How can I restore a file created by mysqldump?
- How do I wrap a string in a file in Python?
- How to call Python file from within PHP?
- How to read data from one file and print to another file in Java?
- How are files extracted from a tar file using Python?
- How to read a file from command line using Python?