How do I change PIP to use Python 2.7 - python

I am on AWS Ec2 Amazon AMI. Trying to install virtualenv but PIP is set to use Python2.6
# pip -V && virtualenv --version
pip 9.0.1 from /usr/local/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg (python 2.6)
bash: /usr/bin/virtualenv: No such file or directory
# python -V
Python 2.7.12
I tried uninstalling virtualenv and reinstalling it but no luck.
#pip install virtualenv
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
Requirement already satisfied: virtualenv in /usr/local/lib/python2.6/site-packages
Here is the install directory:
# which pip
/usr/sbin/pip
# which python
/usr/bin/python

1.python2.7 -m pip install virtualenv
Reference Python official document
2.You can Install virtualenv for any python interpreter and use for a different python interpreter like:-
sudo pip3 install virtualenv
and then if you want to use python2.7 python interpreter then run following command to make virtualenv:-
virtualenv --python=/usr/bin/python2.7 virtualenv_name
NOTE :- python2.7 interpreter has to be present in /usr/bin/* folder
For more on above command see this answer

Related

Connection between pip and python [duplicate]

I'm now currently using Python on Ubuntu 15.10.
But in my OS, I have many different python versions installed:
Python (2.7.9)
Python3 (3.4.3)
Python3.5
PyPy
So, it got messy with the versions of the packages in different environments. For example, if I run:
pip3 install django
But in fact, I cannot import django inside python3.5.
Is there any efficient way to call the correct version of pip?
Note:
Don't suggest that I use virtualenv, I know about it and am seeking another solution.
Finally I found the solution myself, see the Docs:
https://docs.python.org/3/installing/index.html?highlight=pip#work-with-multiple-versions-of-python-installed-in-parallel
Just call:
pythonXX -m pip install SomePackage
That would work separately for each version of installed python.
Also, according to the docs, if we want to do the same thing in windows, the command is a bit different:
py -2 -m pip install SomePackage # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4
How about using pyenv?
You can switch the version.
$ pyenv install 2.7.X
$ pyenv install 3.5.X
$ pyenv local 2.7.X
$ pyenv global 3.5.X
This solution worked for me:
sudo python2.7 -m pip install [package name]
Why not using anaconda?
If you use conda, you can easily create/manage virtual env. For example, if you have root env python 3.4 and py27 env for python 2.7, you can easily switch between them use command source activate [env]
source activate py27
conda install SomePackage

How to install multiple python and pip versions on linux

I'm trying to install multiple python versions in linux, but asdf or pyenv get stuck with an error:
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?
apt install libssl or apt install libssl1.0 didn't solved...
I already have version 2.7 and 3.8 by default.
Then I've stalled version 3.4 by downloading its source and building manually. If i try to create a virtual environment like python3.4 -m venv myenv, i can activate python --version shows the correct one:
$ python --version
Python 3.4.0
but pip still referenced to python 3.8:
(myenv)$ pip --version
pip 21.0.1 from ~/.local/lib/python3.8/site-packages/pip (python 3.8)
/usr/local/lib/python3.4/site-packages/ only has README file, and i can't figure out how to properly set this

Migrating an env to a new python version

I have a python app build on python 3.4
Due to an OS update I have to update this app to python 3.5.
First step I did was this to create new python version path in env
python3 -m venv --upgrade ENV_DIR
But how can I reinstall the needed packages from requirements.txt into this new 3.5 path?
When I do
pip install -r requirements.txt
it says all packages are already installed.
What am I doing wrong? Can someone help me?
Thank you
You can also use pyenv for multiple python versions.
Install pyenv
check available python versions using pyenv.
pyenv install --list | grep " 3.[45]"
Install the version you want. Here you will need 3.4
pyenv install 3.4(Any version you want)
Find available versions
pyenv versions
Go to local folders where your project is there. Set python 3.4 version
pyeve local 3.4
Check python version. It would be 3.4
python --version
Create virtual Environment
python -m venv venv
Install all packages using
pip install -r requirements.txt

Error with installing textgenrnn

I`m trying to install library called textgenrnn, followed by this in docs:
pip3 install textgenrnn
Got error that my Python version is 2.7:
Collecting textgenrnn
Using cached https://files.pythonhosted.org/packages/00/69/5d995322502f8a33d408c547a6dbf00e74d4434ecc1b704b684260739b21/textgenrnn-1.3.1.tar.gz
textgenrnn requires Python '>=3' but the running Python is 2.7.15
You are using pip version 9.0.3, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
I also check my Python version using this command:
python -V
and get
Python 3.6.5 :: Anaconda, Inc.
Update: After uninstalling anaconda I got this:
dmitriy#dmitriy-PC:~$ python -V
Python 2.7.15rc1
dmitriy#dmitriy-PC:~$ python3 -V
Python 3.6.5
My first think is to delete or disable python 2. But after googling I decided that python2 is impossible to uninstall or disable
For making python3 as your default by setting the alias command or you can configure the .bashrc file for conda and set the path for conda
alias python='python3' #create python3 as your default
get install via pip
Another Method:
create a environment after you install conda and you will get a python3 environment. From there you can install via pip
Either of the method should work here.
Just install library from source.
wget https://files.pythonhosted.org/packages/00/69/5d995322502f8a33d408c547a6dbf00e74d4434ecc1b704b684260739b21/textgenrnn-1.3.1.tar.gz
tar -xvzf textgenrnn*.tar.gz
cd textgenrnn-1.3.1
sudo python3 setup.py install
That`s all!

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