I have always been under the belief that pip manages packages for one's python2, and pip3 for one's python 3. On a JupyterHub server that I run, I have the habit of installing packages that our team uses as root. Doing sudo -i and then pip3 install <package-name>. Sometimes, this makes import <package-name> work from a python3 notebook in Jupyter. But often not. Then, if I instead do pip install , the package is accessible from notebooks (python3 ones, all of them) on the server. Why is this?
This is what I have:
~# which python
/anaconda3/bin/python
~# which python3
/anaconda3/bin/python3
~# which pip
/anaconda3/bin/pip
~# which pip3
/anaconda3/bin/pip3
~# which jupyter
/anaconda3/bin/jupyter
I found the answer to my question here:
pip3 always operates on the Python3 environment only, as pip2 does
with Python2. pip operates on whichever environment is appropriate to
the context. For example if you are in a Python3 venv, pip will
operate on the Python3 environment.
Related
So i'm trying to implement stripe on a Django app and i'm having issues.
I installed Stripe using pip3 -install stripe and it downloaded. However when I run the server it says
ModuleNotFoundError: No module named 'stripe'
So looking around and on this I think I found some sort of an answer.
https://nomodulenamed.com/a/I-have-installed-the-package-using-pip#fail-to-install
Are pip and python consistent?
Seems like the answer is no.
pip3 -V returned pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
and
python3 -V returned Python 3.8.2
It seems that the easy fix is using python3 -m pip3 -V but that returns No module named pip3
and
python3 -m pip -V returns pip 20.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
but pip -V returns zsh: command not found: pip
which leaves me quite confused
more over i'm suppose to do # install your package
python -m pip <your-package-name>
so what goes in the place of
<your-package-name>
and I'm I suppose to use pip of pip3 since I use pip3 to install thing.
Since you can have more than one Python2 installation and more than one Python3 installation available on your machine, your question is better answered by understanding virtual environments.
It is precisely the reason why virtual environments exist!
when you create a python3 virtual environment there is no need to call pip3 as it is the default pip.
start by creating your virtual env (Assuming you have virtualenv installed ... if not install it on linux ubuntu by calling
sudo apt-get install virtualenv
sudo apt-get install python3-pip
python3 -m venv env
source bin/env/activate
pip install <yourpackage>
but I believe you are on macOS since you are getting zsh error,
fix your installation by using homebrew
brew install python3
pip3 install virtualenv
virtualenv -p python3 <path-to your-project>
source <path-to your-project>/bin/activate
As the title says, when i run pip3 freeze or pip3 list there is no output on the terminal!
Virtualenv seems also to be broken cause when i was inside of the env it was using the global django-admin.
I did sudo pip3 install --upgrade pip and after that i think it broke.
OS: Manjaro Linux
Python3 Version: Python 3.7.4
Pip3 Version: pip
19.2.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)
Find out how to cleanly reinstall pip (or even Python) from your operating system and really try to avoid anything with sudo pip or even sudo python. My recommendation is to not even install pip at all and use the one preinstalled in the virtual environments (when you do python3 -m venv .venv) instead, this is to avoid any conflict with the Python packages installed by your operating system.
I used VirtualEnv to create a python2 environment without system site packages like this:
virtualenv -p /usr/bin/python2.7 --no-site-packages ENV2.7
And I want to install packages in this environment.
However, I found that my python code is still trying to look for packages out of this environment.
For example, after activate this env, I used:
pip install matplotlib
And in my demo.py, there is
import matplotlib
But this raised an error, and can not find this package
However, when I use python in the terminal and enter the interactive python, import matplotlib dose not raise an error.
Then I started another terminal and tried to install this package out of the environment by pip3:
pip3 install matplotlib
It turned out that my demo.py just work well.
Any idea? Many Thanks!
It sounds like your virtualenv pip version may be using pip3 instead of pip2:
Make sure you are using the correct python version in your project that you mean to, and using the same version of pip in your virtualenv. (Note that you use pip above once, then you used pip3 outside your virtualenv.)
Check your pip version from inside the virtualenv:
workon (your env name)
which pip
pip -V
Output should look something like:
$ which pip
/home/yourname/.virtualenvs/testenv/bin/pip
$ pip -V
pip 9.0.1 from /home/yourname/.virtualenvs/testenv/local/lib/python2.7/site-packages (python 2.7)
It should tell you you're using pip inside your virtualenv, and the correct python version.
If that looks correct, install your packages.
pip install (whatever)
Check they are installed with pip freeze.
Run your project. :)
I am using OSX and I have pip installed for both Python3.5 and Python2.7. I know I can run the command pip2 to use Python2 and when I use the command pip3 Python3.x will be used.
The problem is that the default of pip is set to Python2.7 and I want it to be Python3.x.
How can I change that?
edit:
No, I am not running a virtual environment yet. If it was a virtual environment I could just run Python3.x and forget all about Python2.7, unfortunately since OSX requires Python2.7 for it's use I can't do that. Hence why I'm asking this.
Thanks for the answer. I however don't want to change what running python does. Instead I would like to change the path that running pip takes. At the moment pip -V shows me pip 8.1.2 from /Library/Python/2.7/site-packages (python 2.7), but I am looking for pip 8.1.2 from /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (python 3.5) I am sure there has to be a way to do this. Any ideas?
Run this:
pip3 install --upgrade --force pip
or even more explicit:
python3 -m pip install --upgrade --force pip
This will install pip for Python 3 and make Python 3 version of pip default.
Validate with:
pip -V
I always just run it via Python itself, this way:
python3 -m pip install some_module
or
python2 -m pip install some_module
The -m calls the __main__.py module of a specified package. Pip supports this.
Can't you alias pip='pip3' in your ~/.bash_profile?
In Terminal, run nano ~/.bash_profile, then add a line to the end that reads alias pip='pip3'. This is safe; it won't affect system processes, only your terminal.
For your projects, you should be using a virtualenv.
You can choose which python will be that of the virtualenv at creation time, by specifying it on the command line:
virtualenv -p python3 env
# then
. env/bin/activate
python # ← will run python3
That python interpreter will be the one used when you run python or pip while the virtualenv is active.
Under the hood, activating the virtualenv will:
modify your PATH environment setting so binaries in env/bin
override those from your system.
modify your PYTHONHOME
environment setting so python modules are loaded from env/lib.
So python, pip and any other package you install with pip will be run from the virtualenv, with the python version you chose and the package versions you installed in the virtualenv.
Other than this, running python without using virtualenv will just run the default python of the system, which you cannot usually change as it would break a lot of system scripts.
It works for me:
As super-user
Uninstall pip
sudo pip uninstall pip
Install pip
sudo python3 -m pip install --upgrade --force pip
Check install path
sudo pip -V
As local-user
Uninstall pip
pip uninstall pip
Install pip
python3 -m pip install --upgrade --force pip
Check install path
pip -V
Although PEP 394 does not specifically mention pip, it does discuss a number of other Python-related commands (including python itself). The short version is that, for reasons of backwards compatibility, the unversioned commands should refer to Python 2.x for the immediate future on most reasonable systems.
Generally, these aliases are implemented as symbolic links, and you can just flip the symlink to point at the version you want (e.g. with ln -f -s $(which pip3) $(which pip) as root). But it may not be a good idea if you have any software that expects to interact with Python 2 (which may be more than you think since a lot of software interacts with Python).
The saner option is to set up a Virtualenv with Python 3. Then, within the Virtualenv, all Python-related commands will refer to 3.x instead of 2.x. This will not break the system, unlike the previous paragraph which could well break things.
Since you have specified in the comments you want syntax like pip install [package] to work, here is a solution:
Install setuptools for Python3: apt-get install python3-setuptools
Now pip for Python3 could be installed by: python3 -m easy_install pip
Now you can use pip with the specific version of Python to
install package for Python 3 by: pip-3.2 install [package]
Why not just repoint the link /bin/python to python3? It seems like the easiest solution. Especially if you want it for all users of your system.
I have my Ubuntu 12.04 system set up so that I can create a virtualenv with either Python 2.7 or Python 3.3, and run IPython Notebook. The problem is, I don't know exactly what I did to my system to make this possible. I am trying to help someone else get their system set up the same way, and I'm not sure what packages I am missing.
On my system I can run the following commands to get IPython Notebook running in a virtualenv:
~$ mkdir test_ipython3.3
~$ cd test_ipython3.3
~/test_ipython3.3$ virtualenv -p python3.3 venv
~/test_ipython3.3$ source venv/bin/activate
(venv)~/test_ipython3.3$ pip install ipython[all]==1.1.0
I can do the same set of commands with virtualenv -p python2.7 venv, and have an almost identical environment except it runs Python 2.7.
I am trying to get a 12.04 installation on virtualbox set up so that I can run these commands successfully as well, but I keep failing. After building a clean Ubuntu 12.04 machine in virtualbox, I do the following:
# Update machine:
sudo apt-get update
sudo apt-get dist-upgrade
# Install Python 3.3:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python3.3
# Install virtualenv
sudo apt-get install python-pip
sudo pip install pip --upgrade
sudo pip install virtualenv
# Install necessary packages:
sudo apt-get install python-dev python3.3-dev libzmq-dev
# Build a venv, using Python 2.7, which works:
~$ mkdir test_ipython2.7
~$ cd test_ipython2.7
~/test_ipython2.7$ virtualenv -p python2.7 venv
~/test_ipython2.7$ source venv/bin/activate
(venv)~/test_ipython2.7$ pip install ipython[all]==1.1.0
(venv)~/test_ipython2.7$ ipython notebook
# Works, opening an ipynb that runs Python 3.3
# Build a venv, using Python 3.3, which fails:
~$ mkdir test_ipython3.3
~$ cd test_ipython3.3
~/test_ipython3.3$ virtualenv -p python3.3 venv
~/test_ipython3.3$ source venv/bin/activate
(venv)~/test_ipython3.3$ pip install ipython[all]==1.1.0
(venv)~/test_ipython3.3$ ipython notebook
# Fails, says that ipython is not installed, despite having reported otherwise
After trying to install ipython in the 3.3 virtualenv, I get a message that ipython and a number of supporting packages have been installed successfully. But when I try to run ipython or ipython notebook, I get a message that ipython is not installed. Watching the install process, and scrolling back through the output, I can't find any obvious failures. I even installed zmq from source, so I have zmq 4.0.3 installed, which ipython is finding during its installation.
Can anyone spot where I am going wrong?
IPython 1.x creates scripts with a '3' suffix when installed with Python 3, to avoid conflict with IPython installed with Python 2, so the command you want is:
ipython3 notebook
In current development IPython (will be 2.0), this behavior is changed somewhat, where IPython installs both the non-suffix and the suffix entry points (ipython and ipython3 on Python 3, ipython and ipython2 on Python 2), following the pattern established by other packages, such as nose.
I'm still quite curious about the best way to install ipython when you want access to a 2.7 interpreter sometimes, and a 3.3 interpreter other times.
There are two ways to deal with this:
The first is to create an ipython script somewhere very high priority in your PATH (I use ~/bin),
with the contents:
#!/usr/bin/env python
import IPython
IPython.start_ipython()
This will use the current python on your PATH, no matter what, so when you activate a Python 3 env, ipython will use that, etc.
The second is to just use:
python -m IPython
or
python3 -m IPython
which is the same as typing ipython, but you are specifying the interpreter to use explicitly, so there can be no doubt about what Python is in use.
These problems seem to be solved at: http://ihrke.github.io/jupyter.html