Using anaconda/ipython in terminal [duplicate] - python
When someone says "edit your .plist file" or "your .profile" or ".bash_profile" etc, this just confuses me. I have no idea where these files are, how to create them if I have to do that, etc, and also why there seem to be so many different ones (why? Do they do different things?)
So could someone please explain very patiently to a previous Windows user (wanting desperately to become more familiar with the pleasant if initially somewhat confusing OS X world) how to do this step by step?
I need the variables to be set both for GUI applications and command line applications, and at the moment it's for an ant script that needs the variables, but there will most likely be other needs as well.
Please note that I have Lion too, since many of the answers you get Googling seem to be outdated for Lion...
Also note that I have practically zero experience using the Terminal. I'm willing to learn, but please explain for a novice...
First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:
~/.bash_profile
Update for Mavericks
OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configuration for windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.
Lion and Mountain Lion Only
OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:
~/.MacOSX/environment.plist
If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.
Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).
It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.
One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.
Edit: Here are some instructions for creating a plist file.
Open Xcode
Select File -> New -> New File...
Under Mac OS X select Resources
Choose a plist file
Follow the rest of the prompts
To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.
Once the plist file is created you can open it with Xcode to modify it anytime you wish.
Your .profile or .bash_profile are simply files that are present in your "home" folder. If you open a Finder window and click your account name in the Favorites pane, you won't see them. If you open a Terminal window and type ls to list files you still won't see them. However, you can find them by using ls -a in the terminal. Or if you open your favorite text editor (say TextEdit since it comes with OS X) and do File->Open and then press Command+Shift+. and click on your account name (home folder) you will see them as well. If you do not see them, then you can create one in your favorite text editor.
Now, adding environment variables is relatively straightforward and remarkably similar to windows conceptually. In your .profile just add, one per line, the variable name and its value as follows:
export JAVA_HOME=/Library/Java/Home
export JRE_HOME=/Library/Java/Home
etc.
If you are modifying your "PATH" variable, be sure to include the system's default PATH that was already set for you:
export PATH=$PATH:/path/to/my/stuff
Now here is the quirky part, you can either open a new Terminal window to have the new variables take effect, or you will need to type .profile or .bash_profile to reload the file and have the contents be applied to your current Terminal's environment.
You can check that your changes took effect using the "set" command in your Terminal. Just type set (or set | more if you prefer a paginated list) and be sure what you added to the file is there.
As for adding environment variables to GUI apps, that is normally not necessary and I'd like to hear more about what you are specifically trying to do to better give you an answer for it.
Here's a bit more information specifically regarding the PATH variable in Lion OS 10.7.x:
If you need to set the PATH globally, the PATH is built by the system in the following order:
Parsing the contents of the file /private/etc/paths, one path per line
Parsing the contents of the folder /private/etc/paths.d. Each file in that folder can contain multiple paths, one path per line. Load order is determined by the file name first, and then the order of the lines in the file.
A setenv PATH statement in /private/etc/launchd.conf, which will append that path to the path already built in #1 and #2 (you must not use $PATH to reference the PATH variable that has been built so far). But, setting the PATH here is completely unnecessary given the other two options, although this is the place where other global environment variables can be set for all users.
These paths and variables are inherited by all users and applications, so they are truly global -- logging out and in will not reset these paths -- they're built for the system and are created before any user is given the opportunity to login, so changes to these require a system restart to take effect.
BTW, a clean install of OS 10.7.x Lion doesn't have an environment.plist that I can find, so it may work but may also be deprecated.
echo $PATH
it prints current path value
Then do vim ~/.bash_profile and write
export PATH=$PATH:/new/path/to/be/added
here you are appending to the old path, so preserves the old path and adds your new path to it
then do
source ~/.bash_profile
this will execute it and add the path
then again check with
echo $PATH
Unfortunately none of these answers solved the specific problem I had.
Here's a simple solution without having to mess with bash. In my case, it was getting gradle to work (for Android Studio).
Btw, These steps relate to OSX (Mountain Lion 10.8.5)
Open up Terminal.
Run the following command:
sudo nano /etc/paths (or sudo vim /etc/paths for vim)
Go to the bottom of the file, and enter the path you wish to add.
Hit control-x to quit.
Enter 'Y' to save the modified buffer.
Open a new terminal window then type:
echo $PATH
You should see the new path appended to the end of the PATH
I got these details from this post:
http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.UkED3rxPp3Q
I hope that can help someone else
Simplified Explanation
This post/question is kind of old, so I will answer a simplified version for OS X Lion users.
By default, OSX Lion does not have any of the following files:
~/.bashrc
~/.bash_profile
~/.profile
At most, if you've done anything in the terminal you might see ~/.bash_history
What It Means
You must create the file to set your default bash commands (commonly in ~/.bashrc). To do this, use any sort of editor, though it's more simple to do it within the terminal:
%> emacs .profile
[from w/in emacs type:] source ~/.bashrc
[from w/in emacs type:] Ctrl + x Ctrl + s (to save the file)
[from w/in emacs type:] Ctrl + x Ctrl + c (to close emacs)
%> emacs .bashrc
[from w/in emacs type/paste all your bash commands, save, and exit]
The next time you quit and reload the terminal, it should load all your bash preferences. For good measure, it's usually a good idea to separate your commands into useful file names. For instance, from within ~/.bashrc, you should have a source ~/.bash_aliases and put all your alias commands in ~/.bash_aliases.
What worked for me is to create a .launchd.conf with the variables I needed:
setenv FOO barbaz
This file is read by launchd at login. You can add a variable 'on the fly' to the running launchd using:
launchctl setenv FOO barbaz`
In fact, .launchd.cond simply contains launchctl commands.
Variables set this way seem to be present in GUI applications properly.
If you happen to be trying to set your LANG or LC_ variables in this way, and you happen to be using iTerm2, make sure you disable the 'Set locale variables automatically' setting under the Terminal tab of the Profile you're using. That seems to override launchd's environment variables, and in my case was setting a broken LC_CTYPE causing issues on remote servers (which got passed the variable).
(The environment.plist still seems to work on my Lion though. You can use the RCenvironment preference pane to maintain the file instead of manually editing it or required Xcode. Still seems to work on Lion, though it's last update is from the Snow Leopard era. Makes it my personally preferred method.)
Setup your PATH environment variable on Mac OS
Open the Terminal program (this is in your Applications/Utilites folder by default).
Run the following command
touch ~/.bash_profile; open ~/.bash_profile
This will open the file in the your default text editor.
For ANDROID SDK as example :
You need to add the path to your Android SDK platform-tools and tools directory. In my example I will use "/Development/android-sdk-macosx" as the directory the SDK is installed in. Add the following line:
export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools
Save the file and quit the text editor.
Execute your .bash_profile to update your PATH.
source ~/.bash_profile
Now everytime you open the Terminal program you PATH will included the Android SDK.
Adding Path Variables to OS X Lion
This was pretty straight forward and worked for me, in terminal:
$echo "export PATH=$PATH:/path/to/whatever" >> .bash_profile #replace "/path/to/whatever" with the location of what you want to add to your bash profile, i.e: $ echo "export PATH=$PATH:/usr/local/Cellar/nginx/1.0.12/sbin" >> .bash_profile
$. .bash_profile #restart your bash shell
A similar response was here: http://www.mac-forums.com/forums/os-x-operating-system/255324-problems-setting-path-variable-lion.html#post1317516
Open Terminal:
vi ~/.bash_profile
Apply changing to system (no need restart computer):
source ~/.bash_profile
(Also work with macOS Sierra 10.12.1)
I had problem with Eclipse (started as GUI, not from script) on Maverics that it did not take custom PATH. I tried all the methods mentioned above to no avail. Finally I found the simplest working answer based on hints from here:
Go to /Applications/eclipse/Eclipse.app/Contents folder
Edit Info.plist file with text editor (or XCode), add LSEnvironment dictionary for environment variable with full path. Note that it includes also /usr/bin etc:
<dict>
<key>LSEnvironment</key>
<dict>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/dev/android-ndk-r9b</string>
</dict>
<key>CFBundleDisplayName</key>
<string>Eclipse</string>
...
Reload parameters for app with
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app
Restart Eclipse
Let me illustrate you from my personal example in a very redundant way.
First after installing JDK, make sure it's installed.
Sometimes macOS or Linux automatically sets up environment variable for you unlike Windows. But that's not the case always. So let's check it.
The line immediately after echo $JAVA_HOME would be empty if the environment variable is not set. It must be empty in your case.
Now we need to check if we have bash_profile file.
You saw that in my case we already have bash_profile. If not we have to create a bash_profile file.
Create a bash_profile file.
Check again to make sure bash_profile file is there.
Now let's open bash_profile file. macOS opens it using it's default TextEdit program.
This is the file where environment variables are kept. If you have opened a new bash_profile file, it must be empty. In my case, it was already set for python programming language and Anaconda distribution. Now, i need to add environment variable for Java which is just adding the first line. YOU MUST TYPE the first line VERBATIM. JUST the first line. Save and close the TextEdit. Then close the terminal.
Open the terminal again. Let's check if the environment variable is set up.
I took the idiot route.
Added these to the end of /etc/profile
for environment in `find /etc/environments.d -type f`
do
. $environment
done
created a folder /etc/environments
create a file in it called "oracle" or "whatever" and added the stuff I needed set globally to it.
/etc$ cat /etc/environments.d/Oracle
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=/Library/Oracle/instantclient_11_2
export SQLPATH=/Library/Oracle/instantclient_11_2
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export TNS_ADMIN=/Library/Oracle/instantclient_11_2/network/admin
It is recommended to check default terminal shell before setting any environment variables, via following commands:
$ echo $SHELL
/bin/zsh
If your default terminal is /bin/zsh (Z Shell) like in my case (Personally prefer Z Shell), then you should set these environment variable in ~/.zshenv file with following contents (In this example, setting JAVA_HOME environment variable, but same applies to others):
export JAVA_HOME="$(/usr/libexec/java_home)"
Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.
More detail, which may perhaps be helpful to someone:
Due to my own explorations, I now know how to set environment variables in 7 of 8 different ways. I was trying to get an envar through to an application I'm developing under Xcode. I set "tracer" envars using these different methods to tell me which ones get it into the scope of my application. From the below, you can see that editing the "scheme" in Xcode to add arguments works, as does "putenv". What didn't set it in that scope: ~/.MACOS/environment.plist, app-specific plist, .profile, and adding a build phase to run a custom script (I found another way in Xcode [at least] to set one but forgot what I called the tracer and can't find it now; maybe it's on another machine....)
GPU_DUMP_DEVICE_KERNEL is 3
GPU_DUMP_TRK_ENVPLIST is (null)
GPU_DUMP_TRK_APPPLIST is (null)
GPU_DUMP_TRK_DOTPROFILE is (null)
GPU_DUMP_TRK_RUNSCRIPT is (null)
GPU_DUMP_TRK_SCHARGS is 1
GPU_DUMP_TRK_PUTENV is 1
... on the other hand, if I go into Terminal and say "set", it seems the only one it gets is the one from .profile (I would have thought it would pick up environment.plist also, and I'm sure once I did see a second tracer envar in Terminal, so something's probably gone wonky since then. Long day....)
Step1: open ~/.bash_profile
Now a text editor opens:
Step2: variable name should be in capitals. in this example variable is NODE_ENV
Step3: export NODE_ENV=development
Save it and close.
Restart your system.
Done.
To check env variable: open terminal and type
echo $NODE_ENV
Related
Remove path from $PATH macOS Mojave
I am trying to set up my new MacBook, and installed python 3.7, however realised that I would rather use python 3.6. I uninstalled python 3.7, however the python path is still in my $PATH alongside the path for python 3.6. The questions which I have seen on this topic seem to give answers which involve using export PATH which only changes the path for the current session, or say that the measure which needs to be taken depends on your system configuration and version of macOS echo $PATH gives: /Library/Frameworks/Python.framework/Versions/3.6/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin: I have already deleted the ../3.7 which is being pointed to, so would like to only have the 3.6 path in $PATH
You may use export PATH=/Library/Frameworks/Python.framework/Versions/3.6/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:, but write it in the .bash_profile or the .bashrc in your user. That is, if you are using bash. Maybe you will find already a definition of PATH in there, so you can just change it.
Go to your home directory, cd ~. Open .bash_profile file. Like so, open .bash_profile Make changes to the path variable in the end of the file. Save and exit. Now close the current terminal and open new one. The changes will sustain. Also for using python of your wish you can alias it in .bash_profile (obviously after setting PATH variable). Like, alias python="python3.6"
Impossible to set python.exe to *.py scripts on Win7
i've installed py 2.7 (64bit) on my PC with Win7 (64bit) without problem but I'm not able to run *.py scripts via DOS shell without declare python full path. Let me better explain : If I type D:\ myscript.py it doesn't work. The script is open with wordpad If I type D:\ C:\Python27 myscript.py it works and run correctly I try to change the default application software for *.py file via Win7 GUI ( control pannel etc etc) but without success. Python is not present in the list of available sw and in any case also with the manual set I'm not able to associate python.exe at *.py files. I've checked in my environment variables but I've not found problem (python path is declared in Path = C:\Python27\;C:\Python27\Scripts). I've tried also to modify HKEY_CLASSES_ROOT->Applications->python.exe->shell->open->command : old register value "C:\Python27\python.exe" "%1" new register value "C:\Python27\python.exe" "%1" %* without success. Any suggestion? Thanks
Here is another check to make, which helped me figure out what was going on. I switched from the 32bit Anaconda to the 64bit version. I deinstalled, downloaded then reinstalled, but several things didn't get cleaned up properly (quick launch stuff, and some registry keys). The problem on my side was that the default installation path changed, from C:\Anaconda to C:\Anaconda2. I first tried the assoc and ftype tricks, everything was fine there. However, the HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command registry key was pointing to the old Anaconda path. As soon as I fixed this, python.exe showed up when I tried associating with "Open with" and everything went back to normal. I also added the %* at the end in the registry key.
You could try to use the ASSOCIATE command in CMD: ASSOCIATE .py C:\PathTo\python.exe More information at http://ss64.com/nt/associate.html
#slv 's answer is good and helped me a bit with solving this problem. Anyhow, since I had previous installations of Python before this error occured for me, I might have to add something to this. One of the main problems hereby was that the directory of my python-installation changed. So, I opened regedit.exe and followed these to steps: I searched the entire registry for .py, .pyw, .pyx and .pyc (hopefully I did not forget to mention any here). Then, I radically deleted all occurrences I could find. I searched the entire registry for my old python-installation-path (e.g. C:\Users\Desktop\Anaconda3). Then I replaced this path with my new installation path (e.g. C:\Users\Desktop\Miniconda3). Thereby, I also came across and replaced HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command which #slv mentioned. Afterwards, it was possible again to connect a .py-file from the Open with...-menu with my python.exe.
The *.py file is a source code file. If you set up your system environment correctly, you need to run python myscript.py the following answer is related to your question Making Python scripts run on Windows without specifying ".py" extension
Do you know that when you change the default application of a file, you are able to browse for the application? You can click on the "browse" button (as shown in the red rectangle), then choose C:\Python27\python.exe. Also remember to click on the "Always use this program to open this program" checkbox, which is shown in the green rectangle, so that win7 knows how to open this file the next time you ask it to open *.py file. Then I believe you'll be able to run myScript.py simply by typing "myScript.py" in the correct dirctory in DOS shell. After doing all the above steps, be sure to reopen a new command shell.
How to import modules from alternate locations when using Python IDLE?
I've been trying to figure this out for more than 2 days, screening the internet and the tutorial, but yet I don't have solved my problem. I'm a real newb and don't yet really know what I'm doing.. Software I use: Mac OS X 10.6 Python v3.2.2 Interactive interpreter (IDLE) Problem: IDLE's default directory is /Users/ME/Documents/. Files with the extention .py can only be opened when located in this directory. However, I made a folder where I would like to save all the .py files etc that have to do with this software. Currently, IDLE cannot load .py files from the chosen directory by me. What I did first was I added to IDLE: import sys. sys.path.append('Users/Mydir/') sys.path However, in an already existing thread from 2010 I read sys.path is for the Interpreter ONLY, and that if I am to change this I need to modify the PYTHONPATH environment variable: PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH" export PYTHONPATH However, I'm confused how to use this and cannot find answers to my following questions: 1) PYTHONPATH (.py?) is already existing on my computer when I installed the program? If YES, where is it? I cannot find it anywhere. If NO, I need to create one. But where and what should be the content so that IDLE can load files from a non-default directory? Should it contain only the words in bold? I hope I made my problem clear. Cheers
It's not totally clear to me what you mean by load. That could mean Open and Close files in the IDLE editor. Or it could mean being able to use the Python import statement to load existing Python modules from other files. I'll assume the latter, that by load you mean import. There are two general ways to launch IDLE on Mac OS X. One is from the command line of a terminal session; if you installed Python 3.2 using the python.org installers, by default typing /usr/local/bin/idle3.2 will work. The other way is by launching IDLE.app from /Applications/Python 3.2, i.e. by double-clicking its icon. Because you say the default directory for files is your Documents folder, I'm assuming you are using the second method because IDLE.app sets Documents as its current working directory, which becomes the default directory for *Open*s and *Save*s and is automatically added as the first directory on Python's sys.path, the list of directories that Python uses to search for modules when importing. If you want to add other directories to sys.path, as you've noted you can use the PYTHONPATH environment variable to do so. The standard way to do this is to add an export PYTHONPATH=... definition to a shell startup script, like .bash_profile. However, if you use IDLE.app, no shell is involved so commands in .bash_profile have no effect. While there are ways to modify the environment variables for OS X GUI apps, in this case, a simpler solution is to use the other method to invoke IDLE, from the command line of a shell session, using either /usr/local/bin/idle3.2 or, if you've run the Update Shell Profile command in the /Applications/Python 3.2 folder (and opened a new terminal session), just idle3. Then, a PYTHONPATH environment variable you set up will be inherited by that IDLE. BTW, there is no direct way to modify the initial current working directory of IDLE.app from Documents other than modifying the code in IDLE. If you start IDLE from a command line, it inherits the current working directory of the shell. [UPDATE] But rather than fooling around with defining PYTHONPATH, here is another even simpler, and probably better, approach that should work with either IDLE.app or the command line idle. It takes advantage of Python path configuration (.pth) files and user site-package directories. Assuming you are using a standard Python framework build of 3.2 (like from a python.org installer) on Mac OS X, create a path file for the directory you want to permanently add to sys.path. In a terminal session: mkdir -p ~/Library/Python/3.2/lib/python/site-packages cd ~/Library/Python/3.2/lib/python/site-packages cat >my_paths.pth <<EOF /Users/YOUR_USER_NAME/path/to/your_additional_python_directory_1 /Users/YOUR_USER_NAME/path/to/your_additional_python_directory_2 EOF Now, whenever you run that Python 3.2 or IDLE under your user name, the directories you have added to the .pth file will automatically be added to sys.path. BTW, the exact path location of the user site-packages directory for versions of Python earlier than 3.2 or 2.7 may be slightly different. Also, on other Unix-y systems, the default location for the user site-package directory is ~/.local/lib/python3.2/site-packages.
PYTHONPATH is an environment variable (see here and here). I don't have a Mac, but from the threads I have linked to you would type something like launchctl setenv PYTHONPATH=/Me/Documents/mydir:$PYTHONPATH on the command line to allow you to run Python scripts from /Me/Documents/mydir. Alternatively, put this line in a file called .bashrc in your home directory (~) and this path will be set each time each time you open a terminal. See here for a short introduction to .bashrc and other .bash* files. Hope that helps. EDIT See also this question.
Mac Swampy (Python learning module) install
I thought I would teach my kids programming and am using "Think Python" by Downey. The book is great except when he gets to installing "Swampy" a Turtle-like learning module. I've spent a few hours trying to figure it out--now I need help. Can anyone offer a clear, step-by-step set of instructions on how to install Swampy? Please assume zero prior knowledge of Unix, etc. I'm on OS X (10.6.6). Python is running fine. Tkinter is fine. Here are the overall installation instructions offered at the author's site: http://www.greenteapress.com/thinkpython/swampy/install.html I am stuck at the "Add Swampy to your search path" section. Here are the additional instructions the author points to regarding the search path: http://docs.python.org/install/index.html#inst-search-path Probably terribly obvious buy tough stuff for an intro text! Update A few details for the record for anyone who comes across this later: My installation does not have a file or folder "python2" so the pathname "/home/downey/swampy-2.0/python2" might not work. I just dropped the "python2" and all was well. Had a bit of a problem finding the bashpc or bash_profile file. Didn't seem to exist even when looking for hidden files. So I just created a new one. All seems well. Thanks again for your help--I would never have figured it out :) I'm surprised there isn't some sort of installer or script to handle this given the audience for the book (newbies like me). :)
Open the terminal and navigate to the folder where you unzipped Swampy. Find the full directory path of that location by typing pwd. Let's say that location is /home/downey/swampy-2.0/python2. Now in the terminal issue this command export PYHTONPATH="/home/downey/swampy-2.0/python2". This will set the PYTHONPATH environment variable. If that works fine, you need to make sure that this always runs when you open the terminal. For that you need to edit a file called .bashrc on your computer. In the terminal, try nano ~/.bashrc. That will open a simple text editor. In that file add the same line export PYHTONPATH="/home/downey/swampy-2.0/python2". Type Ctrl+X to close and make sure you save. Now instead of always setting the environment variable manually, you can just do source ~/.bashrc and that will execute the command for you. Note that every time you exit and reopen the terminal, you need to run source ~/.bashrc for the PYTHONPATH to be set.If you don’t want to run source ~/.bashrc every time you open the terminal, you can configure the Terminal to automatically run this command at startup (i.e. when the terminal window is opened). To do that – Go to Terminal –> Preferences –> Settings –> Shell. Check ‘Run Command’ and enter source ~/.bashrc. NOTE: These settings are only need to be applied to each profile separately. For example if you applied these settings to the Basic profile, they won’t be applicable to Grass profile. Hope this helps!
An easy way to add variables to your path is to add them to your .bash_profile, which is located in your home directory. This file loads every time you open up terminal. It is a hidden file, so you will not be able to view it (by default) in Finder. Run this command after opening a new terminal window: open -a TextEdit .bash_profile This will open the profile up in a new TextEdit window. Add this & save: PYTHONPATH=${PYTHONPATH}:/home/downey/swampy-2.0/python2 export PYTHONPATH Assuming your Swampy download path is the same from the Web Site (/home/downey/swampy-2.0/python2), the next time you run terminal, the path should now be included.
Permanently add a directory to PYTHONPATH?
Whenever I use sys.path.append, the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I permanently add a directory to PYTHONPATH?
If you're using bash (on a Mac or GNU/Linux distro), add this to your ~/.bashrc export PYTHONPATH="${PYTHONPATH}:/my/other/path"
You need to add your new directory to the environment variable PYTHONPATH, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose. superuser.com may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.
Instead of manipulating PYTHONPATH you can also create a path configuration file. First find out in which directory Python searches for this information: python -m site --user-site For some reason this doesn't seem to work in Python 2.7. There you can use: python -c 'import site; site._script()' --user-site Then create a .pth file in that directory containing the path you want to add (create the directory if it doesn't exist). For example: # find directory SITEDIR=$(python -m site --user-site) # create if it doesn't exist mkdir -p "$SITEDIR" # create new .pth file with our path echo "$HOME/foo/bar" > "$SITEDIR/somelib.pth"
This works on Windows On Windows, with Python 2.7 go to the Python setup folder. Open Lib/site-packages. Add an example.pth empty file to this folder. Add the required path to the file, one per each line. Then you'll be able to see all modules within those paths from your scripts.
In case anyone is still confused - if you are on a Mac, do the following: Open up Terminal Type open .bash_profile In the text file that pops up, add this line at the end: export PYTHONPATH=$PYTHONPATH:foo/bar Save the file, restart the Terminal, and you're done
You could add the path via your pythonrc file, which defaults to ~/.pythonrc on linux. ie. import sys sys.path.append('/path/to/dir') You could also set the PYTHONPATH environment variable, in a global rc file, such ~/.profile on mac or linux, or via Control Panel -> System -> Advanced tab -> Environment Variables on windows.
To give a bit more explanation, Python will automatically construct its search paths (as mentioned above and here) using the site.py script (typically located in sys.prefix + lib/python<version>/site-packages as well as lib/site-python). One can obtain the value of sys.prefix: python -c 'import sys; print(sys.prefix)' The site.py script then adds a number of directories, dependent upon the platform, such as /usr/{lib,share}/python<version>/dist-packages, /usr/local/lib/python<version>/dist-packages to the search path and also searches these paths for <package>.pth config files which contain specific additional search paths. For example easy-install maintains its collection of installed packages which are added to a system specific file e.g on Ubuntu it's /usr/local/lib/python2.7/dist-packages/easy-install.pth. On a typical system there are a bunch of these .pth files around which can explain some unexpected paths in sys.path: python -c 'import sys; print(sys.path)' So one can create a .pth file and put in any of these directories (including the sitedir as mentioned above). This seems to be the way most packages get added to the sys.path as opposed to using the PYTHONPATH. Note: On OSX there's a special additional search path added by site.py for 'framework builds' (but seems to work for normal command line use of python): /Library/Python/<version>/site-packages (e.g. for Python2.7: /Library/Python/2.7/site-packages/) which is where 3rd party packages are supposed to be installed (see the README in that dir). So one can add a path configuration file in there containing additional search paths e.g. create a file called /Library/Python/2.7/site-packages/pip-usr-local.pth which contains /usr/local/lib/python2.7/site-packages/ and then the system python will add that search path.
On MacOS, Instead of giving path to a specific library. Giving full path to the root project folder in ~/.bash_profile made my day, for example: export PYTHONPATH="${PYTHONPATH}:/Users/<myuser>/project_root_folder_path" after this do: source ~/.bash_profile
On linux you can create a symbolic link from your package to a directory of the PYTHONPATH without having to deal with the environment variables. Something like: ln -s /your/path /usr/lib/pymodules/python2.7/
For me it worked when I changed the .bash_profile file. Just changing .bashrc file worked only till I restarted the shell. For python 2.7 it should look like: export PYTHONPATH="$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python" at the end of the .bash_profile file.
Adding export PYTHONPATH="${PYTHONPATH}:/my/other/path" to the ~/.bashrc might not work if PYTHONPATH does not currently exist (because of the :). export PYTHONPATH="/my/other/path1" export PYTHONPATH="${PYTHONPATH}:/my/other/path2" Adding the above to my ~/.bashrc did the trick for me on Ubuntu 16.04
This is an update to this thread which has some old answers. For those using MAC-OS Catalina or some newer (>= 10.15), it was introduced a new Terminal named zsh (a substitute to the old bash). I had some problems with the answers above due to this change, and I somewhat did a workaround by creating the file ~/.zshrc and pasting the file directory to the $PATH and $PYTHONPATH So, first I did: nano ~/.zshrc When the editor opened I pasted the following content: export PATH="${PATH}:/Users/caio.hc.oliveira/Library/Python/3.7/bin" export PYTHONPATH="${PYTHONPATH}:/Users/caio.hc.oliveira/Library/Python/3.7/bin" saved it, and restarted the terminal. IMPORTANT: The path above is set to my computer's path, you would have to adapt it to your python.
The script below works on all platforms as it's pure Python. It makes use of the pathlib Path, documented here https://docs.python.org/3/library/pathlib.html, to make it work cross-platform. You run it once, restart the kernel and that's it. Inspired by https://medium.com/#arnaud.bertrand/modifying-python-s-search-path-with-pth-files-2a41a4143574. In order to run it it requires administrator privileges since you modify some system files. from pathlib import Path to_add=Path(path_of_directory_to_add) from sys import path if str(to_add) not in path: minLen=999999 for index,directory in enumerate(path): if 'site-packages' in directory and len(directory)<=minLen: minLen=len(directory) stpi=index pathSitePckgs=Path(path[stpi]) with open(str(pathSitePckgs/'current_machine_paths.pth'),'w') as pth_file: pth_file.write(str(to_add))
Just to add on awesomo's answer, you can also add that line into your ~/.bash_profile or ~/.profile
The add a new path to PYTHONPATH is doing in manually by: adding the path to your ~/.bashrc profile, in terminal by: vim ~/.bashrc paste the following to your profile export PYTHONPATH="${PYTHONPATH}:/User/johndoe/pythonModule" then, make sure to source your bashrc profile when ever you run your code in terminal: source ~/.bashrc Hope this helps.
I added permanently in Windows Vista, Python 3.5 System > Control Panel > Advanced system settings > Advanced (tap) Environment Variables > System variables > (if you don't see PYTHONPATH in Variable column) (click) New > Variable name: PYTHONPATH > Variable value: Please, write the directory in the Variable value. It is details of Blue Peppers' answer.
Fix Python Path issues when you switch from bash to zsh I ran into Python Path problems when I switched to zsh from bash. The solution was simple, but I failed to notice. Pip was showing me, that the scripts blah blah or package blah blah is installed in ~/.local/bin which is not in path. After reading some solutions to this question, I opened my .zshrc to find that the solution already existed. I had to simply uncomment a line: Take a look
I found a solution to do this in a anaconda environment here: https://datacomy.com/python/anaconda/add_folder_to_path/ Just: conda develop /your_path
In Python 3.6.4 you can persist sys.path across python sessions like this: import sys import os print(str(sys.path)) dir_path = os.path.dirname(os.path.realpath(__file__)) print(f"current working dir: {dir_path}") root_dir = dir_path.replace("/util", '', 1) print(f"root dir: {root_dir}") sys.path.insert(0, root_dir) print(str(sys.path)) I strongly suggest you use virtualenv and virtualenvwrapper otherwise you will clutter your path
Inspired by andrei-deusteanu answer, here is my version. This allows you to create a number of additional paths in your site-packages directory. import os # Add paths here. Then Run this block of code once and restart kernel. Paths should now be set. paths_of_directories_to_add = [r'C:\GIT\project1', r'C:\GIT\project2', r'C:\GIT\project3'] # Find your site-packages directory pathSitePckgs = os.path.join(os.path.dirname(os.__file__), 'site-packages') # Write a .pth file in your site-packages directory pthFile = os.path.join(pathSitePckgs,'current_machine_paths.pth') with open(pthFile,'w') as pth_file: pth_file.write('\n'.join(paths_of_directories_to_add)) print(pthFile)
After multiple bashing into wall. Finally resolved, in my CentOS 8 the pip3 was old, which was showing error to install the recent packages. Now, I had downloaded the Python source package, which is Python-3.10.4 and installed the usual way, however the post-installation check generated errors in bash. And I could not remove the existing Python, because that would break the CentOS desktop features. Solution: For building ./configure //don't not add --prefix=/usr, which you need to set proper care make -j8 sudo make install Now, as you have multiple Python installed, you can set alias python=python3 And for setting PYTHONPATH export PYTHONPATH="/usr/local/bin/python3.10:/usr/local/lib/python3.10/lib-dynload:/usr/local/lib/python3.10/site-packages" Don't add PYTHONHOME
For those who (like me) don't want to get too deeply involved in Python file management (which seems hopelessly overcomplicated), creating a .pth file works perfectly on my Windows 11 laptop (I'm using Visual Studio Code in Windows). So just go to the folder for your virtual environment site packages - there's mine: Create a text file with a .pth extension - I called mine wheal.pth: Add paths to it: The best thing about this in VS Code is that import statements recognise this path (I had to exit VS Code and go back in), so now more typing # type: ignore to suppress linting warning messages!
on Mac : user#terminal$ env PYTHONPATH=module_path python3 >>> import sys >>> sys.path ['module_path', 'plus_other_python3_paths',...]
Shortest path between A <-> B is a straight line; import sys if not 'NEW_PATH' in sys.path: sys.path += ['NEW_PATH']