Add to python path mac os x
PythonMacosAddPythonpathPython Problem Overview
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?
Python Solutions
Solution 1 - Python
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
Solution 2 - Python
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.
Solution 3 - Python
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!!
Solution 4 - Python
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.
- In terminal,
cd /Applications/Python\ 2.7/IDLE.app/Contents/Resources/
- then
sudo nano idlemain.py
, enter password if required. - after
os.chdir(os.path.expanduser('~/Documents'))
this line, I addedsys.path.append("/Users/admin/Downloads....")
NOTE: replace contents of the quotes with the directory where python module to be added - to save the change, ctrl+x and enter Now open idle and try to import the python module, no error for me!!!
Solution 5 - Python
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.
Solution 6 - Python
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
Solution 7 - Python
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.