Setting Up PYTHONPATH Manually in OSX

Pic source :

Since this post might be useful for English speaker as well, I’ll write this post in English.

Yesterday, I worked on my personal (random) project in Python. I wanted to refresh my Python skill after too much working with MATLAB for the past few months. Also, I don’t want to lose my prior knowledge about web automation–so I made a script to automate few repetitive tasks I’ve been doing for some time. In addition, this time I tried writing my code with PyCharm instead of the usual Sublime editor. Quite a nice exercise.

Anyway. After finishing the script, I ran it on PyCharm. Everything worked well, until I tested the executable file (which I’d need so the script can be put on OSX dock and executed by double-click). Then, here comes the problem: my script couldn’t locate all the Python libraries I used. Turns out that PyCharm somehow takes care of the Environmental Setting automatically, which is not the case when the script is run independently outside the IDE. In Windows, the Environmental Setting is pretty easy to configure. But in UNIX-based OS, it can be quite tricky. You need to know where the correct config file is located and modify it manually. Not to mention, Linux and OSX requires slightly different steps.

I remembered setting up a PYTHONPATH during my previous project, but since I only need to do it once and it had been so long time ago, I forgot how to do it. Worse, I didn’t bother to document it back then (stupid meee! 😦  ). It took almost half day and countless trial and error to figure out again the correct way (especially in OSX)–which is pretty hellish to be honest. There is no set standard, there are too many ways. I have tried adding and looking for the path in .bashrc, .profile, .bash_profile in various location to no avail. But after patiently digging Stackoverflow and random blogpost, I managed to construct the solution that works! It’s quite annoying that there is no straightforward answer to this.

So here it is..

  1. Display hidden files via Terminal by executing this command:
    defaults write AppleShowAllFiles YES
  2. In finder, go to your home folder, open the .bash_profile inside the home folder. You can do it via terminal also by accessing /Users/your_username and edit the bash file with Vi or any editor you like.
  3. Set the environment setting by adding PYTHONPATH, for example:
    # Setting PATH for Python 3.4
    # The orginal version is saved in .bash_profile.pysave
    export PATH
    export PYTHONPATH

    You can also add the PYTHONPATH for other Python versions you have.

  4. Save the file, close and restart Terminal, check if the path is already set up by running command: echo $PYTHONPATH . It should list all the PYTHONPATH you have previously added.
  5. Run your executable script.

If everything goes well, the errors related to import problem should be gone. Now your script can be executed independently outside IDE. If you want to hide the hidden files again, just run this command: defaults write AppleShowAllFiles NO

BONUS: You can change the icon of your executable file in OSX so it looks cuter 😀

But I’ll save that for other post. Meanwhile, if you are interested in the script I made–it’s an AutoLogin bot. The code can be found on my github : [link]

Hope it helps!


2 thoughts on “Setting Up PYTHONPATH Manually in OSX

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s