How to get "python -m venv" to directly install latest pip version - python

As part of the compilation step for a new python version, I fetch and run get-pip.py, to have the latest pip installed next to the python executable:
$ /opt/python/3.7.0/bin/python --version
Python 3.7.0
$ /opt/python/3.7.0/bin/pip --version
pip 18.0 from /opt/python/3.7.0/lib/python3.7/site-packages/pip (python 3.7)
I have 25 such versions under /opt/python, although I mostly use the five latest versions of each major.minor version that is not EOL. To setup an invironment I used to run virtualenv or my virtualenvutils with the -p /opt/python/X.Y.Z/bin/python option to get a virtual environment with a specific version.
With Python 3.7 this gives the imp module deprecation warning:
$ virtualenv -p /opt/python/3.7.0/bin/python /tmp/py37virtualenv
Running virtualenv with interpreter /opt/python/3.7.0/bin/python
Using base prefix '/opt/python/3.7.0'
/opt/util/virtualenvutils/lib/python3.6/site-packages/virtualenv.py:1041: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
New python executable in /tmp/py37virtualenv/bin/python
Installing setuptools, pip, wheel...done.
I have little hope this will be solved in virtualenv, as this has had a PendingDeprecationWarning at least since 2014 (as can be seen from the output in this question)
While investigating replacing virtualenv with python -m venv in virtualenvutils, I first created a new venv based virtual environment by hand:
$ /opt/python/3.7.0/bin/python -m venv /tmp/py37venv
$ /tmp/py37venv/bin/pip --version
pip 10.0.1 from /tmp/py37venv/lib/python3.7/site-packages/pip (python 3.7)
That has an old pip version! If you use it, you'll get:
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command
In the virtual environment created with virtualenv you immediately get the latest version:
$ /tmp/py37virtualenv/bin/pip --version
pip 18.0 from /tmp/py37virtualenv/lib/python3.7/site-packages/pip (python 3.7)
I can run a post-creation step:
/tmp/py37venv/bin/pip install -U --disable-pip-version-check pip
which will take extra time. And if there was a some security update for pip, this would imply running the non-secure version to get a secure version, an ideal point of attack.
From virtualenvutils it is trivial to do the multiple steps to create a pip-less virtualenv and then add pip using get-pip.py. From the command-line this is not so simple:
$ /opt/python/3.7.0/bin/python -m venv --without-pip /tmp/py37venvnopip
$ /tmp/py37venvnopip/bin/python -c "from urllib.request import urlopen; response = urlopen('https://bootstrap.pypa.io/get-pip'); open('/tmp/tmp_get_pip.py', 'w').write(response.read())"
$ /opt/python/3.7.0/bin/python /tmp/tmp_get_pip.py
......
$ /opt/python/3.7.0/bin/pip --version
pip 18.0 from /opt/python/3.7.0/lib/python3.7/site-packages/pip (python 3.7)
What is causing /opt/python/3.7.0/bin/python -m venv to take that old pip version? Is that the version available when 3.7.0 was released?
How can I update my install under /opt/python/3.7.0 in some way so that using /opt/python/3.7.0/bin/python -m venv creates a virtualenv with the latest pip version without reverting to scripts, aliases or using multiple commands? Having the latest pip installed under /opt/python/3.7.0 obviously is not enough.
There are two bundled wheels:
/opt/python/3.7.0/lib/python3.7/ensurepip/_bundled/setuptools-39.0.1-py2.py3-none-any.whl
/opt/python/3.7.0/lib/python3.7/ensurepip/_bundled/pip-10.0.1-py2.py3-none-any.whl
I suspect I need to update those. Is there a better way than updating those by hand? Some option for /some/python -m venv would be nice.
(And running /some/python -m ensurepip --upgrade doesn't do the trick)
Running the deprecated /opt/python/3.7.0/bin/pyvenv has the same old pip version problem.

The trick is not to install the bundled version of pip (which will almost always be out of date), but to use it to install the most current version from the internet.
Standard library venv offers a --without-pip flag that can help here. After creating the virtual environment without pip, you can then you can "execute" ensurepip's wheel directly thanks to Python's zip importer. This is both faster and less hacky than installing pip and then immediately using that same pip installation to uninstall itself and upgrade.
Code speaks louder than words, so here's an example bash function for the process I've described:
# in ~/.bashrc or wherever
function ve() {
local py="python3"
if [ ! -d ./.venv ]; then
echo "creating venv..."
if ! $py -m venv .venv --prompt=$(basename $PWD) --without-pip; then
echo "ERROR: Problem creating venv" >&2
return 1
else
local whl=$($py -c "import pathlib, ensurepip; whl = list(pathlib.Path(ensurepip.__path__[0]).glob('_bundled/pip*.whl'))[0]; print(whl)")
echo "boostrapping pip using $whl"
.venv/bin/python $whl/pip install --upgrade pip setuptools wheel
source .venv/bin/activate
fi
else
source .venv/bin/activate
fi
}
If you prefer the older project virtualenv, it also offers --no-pip, --no-setuptools, and --no-wheel flags to achieve the same on Python 2.7.
Note: Python 3.9+ venv has an --upgrade-deps option to immediately upgrade the pip/setuptools versions after creating an environment, see https://bugs.python.org/issue34556 for more info about that. I don't use this option because it still goes through an unnecessary install/uninstall of the vendored versions, which is inferior to the method of creating an environment with the latest versions directly as shown above.

I use upgrade-ensurepip to update those pip and setuptools wheel files that are part of the ensurepip package. It's not as elegant as being able to upgrade ensurepip via pip, but it's still preferable to doing it manually.
https://pypi.org/project/upgrade-ensurepip/

It is an expected behavior. python -m venv calls python -m ensurepip to install pip and This answer shows that ensurepip would only install the bundled version even with --upgrade option. There isn't any official option to update the bundled pip and setuptools.
Well I have also no good idea to fix this problem as it just is the designed behavior. I would like to give two suggestions:
Use pipenv. It is really good! And it will be the next-generation official package manager in the future(Although there is a big problem related to current Pypi's structure. In short, a package manager can only decide the dependencies with downloading the whole package. This gives a huge difficulty to building dependencies graph.).
Implement your custom EnvBuilder, actually there is an official example about this. And in the example, it also use get-pip.py to install the latest pip.

Related

Should I use pip or pip3? [duplicate]

This question already has answers here:
pip or pip3 to install packages for Python 3?
(10 answers)
Closed 2 years ago.
Eventually, every single time I install a new Linux distribution I do sudo apt-get install python3.
However, once installed I always get confused. python is Python 2.7 and python3 is Python 3.x. But also it appears that pip is for Python 2 and pip3 for Python 3. That said most tutorials I see on Internet always use the traditional pip install even though it is about Python 3.
How should I deal with this? Should I simply continue to put this annoying 3 every time I use Python (pip3, ipython3, python3...)? In most of my lectures I read that creating a symlink python->python3 is a bad practice. Is that correct?
Use python3 -m pip or python -m pip. That will use the correct pip for the python version you want. This method is mentioned in the pip documentation:
python -m pip executes pip using the Python interpreter you specified as python. So /usr/bin/python3.7 -m pip means you are executing pip for your interpreter located at /usr/bin/python3.7.
Symlinking python->python3 is a bad idea because some programs might rely on python being python 2. Though, I have seen some Dockerfiles symlink python->python3, like TensorFlow's CPU dockerfile (it's less of an issue in a Docker image). Coincidentally, that same Dockerfile uses the python3 -m pip install syntax that I recommend.
creating a symlink python->python3 is a bad practice. Is that correct?
Sometimes. Some OSs (looking at you, macOS) deeply rely on python pointing to a Python 2 interpreter for internal tools and tasks. Deleting the shipped Python 2 interpreter (or aliasing python to a Python 3 interpreter) will break stuff. How to uninstall Python 2.7 on a Mac OS X 10.6.4?
Whether the correct command for Python 3 is pip or pip3 or (say) gaschplutzga depends on a number of factors.
If you only have Python 3, and you have a command named pip, that's probably safe to use. Going forward, this will be the simple, obvious, safe answer in more and more places.
If you have both, and there is a command called pip3 installed on your system, probably that's the correct one to use.
More generally, you can go through your PATH and look for commands with suitable names. On Unix-like systems with a POSIX-compatible shell, try the commands command -V pip3 and command -V pip. (On Windows systems, maybe try where pip3 and where pip, or pray to whatever dark deity informed your choice of operating system.)
If you receive output like
/opt/random/nonstandard/whoa/pip
/usr/local/bin/pip
/usr/bin/pip
you can try each of these in turn with the full path and adding the --version option to have them identify themselves. When you specify the full path, you are bypassing the system's PATH mechanism entirely. For example,
/opt/random/nonstandard/whoa/pip --version
might identify itself as belonging to Python version 3.2.1. If that's the one you want, and it's at the top of your PATH, you can simply rely on the PATH to give you this version when you type just pip. If not, perhaps you can shuffle your PATH (but understand that this changes the resolution order for all commands in the directory whose position you change) or create a simple alias or wrapper which bypasses the PATH for this particular command in your personal account. On Unix-like systems with a POSIX-compatible shell, this might look like
alias pip=/opt/random/nonstandard/whoa/pip
(to persist this across sessions, you'd add this to your .profile or similar - for Bash, try .bash_profile if it exists; for Zsh, try .zshrc. The full scoop for each shell is more complicated than I can squeeze into these narrow parentheses); on Windows, you might be able to control this by setting the environment variable PY_PYTHON, but there's a huge can of worms behind that "might".
Some sites and OSes / distros have additional wrappers or conventions which introduce additional options; if you use a specific package manager, perhaps also study its documentation. (One common example is Anaconda, though I don't believe it affects the naming or location of pip specifically.)
Use virtual environments, then pip would be associated with the python used to create that virtual environment. Whether you use pip or pip3, it will be equivalent to python3 -m pip as mentioned in jakub's answer. Also, given that Python 2.7 is already EOL (which means you will most likely work with Python 3) and that pip install-ing things onto the system packages should be avoided, then a virtual environment would be helpful here.
For example, using pipenv:
$ pipenv --python=/usr/local/opt/python#3.8/bin/python3
$ pipenv shell
Launching subshell in virtual environment...
(TEMP) $ pip --version
pip 20.2.3 from /Users/me/.venvs/temp2-SbXvZiFd/lib/python3.8/site-packages/pip (python 3.8)
(TEMP) $ pip3 --version
pip 20.2.3 from /Users/me/.venvs/temp2-SbXvZiFd/lib/python3.8/site-packages/pip (python 3.8)
For example, using venv:
$ python3.8 -m venv .venv
$ source .venv/bin/activate
(.venv) $ pip --version
pip 20.2.3 from /Users/me/temp2/.venv/lib/python3.8/site-packages/pip (python 3.8)
(.venv) $ pip3 --version
pip 20.2.3 from /Users/me/temp2/.venv/lib/python3.8/site-packages/pip (python 3.8)
The virtual environment takes care of making sure pip or pip3 in this env refers to the pip from the correct Python version. You can then happily follow tutorials that still use pip install something (unless of course that tutorial refers to a Python 2.7 or a system-wide installation).
You can install pip through pip3 and this should resolve this issue.
$ pip --version
pip 19.0.3 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
Notice that pip here is of Python 2.7 (in this example).
You can then force pip3 of Python 3.X to install pip under itself.
$ sudo pip3 install pip --upgrade
Installing collected packages: pip
Found existing installation: pip 8.1.1
Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-19.0.3
Once you check this again, it should reference Python 3.X so you don't have to deal with
what is what.
$ pip --version
pip 19.0.3 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
I doubt you'll want to use Python 2 after this, but if you do happen to work with Python 2 code, you can create a virtual environment to access those commands again. Otherwise, you won't have to worry about the pip or pip3 distinction after this.
Not really a duplicate of this question, but this helped me suggest this answer: Can pip (python2) and pip3 (python3) coexist?
Pip is for python version less than 3. and pip3 is used when you want to install packages for python version 3 or higher.

How to use pip3 for python 3.6 instead of python 3.5?

I'm using Kali dist so I have already installed Python 2.7, 3.5 and 3.6. Commands 'python' and 'pip' are associated with Python 2.7. But the 'python3' uses Python 3.6 while pip3 is installing packages for Python 3.5.
When I tried to create an venv:
pip3 -p python3.6 virtualenv myenv
I've got an error:
no such option: -p
How can I associate pip3 with Python 3.6 instead of Python 3.5?
Your version of pip is inextricably linked to your version of Python, you cannot tell pip "use this Python" or "use that Python." If you have a version mismatch between pip3 (using Python 3.X) and python3 (being Python 3.Y), it means your problem is with multiple overlapping distributions of Python and a weirdly configured $PATH.
If you run pip3 --version it will tell you the site-packages directory and Python version number that pip3 is associated with.
If you run python3 and then execute >>> import site; site.getsitepackages(), it should print the site-packages directory your python3 is using.
If these do not match, you've got path problems and you'll need to post more information about what operating system you're on, what Python distributions you're using, and how you installed them.
Update/Summary of Comment Thread: Original poster had a distribution-bundled Python 3.6 installed alongside a self-installed Python 3.5. The pip3 on their path was associated with Python 3.6 (system Python), while the command python3 was associated with Python 3.5 (their self-installed Python). Resolution:
Run which -a python3 to find Python 3.5. Add the location of Python 3.5 to your $PATH. (Do it in .profile or .bash_profile to make it permanent.)
You can explicitly run the pip3 script with a particular Python version, by prefixing it with the appropriate python3.x command:
ldo#theon:~> pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
ldo#theon:~> python3.5 $(which pip3) --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5)
To install a package in the same version location that's associated with the version associated with python3, use the following:
python3 -m pip install [package]
to pick a specific version that you'd like your package to be associated with (so you're not guessing with the above):
python3.5 -m pip install [package]
python3.7 -m pip install [package]
Also, be careful because pip3 can point to different locations and may not necessarily match the location of the python3 binary. I just found that out when I did a pip3 install and it failed to import when running python3.
You can also explicitly call pip3.5, pip3.7, etc, but honestly I prefer using the python[version] -m pip install [package] method because I know that it will install the package in the location associated with whatever python3.x binary I'm using.
When you install Python3, see if there's a comment such as this:
Ignoring ensurepip failure: pip 9.0.1 requires SSL/TLS
You might see entries like this in the log:
INFO: Can't locate Tcl/Tk libs and/or headers
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _dbm _gdbm
_lzma _sqlite3 _ssl
_tkinter readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
atexit pwd time
This answer describes using ensurepip
https://stackoverflow.com/a/38250442/1607937
Also see this regarding openssl
"SSL module in Python is not available" when installing package with pip3
If you want to use only one version of python you sould probably create an alias. Add this line at the end of your ~/.bashrc flie:
alias pip='python3.6 -m pip'
Then, run source ~/.bashrc, and now pip --version will show something like:
pip xx.x.x from /usr/lib/python3/dist-packages/pip (python 3.6)
you can update line #1 from /usr/bin/pip3 to #!/usr/bin/python3.8 as below
#!/usr/bin/python3.8
# GENERATED BY DEBIAN
import sys
# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip import main
if __name__ == '__main__':
sys.exit(main())
First find the right version of python you want to use:
$ which -a python3.6
/usr/bin/python3.6
then invoke that instance of python directly, e.g.
$ /usr/bin/python3.6 -m venv
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip] [--prompt PROMPT] ENV_DIR [ENV_DIR ...]
venv: error: the following arguments are required: ENV_DIR
Next, pip does not create virtual environments. The module venv does. Read the venv documentation for recommended usage. In your case, you might want:
$ /usr/bin/python3.6 -m venv myenv

Checking whether pip is installed?

I am using Python 2.7.12 and I want to check whether the pip is installed or not. For this, in command line of Python application I wrote pip list and pressed enter. However, I get an error like:
File"stdin",line 1
pip list
Syntax Error: invalid syntax
So, how can I solve this issue and get the list of modules as an output?
Thanks
Use command line and not python.
TLDR; On Windows, do:
python -m pip --version
OR
py -m pip --version
Details:
On Windows, ~> (open windows terminal) Start (or Windows Key) > type "cmd" Press Enter
You should see a screen that looks like this
To check to see if pip is installed.
python -m pip --version
if pip is installed, go ahead and use it. for example:
Z:\>python -m pip install selenium
if not installed, install pip, and you may need to add its path to the environment variables. (basic - windows)
add path to environment variables (basic+advanced)
if python is NOT installed you will get a result similar to the one below
Install python. add its path to environment variables.
UPDATE: for newer versions of python
replace "python" with py - see #gimmegimme's comment and link
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
If you are on a linux machine running Python 2 you can run this commands:
1st make sure python 2 is installed:
python2 --version
2nd check to see if pip is installed:
pip --version
If you are running Python 3 you can run this command:
1st make sure python 3 is installed:
python3 --version
2nd check to see if pip3 is installed:
pip3 --version
If you do not have pip installed you can run these commands to install pip (it is recommended you install pip for Python 2 and Python 3):
Install pip for Python 2:
sudo apt install python-pip
Then verify if it is installed correctly:
pip --version
Install pip for Python 3:
sudo apt install python3-pip
Then verify if it is installed correctly:
pip3 --version
For more info see: https://itsfoss.com/install-pip-ubuntu/
UPDATE
I would like to mention a few things. When working with Django I learned that my Linux install requires me to use python 2.7, so switching my default python version for the python and pip command alias's to python 3 with alias python=python3 is not recommended. Therefore I use the python3 and pip3 commands when installing software like Django 3.0, which works better with Python 3. And I keep their alias's pointed towards whatever Python 3 version I want like so alias python3=python3.8.
Keep In Mind
When you are going to use your package in the future you will want to use the pip or pip3 command depending on which one you used to initially install the package. So for example if I wanted to change my change my Django package version I would use the pip3 command and not pip like so, pip3 install Django==3.0.11.
Notice
When running checking the packages version for python: $ python -m django --version and python3: $ python3 -m django --version, two different versions of django will show because I installed django v3.0.11 with pip3 and django v1.11.29 with pip.
$ which pip
or
$ pip -V
execute this command into your terminal. It should display the location of executable file eg. /usr/local/bin/pip and the second command will display the version if the pip is installed correctly.
pip list is a shell command. You should run it in your shell (bash/cmd), rather than invoke it from python interpreter.
pip does not provide a stable API. The only supported way of calling it is via subprocess, see docs and the code at the end of this answer.
However, if you want to just check if pip exists locally, without running it, and you are running Linux, I would suggest that you use bash's which command:
which pip
It should show you whether the command can be found in bash's PATH/aliases, and if it does, what does it actually execute.
If running pip is not an issue, you could just do:
python -m pip --version
If you really need to do it from a python script, you can always put the import statement into a try...except block:
try:
import pip
except ImportError:
print("Pip not present.")
Or check what's the output of a pip --version using subprocess module:
subprocess.check_call([sys.executable, '-m', 'pip', '--version'])
You need to run pip list in bash not in python.
pip list
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
argparse (1.4.0)
Beaker (1.3.1)
cas (0.15)
cups (1.0)
cupshelpers (1.0)
decorator (3.0.1)
distribute (0.6.10)
---and other modules
In CMD, type:
pip freeze
And it will show you a list of all the modules installed including the version number.
Output:
aiohttp==1.1.4
async-timeout==1.1.0
cx-Freeze==4.3.4
Django==1.9.2
django-allauth==0.24.1
django-cors-headers==1.2.2
django-crispy-forms==1.6.0
django-robots==2.0
djangorestframework==3.3.2
easygui==0.98.0
future==0.16.0
httpie==0.9.6
matplotlib==1.5.3
multidict==2.1.2
numpy==1.11.2
oauthlib==1.0.3
pandas==0.19.1
pefile==2016.3.28
pygame==1.9.2b1
Pygments==2.1.3
PyInstaller==3.2
pyparsing==2.1.10
pypiwin32==219
PyQt5==5.7
pytz==2016.7
requests==2.9.1
requests-oauthlib==0.6
six==1.10.0
sympy==1.0
virtualenv==15.0.3
xlrd==1.0.0
yarl==0.7.0

Why 'python3 -m venv myenv' installs older version of pip into myenv than any version of pip I can find anywhere on the system?

This is not causing me any problem that I can't solve by activating the virtual environment and running pip install -U pip, but I always wonder where the older version of pip is coming from.
I'm using OS X 10.7.5. When I create a virtual environment using either pyvenv-3.4 myenv or python3 -m venv myenv, the version of pip that is installed inside the virtual environment is 6.0.8, but I have upgraded my global pip to 6.1.1.
Here is a terminal session demonstrating what I mean:
$ python3 -m venv myenv
$ myenv/bin/pip -V
pip 6.0.8 from /Users/dust/Desktop/myenv/lib/python3.4/site-packages (python 3.4)
Here is what I would like to occur:
$ source myenv/bin/activate
(myenv)$ pip -V
UPDATED SYSTEM VERSION HERE WOULD BE NICE
I can't find a pip 6.0.8 anywhere else, other than what is created inside virtual environments.
Here are the outputs of various commands that I have use to try and figure this out:
$ which pip
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip
$ which pip3
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip3
$ pip -V
pip 6.1.1 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)
$ pip3 -V
pip 6.1.1 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)
I even tried using find:
$ find / -type f -name pip 2>&1 | awk '! /^f.*$/'
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip
/usr/local/bin/pip
$ find / -type f -name pip3 2>&1 | awk '! /^f.*$/'
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip3
I thought maybe that the /usr/local/bin/pip might have been the culprit, but no:
$ /usr/local/bin/pip -V
pip 6.1.1 from /Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg (python 2.7)
Hmm. Perhaps the OS X python has it?
$ /usr/bin/python
>>> import pip
>>> pip.__version__
'6.1.1'
6.1.1 is reported no matter which distribution of python I ask, whether it be OS X's 2.7.1, python.org's 2.7.9, or python.org's 3.4.3.
Is it possible (or advisable) to update the version of pip that gets put into a virtual environment?
I face the same issue, running OSX 10.10.2 and python 3.4.2. Most recently I created a virtual environment in a debian wheezy machine with python 3.4.3 and also ended up with an older version of pip than available. had to upgrade pip.
I've been upgrading pip within the virtual environment to 6.1.1 from 6.0.8 manually, because I'm o.c.d about software library versions that way - and yes, I am upgrading my python 3 version to 3.4.3 right now. Anyway, my system's python3-pip is the latest version 6.1.1, so I've also wondered why pyvenv creates a new virtual environment and loads it with old pip.
I haven't noticed anything bad happen in any of the virtual environments due to upgrading pip, (but on the flip side, I haven't noticed anything good either) Apparently the new pip is faster -- didn't notice, and outputs less junk on successful installs because user's don't care -- also didn't notice, probably because i'm one of those that don't care, and also comes with a state-of-the art coffee machine capable of latte art to boot!!! -- still waiting on sudo pip install latte to finish :(
So, to answer your question, it is definitely possible, and probably advisable to upgrade, because apparently the new pip fixes some bugs and goes faster, but I guess the speed up isn't that major, and the bug fixes don't affect all that many people (I've never faced a bug with my usage of the old pip).
You can link to system site-packages using the flag --system-site-packages when you create a new virtual environment, like this
pyvenv myenv --system-site-packages
This will link to your system wide version of pip, and would remove the annoyance that is manually upgrading pip on every virtual environment, but if you do this, then is your virtual environment all that virtual?
update: following my rant above, I went into the venv package's source to dig. pip is set up by a method called _setup_pip in the file __init__.py, line 248
def _setup_pip(self, context):
"""Installs or upgrades pip in a virtual environment"""
# We run ensurepip in isolated mode to avoid side effects from
# environment vars, the current directory and anything else
# intended for the global Python environment
cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
'--default-pip']
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
So, venv seems to be calling ensurepip from the shell using the subprocess module.
One more minute of google-fu gave me this from the documentation for ensurepip.
ensurepip.version()
Returns a string specifying the bundled version of pip that will be installed when bootstrapping an environment.
So, from the command line, the following code:
$ python3 -c 'import ensurepip; print(ensurepip.version())'
6.0.8
displays my current pip that will be bootstrapped with ensurepip.
I guess we're stuck with the old version of pip for every new install until ensurepip gets upgraded, as I can't find a way to upgrade the version of pip that comes with ensurepip
Newer
If you want to "hotpatch" your installed python, just modify the versions listed in ensurepip/__init__.py and replace the two files in ensurepip/_bundled. You can find this location by running find * | grep ensurepip from the directory where python is installed. On macOS with Homebrew, this is the location: /usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ensurepip
You will also want to delete the ensurepip/__pycache__ directory that contains the .pyc files.
My older, build-time fix:
You are able to update the bundled versions of pip and setuptools by patching Python before building it from source. The following patch will update the bundled versions of pip and setuptools to the current available today. You will want to invoke configure with the following option: --with-ensurepip=upgrade
Those whl files are downloaded from PYPI here:
https://pypi.org/project/pip/#files
https://pypi.org/project/setuptools/#files
diff -ru Python-3.7.1/Lib/ensurepip/__init__.py Python-3.7.1.new/Lib/ensurepip/__init__.py
--- Python-3.7.1/Lib/ensurepip/__init__.py 2018-10-20 06:04:19.000000000 +0000
+++ Python-3.7.1.new/Lib/ensurepip/__init__.py 2018-11-27 02:36:19.301655008 +0000
## -8,9 +8,9 ##
__all__ = ["version", "bootstrap"]
-_SETUPTOOLS_VERSION = "39.0.1"
+_SETUPTOOLS_VERSION = "40.6.2"
-_PIP_VERSION = "10.0.1"
+_PIP_VERSION = "18.1"
_PROJECTS = [
("setuptools", _SETUPTOOLS_VERSION),
Only in Python-3.7.1/Lib/ensurepip/_bundled: pip-10.0.1-py2.py3-none-any.whl
Only in Python-3.7.1.new/Lib/ensurepip/_bundled: pip-18.1-py2.py3-none-any.whl
Only in Python-3.7.1/Lib/ensurepip/_bundled: setuptools-39.0.1-py2.py3-none-any.whl
Only in Python-3.7.1.new/Lib/ensurepip/_bundled: setuptools-40.6.2-py2.py3-none-any.whl
If you're lucky, you can bump it up with a Python patch.
Is your Python outdated? This is using macports, but any of brew, apt should do.
port outdated
python36 3.6.9 < 3.6.10
Let's fix that:
sudo port upgrade python36
And now... drumroll, in my newly-recreated venv, it seems that the Python patch has brought along the latest pip.
pip --version shows 20.0.2, it was at 18 previously and that's precisely what it was complaining about in the venv.
This is after spending a ludicrously long amount of time trying to figure out how to update my user-level Python 3.6 version of pip, on a mac where, of course, if you quit the virtualenv you are at Python 2.7 and where Python 3.6, installed through macports, doesn't seem to have a pip command to work with, until you build a venv.
From looking at /opt/local/Library/Frameworks/Python.framework/Versions/3.6, where macports puts things, I suspect Utknonos is on to something, so +1.
There really should be something a bit more obvious, like a pip-system-selfupdate command or a pip36 that comes along with python36. Especially considering that a search of pip upgrade will return tons of hits on anything but this subject.
Note: after my update, even though Python has been updated
(venv)$ pwd
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
(venv)$ grep PIP_VERSION ensurepip/__init__.py
_PIP_VERSION = "18.1"
(venv) $python --version
Python 3.6.10
(venv) $pip --version
pip 20.0.2 from .../venv/lib/python3.6/site-packages/pip (python 3.6)

Instructing Pip to Use Python 3

I'm using Ubuntu, how do I instruct pip to use the Python3 installation and not Python2.6? 2.6 is the default installation on Ubuntu. I can't upgrade that as it will break Ubuntu.
Any single pip installation is (roughly) specific to one Python installation. You can, however, have multiple parallel pip installations. Your package manager probably has a package called pip-3.3 or similar. If not, you can manually install it (run the get-pip.py script using Python 3.3), though you'll have to be careful that it ends up in the right place in PATH. You can also use a virtualenv.
Install pip by downloading the source and doing:
python3 setup.py install
Then, you should have a pip3 or a pip-3.3 command that you can use, to instruct pip to install whatever package using python3
Conversely, if you want to migrate to python3 entirely, do which python and follow the symlinks to /usr/local/bin (I think). There, you should be able to sudo ln -s to change what python points to

Categories