/usr/bin/python vs /opt/local/bin/python2.7 on OS X - python

Can you shed some light on the interaction between the Python interpreter distributed with OS X and the one that can be installed through MacPorts?
While installing networkx and matplotlib I am having difficulties with the interaction of /usr/bin/python and /opt/local/bin/python2.7. (The latter is itself a soft pointer to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7)
How can I be certain which Python, pip, and Python libraries I am using at any one time?
More importantly, it appears that installing matplotlib is not possible on Lion. It fails with Requires numpy 1.6 or later to build. (Found 1.5.1). If I upgrade by running sudo pip install --upgrade numpy, it does not help. Subsequently attempting to install matplotlib (sudo /usr/local/bin/pip install matplotlib) still fails with the same (Requires numpy 1.6...) message. How can I install matplotlib?

Points to keep in mind about Python
If a script foobar.py starts with #!/usr/bin/env python, then you will always get the OS X Python. That's the case even though MacPorts puts /opt/local/bin ahead of /usr/bin in your path. The reason is that MacPorts uses the name python2.7. If you want to use env and yet use MacPorts Python, you have to write #!/usr/bin/env python2.7.
If a script foobar.py starts explicitly with #!/usr/bin/python or with #!/opt/local/bin/python2.7, then the corresponding Python interpreter will be used.
What to keep in mind about pip
To install pip for /usr/bin/python, you need to run sudo /usr/bin/easy_install pip. You then call pip (which will not be installed by easy_install in /usr/bin/pip, but rather in /usr/local/bin/pip)
To install pip for /opt/local/bin/python2.7, you need to run sudo port install py27-pip. You would then call pip-2.7. You will get the pip in /opt/local/bin. Be careful, because if you type pip2.7 you will get /usr/local/bin/pip2.7 (the OS X pip).
Installing networkx and matplotlib
To install networkx for the OS X Python you would run sudo /usr/local/bin/pip install networkx. I don't know how to install matplotlib on OS X Lion. It may be that OS X has to stick to numpy 1.5.1 because it uses it internally.
To install networkx and matplotlib for MacPorts-Python, call sudo pip-2.7 install networkx and sudo pip-2.7 install matplotlib. matplotlib installs with a lot of warnings, but it passes.

May I also suggest using Continuum Analytics "anaconda" distribution. One benefit in doing so would be that you won't then need to modify he standard OS X python environment.

Related

mac pre-bundled python and newly-installed one

I'm trying to install python and its library in bash, using brew and pip.
When I type command
pip list
it seems that numpy(1.8.0rc1), matplotlib(1.3.1), scipy(0.13.0b1) are all installed. However, when I type ipython command and enter the interactive python interface,
import numpy
works fine, but
import matplotlib
import scipy
run into error saying that "ModuleNotFoundError".
I think it is because OS has its own pre-bundled python and pip list command shows what libraries are installed for the pre-bundled one. But ipython command enters into the newly-installed python where those two libraries are not installed.
So could any one talk about the two pythons, and how could I install the library to the correct position and enter the proper python.
I've tried brew, reinstall, pip, sudo and they didn't work quite well.
BTW, when type print(sys.path) in ipython, it gives
['', '/usr/local/Cellar/ipython/6.2.1/libexec/bin', '/usr/local/Cellar/ipython/6.2.1/libexec/lib/python3.6/site-packages', '/usr/local/Cellar/ipython/6.2.1/libexec/vendor/lib/python3.6/site-packages', '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages', '/usr/local/Cellar/numpy/1.13.3/libexec/nose/lib/python3.6/site-packages', '/usr/local/Cellar/ipython/6.2.1/libexec/lib/python3.6/site-packages/IPython/extensions', '/Users/bazinga/.ipython']
The reason behind this is that you now have two Python installations, one being the system one:
$ python -V
Python 2.7.13
and the other one installed via Homebrew ("brewed" Python):
$ python3 -V
Python 3.6.3
When you issue pip list, you are listing packages installed for the system Python. You can check what Python installation does pip belong by issuing pip -V. The package manager for the brewed Python is pip3, check this: pip3 -V. All the commands valid for pip will also work with pip3, for example list packages by issuing pip3 list etc.
The ipython Installation uses the brewed Python, so in order to install packages so be accessible by ipython, use pip3:
$ pip3 install --user numpy matplotlib scipy

How to install Numpy & pip3 for python3.x when they were installed in python2.7? Using Conda?

