mac pre-bundled python and newly-installed one - python

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

Related

No module named scipy even though it is installed

I am trying to use scipy but when I import it with import scipy, it says "no module named scipy. I have used pip list to see if it is installed and it is, and i have tried pip install scipy but it just said that it was already installed. Does anyone know how to fix this?
SOLVED: ran the code with python3 instead of python
it is possible you have 2 versions of python installed, python2.7 or python3.x .. when you install stuff with pip and want to use it for a specific python version:
pip install scipy is for python 2.7, so its "python" in the terminal and "python x" to run scripts
pip3 install scipy is for python 3, so its "python3" in the terminal and "python3 x" to run scripts
in your case, you would be calling scripts like:
python pythonFile.py
you would be installing packages like:
pip install package
and you would be using python in the terminal with "python"
if that still doesn't work, for me solving that problem was updating numpy. not sure if it would be the same with you. hope this helped

Is there a solution to why I can't pip install any python modules past 2.7?

I can't install any python modules that require python 2.7 or later. I have uninstalled everything that Mac would let me that was related to python 2, and I run everything on python 3. I am completely lost. I am on Mac and whenever I try to install a module(like praw) this pops up.
I used the command
pip install praw
Collecting praw
Using cached https://files.pythonhosted.org/packages/41/89/94c1ec81a05536e2c2a1dc2e8f5402c8ad65963f28948bf41c64621e238b/praw-6.5.0-py2-none-any.whl
ERROR: Package 'praw' requires a different Python: 2.7.16 not in '>=3.5' ```
Welcome to Stack Overflow Malachi! I believe you're looking for the command pip3 (pip3 install praw) to install Python 3 packages through pip. If you open your terminal and type man pip to get the manual page, there is a line that says:
pip is the command to use when installing packages for Python 2, while pip3 is the command to use when installing packages for Python 3.
Looks like the most recent version of praw requires Python 3.5 or greater.
The last version that supported Python 2.7 was praw 5.4.0. If you're still using Python 2.7 and need this package, try running
pip install praw==5.4.0
If you're using Python 3.x, check to see if you're using the right version of pip. If you have both Python 2 and Python 3 installed on your system, you'll likely need to install system wide packages using pip3 instead if pip. In this case, try running
pip3 install praw
Alternatively, if you're targeting a specific Python interpreter (e.g. python3.7, python3.8, etc), and want to be certain that you are using the correct pip executable for your interpreter, you can run pip as a Python package via
python3 -m pip <args>
where python3 can be replaced by any interpreter path.

Installing Python Libraries When Multiple Versions of Python Exist

When I run python -V from terminal, I see that Python 2.7.10 is installed. I want to keep this as the "global" version as OSX utilizes it.
When I run Idle, I see that Python 3.6.0 is running. How do I install libraries to this version of Python?
For example, if I run pip install bs4, the library is installed here beautifulsoup4 in /Library/Python/2.7/site-packages/beautifulsoup4-4.5.3-py2.7.egg - which is obviously Python 2.7.
So when I run my script from Idle, I get the following error:
ModuleNotFoundError: No module named 'bs4'
You want to use virtualenv. It sounds like you have two versions of Python installed and you need to focus on one while being able to manage the packages in each. Virtualenv will do this for you.
First install virtualenv (https://virtualenv.pypa.io/en/stable/),
Second run it specifying the version of python you want as so: `virtualenv -p /usr/bin/python2.6
Third you can use pip to install packages directly into this environment. This increases the amount of disk space you need, but will allow you greater control over your code.
When you have two versions of python, you will need to specify which version of python you would like to run. You can do this with the activate command. For example:
activate python3
Once you have activated the python 3 environment, you can then run pip:
pip3 install bs4
which will install the beautiful soup library in your python 3 environment.
Another answer was found here: https://stackoverflow.com/a/4910393/1580659
pipVERSIONNUMBER install will install the library to the correct version of Python.
$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage

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

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

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.

Categories

Resources