Python Sheepsheet: The simplest way to add a path to your PYTHONPATH in OS X

One day as I was writing python, I grew weary of trying to copy and paste code into the IDLE shell. So I attempted to import the module I was writing …

>>> import pussinboots

Traceback (most recent call last):
File "<pyshell#95>", line 1, in <module>
import pussinboots
ImportError: No module named pussinboots

“What do you mean? I just had it, I was just writing it a moment ago – heck it’s right here!” Then I learned about default paths and system paths and PATH and $PATH and PYTHONPATH. So, since I was on a Windows 7 computer, I used setx to add my working directory to the PYTHONPATH. Hoping for a similar solution, I combed the internet – mostly stackexchange – and found no BASH/scripting/Command Line solution.

In the process of learning all that stuff about the os module and os.getcwd() and os.chdir(), I just figured I’d keep importing os each time I needed it, until I found a programmatic approach I could use with all future mac/linux installs. [The idea is to maximize the reuseability of the everything, including the environment. Since I figure I’ll have to change computers at some point (things break, get reformatted, or I just move to another machine), I’ll keep a list of setup commands to reset my environment to as-close-to-how-I-like-it, and keep working with minimal overhead/downtime. Having figured that out with Win7, I just figured it’d be just as feasable (if not more so) in the mac/linux universe.

I quit looking when I found something easier.

Knowing that messing with the internal Python on which the OS was dependent, I was a bit wary of system-wide commands – and by extension, something executed as Admin in Terminal. I was poring through StackExchange (both SuperUser and StackOverflow) and came upon this question, where the asker is trying to do the same things as I (import modules from a non-default directory). However, the OP wants to add the directory in question to his Python path. (As did I.) Not understanding the primary upvoted answer [and being hesitant, as I said, to alter system-level variables], I kept reading, and the currently-third answer nailed it for me: just change the parameter within IDLE.

The rough concept is to change a parameter, loaded by IDLE, at time of loading. IDLE looks in the file idlemain.py to initialize a few variables – one of which is the path(s) to use when searching for modules to import. By adding sys.path.append("/Users/Me/Documents/PyProject") on the line directly under os.chdir(os.path.expanduser('~/Documents')), IDLE now knows to search my PyProject folder for modules.

Yes, I wanted something smooth and elegant and all CLI-ninja-esque. But this just works so much more easily.

Note that if you want to replicate my results, you’ll need to change the path to be the one that leads to your project… as I will do with future projects. And for the record, this is using IDLE 2.7.10 on OS X 10.8.5.
Advertisements

About Steve

I am. Growing Learning Living
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s