Fixing PATH for Python libraries using Bash - python

I am attempting to install some Python libraries by executing variations of the following command in Bash:
pip install --user -U numpy
I installed Python3 using Homebrew.
I then get variations of the following message each time:
WARNING: The scripts f2py, f2py3 and f2py3.7 are installed in '/Users/x/Library/Python/3.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
How can I fix this issue to avoid problems in the future?

The error message is telling you to add Python 3 to your path.
To do that, use a text editor to open /Users/<you>/.profile, and as the very last line add:
export PATH=/Users/<you>/Library/Python/3.8/bin:$PATH
Then you'll need to launch a new Terminal window for those settings to take effect. (you could make it take effect in your current shell by entering the line directly into your shell)
[Edit: now that macOS 12.3 has removed all versions of Python, Homebrew is the easiest way to install Python. Fortunately things are simpler because there's only one version of Python on your system and you won't need to override the system's version (because there isn't one any longer).]

Update: As of python 3.8, the following path should be used:
export PATH=/Library/Frameworks/Python.framework/Versions/3.8/bin:$PATH
If you're using bash, you can store this in your /.bashrc
If you're using zsh, you can store this in your /.zshrc

You have to update the seeking path location, to needed bin folder, in your .zshrc, .bashrc etc.
Example
For /Library/Python/3.8/bin,
you can prepend the variable $HOME, and use with the needed path:
export PATH="$HOME/Library/Python/3.8/bin:$PATH"
Epilogue
In current Terminal tab, you have to reload your shell, with the config, by . ~/.zshrc, or .bashrc etc

Related

Error Installing OpenCV with Python on OS X

