66

I thought

import sys
sys.path.append("/home/me/mydir")

is appending a dir to my pythonpath

if I print sys.path my dir is in there.

Then I open a new command and it is not there anymore.

But somehow Python cant import modules I saved in that dir.

What Am I doing wrong?

I read .profile or .bash_profile will do the trick.

Do I have to add:

PATH="/Me//Documents/mydir:$PYTHONPATH"
export PATH

To make it work?

Matthew Flaschen
  • 268,153
  • 48
  • 509
  • 534
MacPython
  • 17,451
  • 10
  • 40
  • 47

7 Answers7

88

Modifications to sys.path only apply for the life of that Python interpreter. If you want to do it permanently you need to modify the PYTHONPATH environment variable:

PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH

Note that PATH is the system path for executables, which is completely separate.

**You can write the above in ~/.bash_profile and the source it using source ~/.bash_profile

Mona Jalal
  • 29,571
  • 61
  • 202
  • 359
Matthew Flaschen
  • 268,153
  • 48
  • 509
  • 534
  • 3
    Thanks a lot (forgot that). WHERE do I put that? in .profile in .bash_profile? Before Macpython?: # Setting PATH for MacPython 2.6 # The orginal version is saved in .bash_profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}" export PATH Or after that? Does order matter? – MacPython Aug 02 '10 at 12:31
  • 1
    `.bash_profile`. If you already have a `.bash_profile`, I believe bash ignores `.profile`. Order doesn't matter here, because they're two different environment variables. – Matthew Flaschen Aug 02 '10 at 12:36
  • @Felix, note that the MacPython code he has deals with `PATH` (system path), a separate variable. – Matthew Flaschen Aug 02 '10 at 12:37
  • 2
    So I did add PYTHONPATH "path:$PYTHONPATH" export PYTHONPATH and AFTER I restarted my computer it worked. Big Thanks to Matthew and Felix!! – MacPython Aug 02 '10 at 13:21
  • Where is /Me/Documents/mydir? I don't have that folder on my system and $PYTHONPATH is empty – Jonathan Rys Jan 29 '19 at 22:34
  • @JonathanRys, thats an example folder, you will not find it in your computer. Me: your username, /Documents: if you want to put the path inside Documents folder, /mydir: example dir. – Rho Oct 13 '21 at 13:22
  • 1
    `$PYTHONPATH` does not exist for me... is this normal? – Charlie Parker Dec 10 '21 at 17:02
10

Not sure why Matthew's solution didn't work for me (could be that I'm using OSX10.8 or perhaps something to do with macports). But I added the following to the end of the file at ~/.profile

export PYTHONPATH=/path/to/dir:$PYTHONPATH

my directory is now on the pythonpath -

my-macbook:~ aidan$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/path/to/dir', ...  

and I can import modules from that directory.

Aidan Ewen
  • 12,509
  • 8
  • 60
  • 86
6

On MAC OS you can simply find the location of python/python3 by using the command which python or which python3. (works for Linux too)

And it should give something like:

For python

/usr/local/bin/python

For python3

/Library/Frameworks/Python.framework/Versions/3.9/bin/python3

Export the path to your bash_profile

In your terminal type

sudo nano ~/.bash_profile

Enter your password and paste the following lines

PYTHONPATH="/Library/Frameworks/Python.framework/Versions/3.9/bin/python3"
export PYTHONPATH

Press control + x to exit, and press y for saving on being asked to save

Press `enter' to return to terminal window

Source it using the following command in terminal, run

source ~/.bash_profile

Path to python3 should be updated now!!

shubham malik
  • 141
  • 2
  • 4
2

Mathew's answer works for the terminal python shell, but it didn't work for IDLE shell in my case because many versions of python existed before I replaced them all with Python2.7.7. How I solved the problem with IDLE.

  1. In terminal, cd /Applications/Python\ 2.7/IDLE.app/Contents/Resources/
  2. then sudo nano idlemain.py, enter password if required.
  3. after os.chdir(os.path.expanduser('~/Documents')) this line, I added sys.path.append("/Users/admin/Downloads....") NOTE: replace contents of the quotes with the directory where python module to be added
  4. to save the change, ctrl+x and enter Now open idle and try to import the python module, no error for me!!!
Tennom
  • 121
  • 1
2

Setting the $PYTHONPATH environment variable does not seem to affect the Spyder IDE's iPython terminals on a Mac. However, Spyder's application menu contains a "PYTHONPATH manager." Adding my path here solved my problem. The "PYTHONPATH manager" is also persistent across application restarts.

This is specific to a Mac, because setting the PYTHONPATH environment variable on my Windows PC gives the expected behavior (modules are found) without using the PYTHONPATH manager in Spyder.

Andros
  • 41
  • 5
2

On MacOS Big Surf the file to add the "export" is $HOME/.zprofile

So, this should work for adding PYTHONPATH to your Mac Big Surf environment variables:

export PYTHONPATH=$HOME/my_folder

If the file doesn't exist just create it in $HOME, normally /Users/my_user_name

This change in the file name is because the default shell for MacOS Big Surf is zsh and not bash

Kratos
  • 96
  • 3
0

In my .zshrc file located at /Users/your_username/.zshrc

I add the following line: export PYTHONPATH="${PYTHONPATH}:/your/path" and save it.

If the file doesn't exist, create a nameless .txt file and change its extension to .zshrc. It's a hidden file, so you need to press cmd+shift+. to see it.

I am using macOS Monterey.