Installing python packages with multiple versions on OSX - python

I am attempting to install a package for python3.4 on Mac OSX 10.9.4. As you know, python ships with OSX, so when I installed python3.4 I was happy to find that it came with its own version of pip, that would install packages to it (installing pip on a mac with multiple versions of python will cause it to install on the system's python2.7.)
I had previously tried installing this package (https://pypi.python.org/pypi/chrome/0.0.1) with my first installation of pip (the one tied to python2.7) and found that it successfully installed on that version, but not on any others.
I ran an install with the new pip keyword for python3.4 (which when called by itself spits out the help page so i know it works) and it told me that the package was already installed and to try updating. The update revealed that I already had the most recent version. so I tried uninstalling it from just the python3.4 and reinstalling to no avail, and got the same results when uninstalling pip from python2.7 and reinstalling only on version 3.4.
I know that's a bit hard to follow but hopefully that makes sense.
I also reviewed the content here with no success.
RESOLVED:
while python did have a directory named the same as a directory it uses with packages, this was not the correct directory, for me it was in a subdirectory of library. while documentation said that referencing pip2 would cause the package to install on python3.4, this was false. however, referencing pip3.4 worked for me.

My suggestion is that you start using virtualenv.
Assuming you have 3.4 installed, then you should also have pyvenv. As for pip and 3.4, it should already be installed.
Using for example version 3.4 create your own virtual environment and activate it:
$ mkdir ~/venv
$ pyvenv-3.4 ~/venv/py34
$ source ~/venv/py34/bin/activate
$ deactive # does what is says...
$ source ~/venv/py34/bin/activate
$ pip install ... # whatever package you need
With version 2.7 first install virtualenv and then create your own virtual environment and activate it. Make sure that setuptools and pip are updated:
$ virtualenv-2.7 ~/venv/venv27
$ . ~/venv/venv27/bin/activate
$ pip install -U setuptools
$ pip install -U pip
$ pip install ... # whatever package you need

Related

How to Install virtualenv without pip?

I have a problem on pip.
As I use pyenv, using python version 3.7.x, there is no problem around pip.
$ /home/yuis/.pyenv/shims/pip --version
pip 19.2.3 from /home/yuis/.pyenv/versions/3.7.6/lib/python3.7/site-packages/pip (python 3.7)
But using python version 3.6.x, I get a result that saying the pip is located on "~/.local", but not on "~/.pyenv".
This is very bad for me. Because the version what I need now is 3.6.x.
This incomprehensive pip behavior continues if I install other python 3.6 version, so python 3.6.12 and 3.6.11 will show this same path.
I guess this bug is most probably because some kind of conflict from the locally installed python and pyenv installed one.
$ /home/yuis/.pyenv/shims/pip --version
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
pip 20.3.3 from /home/yuis/.local/lib/python3.6/site-packages/pip (python 3.6)
Now, I don't care much about pip anymore, because it seems an unsolvable issue. No time to waste. So now I want to try with virtualenv.
I need to install virtualenv first, because I can see this error.
$ virtualenv venv
pyenv: virtualenv: command not found
The `virtualenv' command exists in these Python versions:
3.5.10
3.7.6
Note: See 'pyenv help global' for tips on allowing both
python2 and python3 to be found.
But both of the followings didn't work.
pip install virtualenv
/home/yuis/.pyenv/shims/pip install virtualenv
python -m pip install virtualenv
Now I have no idea what is going on on my machine and how to solve this problem.
Have you tried python -m virtualenv .venv?
Try this maybe,
pip install git+https://github.com/pypa/virtualenv.git#main
You can visit this link for further details,
Also make sure your python & conda env are properly configured and added to PATH

I'm confused about the difference between pip and pip3, in anaconda env

TL;DR: a package is installed under pip3, but it cannot be found under Python3. Why?
All of this is happening in my anaconda base environemnt:
So I've been struggling with tensorflow and its versions (another post coming up).Turns out version 2.1 is only available at pip and not with conda install. So after upgrading pip3 install --upgrade pip I install pip3 install tensorflow==2.1.0. I open Jupyer-Notebook afterwards, and turns out tensorflow is not installed(running Python3). I check from the terminal first for the version, and then to uninstall tensorflow. It is not installed under pip (as expected) but it is indeed installed under pip3. I also get this message when uninstalling via pip3:
"pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly."
which might be related. I was under the impression that pip installs packages for the default python (3.7.4 in my case) but pip3 installs them only for Python3. What am I missing?
Thanks!
a package is installed under pip3, but it cannot be found under Python3. Why?
Because you have many different Pythons. pip doesn't install packages for all Pythons; pip3 doesn't install packages for Python3. They install packages for that particular Pythons they're running under. You cannot expect to install a package with one Python and import it in another eve if they're of the same version.
To see what Python is used with a particular pip see its shebang:
head -1 $(which pip)
head -1 $(which pip3)
If the shebang is #!/usr/bin/env python continue investigating with which python (or which python3).
Finding the Python run python -m site to see where from the packages are imported.

Python 3.5 install pyvenv

I am trying to get a virtual environment for a repo that requires python 3.5. I am using Debian, and from what I can tell, python 3.5 does not have an aptitude package. After reading some posts, it was recommended to download 3.5 source code and compile it.
After running the make and install, python3.5 was installed to /usr/local/bin. I added that to the $PATH variable.
Here is where I ran into problems. After I ran:
$ cd project-dir
$ pyvenv env
$ source env/bin/activate
$ pip install -r requirements.txt
I was getting issues with needing sudo to install the proper packages. I ran:
$ which pip
and it turns out that pip was still using the /usr/local/bin version of pip.
$ echo $PATH
returned
/home/me/project-dir/env/bin:/usr/local/bin:/usr/bin:/bin: ...
I am assuming that because the /usr/local path came after the virtual environment's path in my PATH variable, it is using that version of pip instead of my virtual environments.
What would be the best way to run the correct version of pip within the virtualenv? The two options I can think of is moving the binaries over to /usr/bin or modifying the activate script in my virtual env to place the virtualenv path after /usr/local.
Option 1
You can upgrade pip in a virtual environment manually by executing
pip install -U pip
Option 2
Good method to upgrade pip inside that package
python -m ensurepip --upgrade does indeed upgrade the pip version in the system (if it is lower than the version in ensurepip).
You are facing this problem, because venv uses ensurepip to add pip into new environments:
Unless the --without-pip option is given, ensurepip will be invoked to
bootstrap pip into the virtual environment.
Ensurepip package won't download from the internet or grab files from anywhere else, because all required components are already included into the package. Doing so would add security flaws and is thus unsupported.
Ensurepip is not designed to give you the newest pip, but just "a" pip. To get the newest one use the manual way at the beginning of this post.
To check ensurepip version you can type into python console import ensurepip print(ensurepip.version())
More Findings for further reading:
To upgrade ensurepip manually using files - https://github.com/python/cpython/commit/f649e9c44631c07e707842c42747b651b986dcc4
What's the proper way to install pip, virtualenv, and distribute for Python?
Comprehensive beginner's virtualenv tutorial?
Kesh's answer led me in the right direction.
The problem was that I didn't actually have pip installed in my venv.
It turns out, when I built python3.5 from source, I did not have the libssl-dev package. It looks like one of the dependencies of ensurepip was the python ssl package that didn't get installed because I didn't have libssl-dev.
To fix the problem, I rebuilt python 3.5 for source with the libssl-dev package installed. The rebuilt python now included the ssl package, which allowed ensurepip to install pip in my virtual environment.
Try installing it locally:
pip install --user -r requirements.txt
which would, I believe, install the file in a sub-directory of your $HOME directory (which your virtual env I would think would set). Otherwise I think you could just use:
/path/to/virtualenv/pip install -r requirements.txt

How to use pip with python3.5 after upgrade from 3.4?

I'm on Ubuntu and I have python2.7, (it came pre-installed) python3.4, (used before today) and python3.5, which I upgraded to today, installed in parallel. They all work fine on their own.
However, I want to use pip to install some packages, and I can't figure out how to do this for my 3.5 installation because pip installs for 2.7 and pip3 installs python 3.4 packages.
For instance, I have asyncio installed on 3.4, but I can't import it from 3.5. When I do pip3 install aysncio, it tells me the requirement is already satisfied.
I'm a bit of a newbie, but I did some snooping around install directories and couldn't find anything and I've googled to no avail.
I suppose you can run pip through Python until this is sorted out. (https://docs.python.org/dev/installing/)
A quick googling seems to indicate that this is indeed a bug. Try this and report back:
python3.4 -m pip --version
python3.5 -m pip --version
If they report different versions then I guess you're good to go. Just run python3.5 -m pip install package instead of pip3 install package to install 3.5 packages.
Another way would be to setup a virtual environment:
$ python3.4 -m venv envdir
$ source envdir/bin/activate
$ pip --version
Obviously, this won't install the packages globally and you'll have to source venv/bin/activate every time you wan to make use of it.

Control the pip version in virtualenv

How do I control the version of pip which is used in a freshly created venv?
By default, it uses a vendored pip distribution which may be out of date or unsuitable for whatever other reason. I want to be able to create a venv with a user-specified version of pip installed initially, as opposed to creating one and then upgrading the pip installation from within the env.
For me, I just upgraded pip/virtualenv/virtualenvwrapper on my machine (not inside the virtualenv). Subsequently created virtualenvs had the updated version.
deactivate
pip install --upgrade pip virtualenv virtualenvwrapper
mkvirtualenv ...
From reading the source of virtualenv, it looks like pip is installed from a source tarfile included with virtualenv. In virtualenv 1.10.1, it is pip-1.4.1.tar.gz in the site-packages/virtualenv_support directory (it gets setuptools from the same place). You could feasibly replace that archive to control the version; virtualenv.py, at least the version I have, doesn't care which version of pip is there:
if not no_pip:
install_sdist('Pip', 'pip-*.tar.gz', py_executable, search_dirs)
You could also pass the --no-pip option and then install the version you want from source.
In virtualenv 1.11, it looks for a wheel file (e.g. pip-*.whl) instead of a tar.gz, but other than that it acts the same way (thanks #wim for the update).
You cannot downgrade pip using pip, the solution is to install a specific version in your virtual environment:
virtualenv env -p python3.6 --no-pip
source env/bin/activate
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py pip==18.1
This will allow you to keep using --process-dependency-links that was removed in pip 19.
It's easy enough to replace the pip that gets installed in your virtual environment. Within your virtual environment active, simply execute the following command:
pip install pip==1.4.1
Since Python 3.9 the stdlib venv module has EnvBuilder.upgrade_dependencies. Unfortunately, it has two shortcomings:
Won't really help users to install a specific pip version, only the latest.
It still installs the vendored pip and setuptools versions first, and then uninstall them if they're outdated, which they almost always will be in practice.
It would be ideal to install the latest versions directly! The venv CLI provides a --without-pip argument that is useful here. You can use this to opt-out of the vendored pip, and then actually use the vendored pip wheel to install your desired pip version instead (along with any other packages you might want in a freshly created virtual environment).
It's best to put it into a function - this goes into your shell profile or rc file:
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] = pathlib.Path(ensurepip.__path__[0]).glob('_bundled/pip*.whl'); 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
}
As written, this function just pulls latest pip, setuptools, and wheel from index. To force specific versions you can just change this line of the shell script:
.venv/bin/python $whl/pip install --upgrade pip setuptools wheel
Into this, for example:
.venv/bin/python $whl/pip install pip==19.3.1
For Python 2.7 users, you may do a similar trick because virtualenv provides similar command-line options in --no-pip, --no-setuptools, and --no-wheel, and there is still a vendored pip wheel available to bootstrap since Python 2.7.9. Pathlib will not be available, so you'll need to change the pathlib usage into os.path + glob.
While creating virtual environment using venv module, use optional argument --upgrade-deps.
That will upgrade pip + setuptools to the latest on PyPI.
Example : python3 -m venv --upgrade-deps .venv
Reference link :
venv module documentation
It indicates "Changed in version 3.9: Add --upgrade-deps option to upgrade pip + setuptools to the latest on PyPI"
Note : I tried this using Python 3.10.4
Solved the same issue today on my windows machine with python 3.10.2 installed.
download required pip wheel from history to path\to\python\lib\ensurepip\bundled
in path\to\python\lib\ensurepip\__init__.py change _PIP_VERSION = your version
create environment as usual python -m venv path\to\env
I had issues with pip 22.3.1, so I wanted to downgrade it to 22.3, while pip 22.3.1 produces errors and not letting me downgrade as the other solutions suggest.
I solved the issue by creating a new venv with the specific pip version, as follows:
virtualenv env -p python3.10 --pip 22.3
TLDR
python -m pip install --upgrade pip==<target version number>
Example
Downgrading from pip 20.3 to pip 19.3 from within a virtual environment.
(env) $ pip --version
pip 20.3.1
(env) $ python -m pip install --upgrade pip==19.3 # downgrading
Collecting pip==19.3
Using cached pip-19.3-py2.py3-none-any.whl (1.4 MB)
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.3.1
Uninstalling pip-20.3.1:
Successfully uninstalled pip-20.3.1
Successfully installed pip-19.3
(env) $pip --version trex#Tobiahs-MacBook-Pro
pip 19.3

Categories