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:
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
>>> import fox
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:
el@apollo:/home/el/foo$ python main.py
The function moobar was imported from mylib.py and made available in main.py
Put this in /home/el/foo3/chekov.py:
print "where are the nuclear wessels?"
Put this in /home/el/foo3/main.py:
from chekov import question
el@apollo:/home/el/foo3$ python main.py
where are the nuclear wessels?
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/bittorrent/riaa.py:
print "there are honeypot kesha songs on bittorrent that log IP " +
"addresses of seeders and leechers. Then comcast records strikes against " +
"that user and thus, the free internet was transmogified into " +
"a pay-per-view cable-tv enslavement device back in the 20th century."
Put this in /home/el/foo4/stuff/riaa.py:
print "computers are transforming into a noose and a yoke for humans"
Put this in /home/el/foo4/main.py:
from riaa import *
el@apollo:/home/el/foo4$ python main.py
computers are transforming into a noose and a yoke for humans
el@apollo:/home/el/foo4$ python main.py
there are honeypot kesha songs on bittorrent...
That imports everything in the foreign file from a different directory.
Example 5, Import files in python with the bare import command:
Example 6, import your file via piggybacking the python startuphook:
def secretmessage(cls, myarg):
return myarg + " is if.. up in the sky, the sky"
secretmessage = classmethod( secretmessage )
return "cookie and sky pie people can't go up and "
skycake = classmethod( skycake )
msg = "The only way skycake tates good"
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
$ python main.py
The only way skycake tates good is if.. up in the sky,
the skycookie and sky pie people can't go up and have the sky pie!
Example 7, Most Robust: Import files in python with the bare import command:
el@apollo:/home/el/foo5/herp$ touch __init__.py
Make a new directory /home/el/foo5/herp/derp
el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:
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
print "SkyCake evolves to stay just beyond the cognitive reach of " +
"the bulk of men. SKYCAKE!!"
The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;
from herp.derp.yolo import skycake
el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk
of men. SKYCAKE!!
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
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 should also add Example 6: using __import__(py_file_name). Amazing guide anyway
What's the big difference between all of these, and why is one better than any other? For example 5, you write "Import files in python with the bare import command," but you also use the (bare?) import command in examples 1, 3 and 4, don't you?
Hey Eric! I think you got me wrong.. I just wanted to answer HelloGoodbyes question "what's the big difference between all of these" because I too was curious and found the blog entry (which is NOT mine btw) which I thought was helpful for him too...
Good answer but the fact that you use a different import file as example all the times makes it cumbersome to read.