How do I import other files in Python?
- How exactly can I import a specific python file like
import file.py
? - How can I import a folder instead of a specific file?
- I want to load a Python file dynamically at runtime, based on user input.
- I want to know how to load just one specific part from the file.
For example, in main.py
I have:
from extra import *
Although this gives me all the definitions in extra.py
, when maybe all I want is a single definition:
def gap():
print
print
What do I add to the import
statement to just get gap
from extra.py
?
There are many ways to import a python file, all with their pros and cons.
Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.
I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7
Example 1, Import a python module with python interpreter:
Put this in /home/el/foo/fox.py:
Get into the python interpreter:
You imported fox through the python interpreter, invoked the python function
what_does_the_fox_say()
from within fox.py.Example 2, Use
execfile
or (exec
in Python 3) in a script to execute the other python file in place:Put this in /home/el/foo2/mylib.py:
Put this in /home/el/foo2/main.py:
run the file:
The function moobar was imported from mylib.py and made available in main.py
Example 3, Use from ... import ... functionality:
Put this in /home/el/foo3/chekov.py:
Put this in /home/el/foo3/main.py:
Run it like this:
If you defined other functions in chekov.py, they would not be available unless you
import *
Example 4, Import riaa.py if it's in a different file location from where it is imported
Put this in /home/el/foo4/stuff/riaa.py:
Put this in /home/el/foo4/main.py:
Run it:
That imports everything in the foreign file from a different directory.
Example 5, use
os.system("python yourfile.py")
Example 6, import your file via piggybacking the python startuphook:
See: https://docs.python.org/3/library/user.html
Put this code into your home directory in
~/.pythonrc.py
Put this code into your main.py (can be anywhere):
Run it:
Credit for this jist goes to: https://github.com/docwhat/homedir-examples/blob/master/python-commandline/.pythonrc.py Send along your up-boats.
Example 7, Most Robust: Import files in python with the bare import command:
/home/el/foo5/
/home/el/foo5/herp
Make an empty file named
__init__.py
under herp:Make a new directory /home/el/foo5/herp/derp
Under derp, make another
__init__.py
file:Under /home/el/foo5/herp/derp make a new file called
yolo.py
Put this in there:The moment of truth, Make the new file
/home/el/foo5/main.py
, put this in there;Run it:
The empty
__init__.py
file communicates to the python interpreter that the developer intends this directory to be an importable package.If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131
Bonus protip
whether you are using Mac, Linux or Windows, you need to be using python's idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII
You can also do this:
from filename import something
example:
from client import Client
Note that you do not need the.py .pyw .pyui
extension.