I want to write program in python3 (3.5), hence I installed python3 next to the pre-installed python2 (2.7) on Mac OS X El Captian.
Since my terminal runs python2.7 by default and Numpy is already installed for it, I put alias python=python3 and expected to be able to install Numpy for python3. when I type pip install numpy. This was the generated message:
Requirement already satisfied (use --upgrade to upgrade): numpy in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
I also noticed that I have no pip3 even though I am using python3: python --version returned Python 3.5.2, but pip3 install numpy got me -bash: pip3: command not found.
So my questions are:
1) How to install Numpy for python3.x when Numpy is installed on python2.x?
2) How to get pip3?
3) Is it better to use virtual environments, such as Conda, instead of juggling between python2 and python3 on the system?
Thank you from a total n00b
------------------- Update -------------------
Reinstalling python3 also fixed another problem in my case.
When I ran brew doctor, one of the warning message I got was:
Warning: You have unlinked kegs in your Cellar Leaving kegs unlinked can lead to build-trouble and cause brews that depend on those kegs to fail to run properly once built. Run brew link on these: python –
This is a result of me running brew unlink python in order to fix
"Python quit unexpectedly"
when I launch Vim and also
"The ycmd server SHUT DOWN"
Both seem to relate to the YouCompleteMe autocomplete plugin which I downloaded for Python.
I got my idea of removing symlinks from here and here
However, Homebrew evidently did not like the absence of those 39 symlinks.
After uninstall (brew uninstall python3) and then re-install python3 (brew install python3) as Toby suggested, Homebrew gave me
You can install Python packages with
pip3 install <package>
Then when I pip3 install numpy and pip3 install scipy, both executed successfully.
To my surprise, symlinks created during Python installation used to cause aforementioned error messages for Python and YouCompleteMe, but now I open python files using Vim without crash from a fresh Python installation, which definitely created the symlinks.
------------------- Update2 ------------------
After re-installing Anaconda2, the same YouCompleteMe error came back. I suspect Anaconda messed up symlinks.
I would recommend using the Anaconda Python distribution.
The main reasons are as such:
You will have a Python distribution that comes with numpy and the rest of the Scientific Python stack.
The Anaconda Python will be installed under your home directory, with no need for sudo-ing to install other packages.
conda install [put_packagename_here] works alongside pip install [put_packagename_here]; conda install is much 'cleaner' (IMHO, differing opinions are welcome).
If you have a Python 3 environment as your default, then pip works out-of-the-box without needing to remember to do pip3.
conda environments are easier to manage than virtualenv environments, in my opinion. And yes, you can have Python 2 alongside Python 3.
I once messed up my system Python environment - the one that came with my Mac - and it broke iPhoto (back in the day). Since then, I became convinced of needing separate, atomic environments for different projects.
I've detailed more reasons in a personal blog post.
Other distributions, of course, are all good, provided they give you what you need :).
The simplest way on a Mac is with Homebrew:
http://brew.sh/
Install Homebrew, then run:
brew install python3 pip3
Edit --
Python3 includes pip3, but Homebrew occasionally has trouble linking to the correct versions, depending on what has been installed. Running the following command:
brew doctor
And if you see errors relating to python or unlinked kegs, try running:
brew uninstall python python3
And reinstalling after checking brew doctor.
https://unix.stackexchange.com/questions/233519/pip3-linked-to-python-framework-instead-of-homebrew-usr-local-bin

Error with igraph library - deprecated library

