Add to python path mac os x
Asked Answered
K

8

78

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?

Kopaz answered 2/8, 2010 at 12:23 Comment(1)
$PYTHONPATH does not exist for me... is this normal?Bloodstock
A
99

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

Abuttal answered 2/8, 2010 at 12:28 Comment(7)
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?Kopaz
.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.Abuttal
@Felix, note that the MacPython code he has deals with PATH (system path), a separate variable.Abuttal
So I did add PYTHONPATH "path:$PYTHONPATH" export PYTHONPATH and AFTER I restarted my computer it worked. Big Thanks to Matthew and Felix!!Kopaz
Where is /Me/Documents/mydir? I don't have that folder on my system and $PYTHONPATH is emptySheley
@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.Recurved
$PYTHONPATH does not exist for me... is this normal?Bloodstock
T
26

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!!

Tredecillion answered 26/10, 2021 at 2:38 Comment(0)
C
11

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.

Chafer answered 13/11, 2012 at 10:44 Comment(3)
On Mac Osx terminal, if I simply write python, then it is going to python 2.7 and if I write python3.6 then it open up version 3.6. Is there anything I can do to point python to python3.6Florescence
You would have to create an alias in your .bash_profile.Posset
$PYTHONPATH does not exist for me... is this normal?Bloodstock
B
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!!!
Baese answered 26/6, 2014 at 8:53 Comment(0)
L
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.

Lorenlorena answered 10/6, 2016 at 13:22 Comment(0)
H
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

Hoffmann answered 25/5, 2021 at 16:51 Comment(0)
H
1

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.

Hourigan answered 12/4, 2022 at 11:47 Comment(2)
It was very useful for my Mac (Also Monterey) with chipset M1Bucentaur
export PYTHONPATH="${PYTHONPATH}:/opt/homebrew/opt/[email protected]/libexec/bin"Bucentaur
N
0

Saw all of the previous answer and tried it myself. But most would come to be useless.

When typing in terminal, the python accepted will be 2.7 but all new modules would be for v3.x so just type

python3 /path/to/file/main.py

This will eventually run your file in python 3 and all of the imports from installed packeges will work fine.

Nissie answered 23/7, 2023 at 7:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.