I have been trying to install Open CV 3 on my mac using this tutorial but I cannot get past step three.
So after I do
brew install python
I do
nano ~/.bash_profile
And the at the bottom of the script I paste
# Homebrew
export PATH=/usr/local/bin:$PATH
After that I reload the file like this
source ~/.bash_profile
Finally I check the python like this
which python
And it prints
/usr/bin/python
instead of
/usr/local/bin/python
I have also tried edited the file in TextEdit but it has the same result.
Am I doing something wrong or is this just a bad tutorial?
Thank You in Advance!
Edit:
# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
##
# Your previous /Users/UserName/.bash_profile file was backed up as /Users/UserName/.bash_profile.macports-saved_2016-07-26_at_12:50:19
##
# MacPorts Installer addition on 2016-07-26_at_12:50:19: adding an appropriate PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.
# Homebrew
export PATH=/usr/local/bin:$PATH
pydoc3.5
python3
python3-32
python3-config
python3.5
python3.5-32
python3.5-config
python3.5m
python3.5m-config
Is there a
/usr/local/Cellar/python/2.7.12/
directory? (Version number might differ.)
Is there a
/usr/local/bin/python
file?
If the Cellar directory is present, but the file isn't, then Homebrew decided to be careful and not put Python in /usr/local/bin/ immediately.
You could manually do
brew link python
and see if there's now a
/usr/local/bin/python
file.
In your case, it appears you have some files related to Python (they might be from a Python 3 installation, can't tell), such as 2to3. You can safely overwrite them, since Python 2 also has this.
Thus:
brew link --overwrite python
is fine.
Note:
Specific Python versions will always exist as python2.7, python3.5 etc (including the full path as necessary). Thus, even overwriting the python executable is safe (provided it's not the system one in /usr/bin): you should then simply be explicit which python executable to use.
Also, when using a tool like pip, you can make sure you're using the correct version by running it e.g. as
/usr/local/bin/pythnon2.7 -m pip <...>
or whatever python executable you want to install things for.
Okay so one brute force solution could be this one https://stackoverflow.com/a/9821036/128517
But maybe you could check the value of your $PATH after source ~/.bash_profile
typing
> echo $PATH
and see if /usr/local/bin is indeed at the beginning.
if it's not, you might need to check if there's another export before yours or maybe you need to edit .profile instead.

Using pdb in emacs with a miniconda install

I am trying to use pdb as a python debugger in emacs
I'm getting the import error issues, as in:
https://emacs.stackexchange.com/questions/18190/missing-modules-when-debugging-python-code-in-emacs
How to specifiy path when using pdb in emacs?
the suggested solution, i.e.
(setenv "PYTHONPATH" "lib")
in .emacs or emacs.d/init.el
This seems very unsatisfactory as you are effectively duplicating the pythonpath data which will trip you up at some point when you update one but not the other.
How can one get the pythonpath as used by pdb to be exactly the same as that is used say if I open an ipython shell.
In other words I want pdb to reflect the path locations that get automatically loaded from my .bashrc and miniconda installation when I open ipython
also, how does ipython know which miniconda dirs to pull into the pythonpath? which file is that specified in?
Find out where your miniconda install is, for me it was:
~/miniconda2
Ensure that your .bashrc PATH is picking up the miniconda python install before any of the default linux installations, i.e. its looking in ~/miniconda2/bin prior to /usr/bin/
Dired into ~/miniconda2/lib/python2.7
'S' to create a soft symlink to pdb.py, place the link in ~/miniconda2/bin and call it pdb2.7
Dired into ~/miniconda2/bin
'S' to create a soft symlink to pdb2.7, place link in same dir and call it pdb
Now when you run M-x pdb to invoke the python debugger it should pick up the correct version and not complain of any missing modules
Prior to this it was picking up pdb from /usr/bin, hence all the module import failures. These steps just copy the set of symlinks found in that dir but using the miniconda equivalent files and dirs

Virtual Env on Mac with python 3.4

I am trying to get a django project up and running, and I have django running, but I am having trouble with python's virtualenv.
here is my error (on terminal open this shows up)
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/2.7/bin/python and that PATH is
set properly.
Here is what My .bash profile looks like:
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
export WORKON_HOME=$HOME/.virtualenvs
source /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
I am pretty new to command line stuff and virtual environments, so I might not know some "obvious" things I am doing wrong. Thanks.
First some basics of the command-line shell:
PATH is an environment variable that contains a list of filesystem directories. When you type a command such as ls, python or virtualenvwrapper.sh your shell will search each directory starting from the first one listed. To see your current PATH type:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
To see what a given command is going to resolve to the location of the program that is going to be run, use the which command:
$ which ls
/bin/ls
Now in your example you are first adding a Python 3.4 location to your PATH and then a Python 2.7 location. The latter location is going to be first on your PATH. So all your Python related commands are first going to try and run Python 2.7, if a command isn't found there, it next searches in your Python 3.4 installation.
You appear to have installed virtualenvwrapper for Python 3.4, but when you run virtualenvwrapper.sh from the line:
source /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper.sh
You are explicitly running the virtualenvwrapper.sh installed for Python 3.4. This runs a python command where your Python 2.7 is run, which does not appear to have virtualenvwrapper installed, as shown by this error message:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
If you tried removing the Python 2.7 location addition to your PATH and it didn't work, you have to remember that changes to your .bash_profile are not automatically reflected in your terminal. You have to create a new terminal session and that new session will read your new .bash_profile.
If you are trying to get the command virtualenv to resolve to two different Pythons, it's simply not possible. It will always resolve to one specific Python based on the ordering of the locations on your PATH. VirtualEnv does install alternate versions of the command with the version of the Python built into the name. Use the commands virtualenv-3.4 and virtualenv-2.7 to create virtual environments for each different Python.
The key to all of this is remembering that PATH is used to resolve to an absolute path location of a program this is run. Use the echo $PATH and which commands to help you understand how that final path is being resolved.
It may seem tedious, but typing out the full absolute path is always going to side-step the magic of PATH resolution:
$ /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv
Try using smaller steps on your way to manipulating the path. Use just virutalenv-3.4 to make a Python 3.4 virtual environment and only bring in virtualenvwrapper as you need it and after your understanding of shell environments is more solid.
Finally, even pro's can get tripped up by playing games manipulating the path. You are ultimately trying to take a bunch of different application locations and flatten them all into one namespace. At some point conflicts can become unresolveable. A Python installation tool such as Buildout has it's own learning curve, but it hard-codes the absolute path to the python of each python script that it installs. Hard-coding absolute paths is the only way to deal with extreme corner cases such as having two builds of Python 3.4 side-by-side where you have two scripts which each need to run on two different builds of the same Python. Hard-coding absolute locations is also desirable in production environments, because then your application is immune to any changes to the bash shell. If you forget about a required ordering of your PATH, or another sysadmin tinkers with the PATH, you won't find your application breaking unexpectedly.

setting/changing PYTHONPATH in OS Mavericks

Ok, I hate to start another of seemingly hundreds of other threads about this, but how do I set or tweak the PYTHONPATH? I'm very new to Mac and the only other time I've successfully done this was on Windows using Rapid Enviroment Editor. This looks like a whole different beast. I tried installing Django and it failed to do it for 2.7, while I need 3.4, so the current path must be for 2.7. Running which python in Terminal shows the /usr/bin/python directory, which I then can't cd into, let alone find by browsing. I see that my Python 3.4 directory has the Update Shell Profile file, but it has a lot of limitations. I also see other threads mention PYTHONPATH commands in IDLE and creating one of the bash profile type files for the Terminal. How can I set this and not worry about it anymore until I need to run a different version of Python? I'm on Mac 10.9.2.
"Explain like I'm five".
If you want to install packages for python 3.4 use: pip3 install django
When installing for python 2.7 just use: pip install django
To use python 3.4 type python3 in your shell.
To see where all installations of python are use: which -a python
Depending on how you installed the new versions of python you will see output like:
/usr/local/bin/python
/usr/bin/python
If you wanted to use the python in /usr/local/bin/python you can edit your .bashrc file and add export path="/usr/local/bin:$path".
Save, then type source .bashrc in your shell and when you type which python it will show something like /usr/local/bin/python
Don't screw around too much with different versions of python, you will end up causing yourself a lot of problems.
You should not have to change your PYTHONPATH, just specify which python or pip version you want to use and that will most likely be all you need to do.
To update PYTHONPATH you can run from the terminal:
export PYTHONPATH=$PYTHONPATH:/desired/path/to/add
Then to check the updated PYTHONPATH you can run:
echo $PYTHONPATH
I'm not sure if this completely answers your question, but this is one way to make sure modules are visible to python when you import them.

getting error message when trying to import a newly installed python package

I just downloaded a python package and installed it on a Linux box using the following command:
python setup.py install --prefix=/home/ubuntu/dev/git/nx
That is, I did not use the default install directory. I then appended the existing PYTHONPATH variable in ~/.bashrc as follows:
export PYTHONPATH=/some/previous/path:/home/ubuntu/dev/git/nx
But when I run import of the package in python, I get a ImportError: No module named error.
Am I specifying the wrong path in PYTHONPATH? If so, how do I figure out which path to put there? Or is there a different error?
Thx
If you put it in bashrc you will either need to source ~/.bashrc or log in again. Depending on your distro, bashrc might not be the right place to put it. You're using Ubuntu, so it will work.
It's also generally good practice to include the current variable when exporting a path-type variable.
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/dev/git/nx
You can verify that your path is correct with echo $PYTHONPATH. You can also run the above command to see if it will fix it (albeit temporarily and only in your current shell).
Ran
sudo python setup.py install
Didn't have to mess with PYTHONPATH. Working great!

Categories