I have changed my computer to a Linux Mint x64 OS and I have throubles with a python library, igraph library, when i try to execute and old program I made.
DeprecationWarning: To avoid name collision with the igraph project, this visualization library has been renamed to 'jgraph'. Please upgrade when convenient.
I can't find information about how to change my code for the new library.
On a Win8 PC, via pip installation, it works perfectly but I can't make it working on my PC with Mint.
The normal installation made with
sudo apt-get install python-igraph
install 0.6.5-1 library version.
I've also tried installing it with pip but it gives me same error
but installed version is igraph-0.1.11-py2.py3-none-any.whl
I'm using only Graph class
from igraph import Graph
What have I to do to change my code to make it working with the new library? Am I missing something?
edit: it's working on my laptop with Mint x86 OS, library version 0.6.5-1
I think you have installed the wrong igraph libray.
This igraph(0.1.11) is the one you installed, while this igraph(0.7.1) is the one you need (and the well-known iGraph).
Using pip (once you've installed it), Just do:
sudo pip uninstall igraph
Then install the python-igraph-0.7.1 package, using either pip or apt-get as mentionned in comments below:
sudo pip install python-igraph
Hope it works.
sudo -H pip uninstall igraph
then:
sudo pip install python-igraph
worked for me.

Import pandas on jupyter ipython notebook fails

I am able to import the pandas package within the spyder ide; however, if I attempt to open a new juypter notebook, the import fails.
I use the Anaconda package distribution on MAC OS X.
Here is what I do:
In [1]: import pandas
and this is the response I get:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-97925edf8fb0> in <module>()
----> 1 import pandas
//anaconda/lib/python2.7/site-packages/pandas/__init__.py in <module>()
11 "pandas from the source directory, you may need to run "
12 "'python setup.py build_ext --inplace' to build the C "
---> 13 "extensions first.".format(module))
14
15 from datetime import datetime
ImportError: C extension: hashtable not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first.
You have more than one Python 2 engines installed. One in your main OS platform, another one inside Anaconda's virtual environment. You need to install Panda on the latter.
Run in your Bash prompt:
which python
Then run the following in Jupyter/IPython and compare the result with the output you got from the Bash script:
from sys import executable
print(executable)
If they differ, you should note the result of the latter (i.e. copy it), and then go to your Bash prompt, and do as follows:
<the 2nd output> -m pip install pandas
so it would be something like this:
/usr/bin/anaconda/python2 -m pip install pandas
And Pandas will be installed for your Anaconda Python.
There is a way to add library paths to your existing environment, using sys.path.append('path to alternative locations'), but this has to be done every time your want to use the alternative environment as the effects are temporary.
You can alternatively install everything in your main environment:
python -m pip install cython scipy panda matplotlib jupyter notebook ipython
Update:
Based on responses to the above section:
Install homebrew like so:
In your Terminal:
xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
then run:
brew doctor
brew update
brew upgrade
Now go ahead and install Python 2 through Brew:
brew install python
or for Python 3
brew install python3
Or do both. The install other useful stuff!
brew install git conda gfortran clang pkg-config
Then you can go ahead and install your desired libraries either using brew, or using pip, but first you should ensure that pip itself is installed.
easy_install pip
then you can install Python packages like so (NumPy is included in SciPy, and SciPy and Matplotlib depend on Cython and C, Scipy additionally uses fortran for ODE):
python2 -m install cython scipy pandas matplotlib jupyter
you can do that same thing for Python 3.
This clean install should really solve the problem. If it didn't, download Python from Python.org and re-install it. brew sometime refuses to install a package if it finds out that the package already exists. I don't recommend removing Python 2 so that you can install it through brew. That might cause issues with OS X. So the best alternative is to repair existing installations by installing the package downloaded from the website. OS X ensures that the package is installed in the right place. Once this is done, you can then go back to the instructions, but start from brew install python3.
I had the same issue on Mac OS X with Anaconda (Python 2). I tried importing the pandas package in python repl, and got this error:
ValueError: unknown locale: UTF-8
Therefore, I've added the following lines to my ~/.bash_profile:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
And this has fixed the issue for me.
One thing that you can do it is install the libraries straight in Jupyter, you can try, in the cell:
!pip install pandas
or
!pip3 install pandas

Installing numpy on Red Hat 6?

I'm trying to install numpy on a Red Hat (RHEL6) 64-bit linux machine that has Python 2.7. I downloaded and untar'd numpy 1.6.2 from Sourceforge, and I did the following commands in the numpy-1.6.2 folder:
python ./setup.py build
sudo python ./setup.py install #without sudo, this gives a permissions error.
Then, when I do import numpy on the Python prompt, I get ImportError: No module named numpy.
I read somewhere that numpy 1.6.2 is for Python 3.x, so I also tried the above steps with numpy 1.5.1, and I got the same ImportError.
I'm speculating that the solution lies in some environment variable gymnastics, but I'm not sure what files/directories Python needs to "see" that isn't in scope. Any suggestions for how to get numpy working?
I also tried some precompiled binaries for RHEL, but they gave various errors when I did sudo yum install [numpy precompiled binary url].rpm.
As an aside, my motivation for installing numpy is to use PyGnuplot. Also, I've installed numpy and PyGnuplot on other machines before, but it's been on Ubuntu and Mac OS.
RHEL6 ships numpy 1.4.1, see distrowatch. If 1.4.1 is new enough for you, you can install it with:
$ yum install numpy
When I install Python packages on Ubuntu using setup.py packages end up in
/usr/local/lib/python2.7/dist-packages/
assuming Python2.7
If numpy is installed there, you will need to append this path to your Python's path:
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/
you can append it permentanly using ~/.profile
Another way is by adding a .pth to a directory that’s already on Python’s path, for example
echo "/usr/local/lib/python2.7/dist-packages/" > /usr/local/lib/python2.7/site-packages/dist_pkg.pth
run that last command as root of course
I gave up on using RedHat, and I installed Ubuntu in a VM. It was a one-liner using apt-get.
Not an ideal solution to the question at hand, but I just didn't want to spend any more time chasing down a solution for RedHat.

Categories

Resources