I have installed and been using Pyenv for some time now, utilizing the Pyenv Virtualenv plugin to create venvs and activating automatically by including a .python-version as per the plugin docs.
It seems to not always work as expected, though. Recently upgraded OSX to Big Sur.
Pip seems to be installing packages globally, rather than into the venv:
pyenv versions
system
2.7.17
3.8.5
3.9.5
* 3.9.5/envs/project_venv (set by /Volumes/path_to_my_project/.python-version)
fulfill
So far, so good.
pyenv which pip
/Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/bin/pip
As I would expect.
Here's where I'm confounded:
Outside of the venv:
$ pyenv which pip
/Users/mikekilmer/.pyenv/versions/3.8.5/bin/pip
$ pip list -v
Package Version Location Installer
----------------- --------- ------------------------------------------------------------- ---------
# all the global pip packages...
defusedxml 0.7.1 /usr/local/lib/python3.9/site-packages pip
# No Django, which is good
idna 2.10 /usr/local/lib/python3.9/site-packages pip
Inside of the venv:
$ pip install django
# installation output...
$ pip list -v
# abridged
Django 3.2.5 /usr/local/lib/python3.9/site-packages pip
## That's not supposed to be in the global packages!!!
And of course Django is also in the global pip list.
But wait...
Look at this:
$ /Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/bin/pip install django
# installation output...
$ /Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/bin/pip list -v
asgiref 3.4.1 /Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/lib/python3.9/site-packages pip
Django 3.2.5 /Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/lib/python3.9/site-packages pip
pip 21.1.1 /Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/lib/python3.9/site-packages pip
pytz 2021.1 /Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/lib/python3.9/site-packages pip
setuptools 56.0.0 /Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/lib/python3.9/site-packages pip
sqlparse 0.4.1 /Users/mikekilmer/.pyenv/versions/3.9.5/envs/project_venv/lib/python3.9/site-packages pip
Now it's installed to the correct place.
But that's not the way it's supposed to work, is it? It seems that somehow, pyenv thinks that it's using the venv pip, but isn't.
When I run which pyenv, it returns this function:
pyenv () {
local command
command="${1:-}"
if [ "$#" -gt 0 ]
then
shift
fi
case "$command" in
(activate | deactivate | rehash | shell) eval "$(pyenv "sh-$command" "$#")" ;;
(*) command pyenv "$command" "$#" ;;
esac
}
I am not finding that code in the pyenv repository and I'm using the latest master (pyenv 2.0.3) via Homebrew.
Any insights?
Update
Seems that pip is symlinked to /usr/local/bin/pip3:
$ which pip
pip: aliased to /usr/local/bin/pip3
While pip3 points correctly to the pyenv location:
$ which pip3
/Users/mikekilmer/.pyenv/shims/pip3
$PATH starts like this:
/var/folders/62/38l28z6j12s9lqp6nnhmrlhc0000gn/T//zsh-501/bin:/usr/local/Cellar/pyenv-virtualenv/1.1.5/shims:/Users/mikekilmer/.pyenv/shims:/Users/mikekilmer/.pyenv/bin
The first directory is empty and seems to be part of the Darwin cache directory:
$ getconf DARWIN_USER_CACHE_DIR
/var/folders/62/38l28z6j12s9lqp6nnhmrlhc0000gn/C/
I'm not sure how it got appended to my PATH (by zsh?) or if it could somehow be making the shell think it should find pip elsewhere than with pyenv.
I was similarly confused at some point. Turned out that I installed / upgraded python and pip with different methods (brew, easy_install, curl, sudo ...). That's why my paths got messed up.
Removing pip everywhere and reinstalling it helped.
When I run which pyenv, it returns this function:
pyenv () {
local command
command="${1:-}"
if [ "$#" -gt 0 ]
then
shift
fi
case "$command" in
(activate | deactivate | rehash | shell) eval "$(pyenv "sh-$command" "$#")" ;;
(*) command pyenv "$command" "$#" ;;
esac
}
You can add pyenv to the PATH in your ~/.zshrc or ~/.zshenv in order to get the correct path.
echo 'export PATH=$HOME/.pyenv/bin:$PATH' >> ~/.zshenv
I hope for a more complete explanation, but installing with pip3 solved the issue.
Related
I am using pip in a cygwin environment under Win10.
$ pip --version
pip 9.0.1 from c:\python36\lib\site-packages (python 3.6)
I had coded a bash function molt to do python package maintenance in the past
molt () {
local i
for i in $(pip list | cut -d' ' -f1)
do
case $i in
pip|pywin32)
continue
;;
*)
pip install -qU $i
;;
esac
done
}
It worked until last week.
$ molt
Command "python setup.py egg_info" failed with error code 1 in C:\cygwin64\tmp\pip-build-lbe5xcu8\Package\
Usage:
pip install [options] <requirement specifier> [package-index-options] ...
pip install [options] -r <requirements file> [package-index-options] ...
pip install [options] [-e] <vcs project url> ...
pip install [options] [-e] <local project path> ...
pip install [options] <archive url/path> ...
no such option: --------------
I suspect pip must have changed but couldn't figure out what change broke the code. Please help.
It turned out that pip list returns
Package Version
-------------- -------
pip 9.0.1
setuptools 38.5.2
which leads pip install -qU Package -------------- according to the molt function, causing the error. The culprit is the new pip list output format (I forgot that I have changed the default legacy to columns a while ago). Restoring the default format option fixes the error, i.e.,
pip list --format=legacy
I uninstalled pip, and I installed pip3 instead. Now, I want to use pip3 by typing pip only. The reason is I am used to type pip only and every guide uses the pip command, so every time I want to copy and paste commands, I have to modify pip to pip3 which wastes time. When I type pip I have an error which is pip: command not found which means pip command is not taken. Is it possible to make pip points to pip3?
You can use pip3 using the alias pip by adding alias to your .bashrc file.
alias pip=pip3
or by adding a symlink named pip to your $PATH, which points to the pip3 binary.
If there is no ~/.bashrc in your home directory on macOS, inputting
alias pip=pip3
in your ~/.zprofile file has the same effect.
Rather than manually creating your own alias in bash and hoping this doesn't conflict with anything, most package managers should allow you to register the version you wish to use whilst maintaining dependencies.
For instance on Linux:
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
Or on Mac (MacPorts):
port select --set pip pip3
Solution 1
Check which version pip is pointing to
pip --version
pip 18.0 from /usr/lib/python2.7/site-packages/pip (python 2.7)
If your pip is pointing to pip2, locate where is the pip "binary".
which pip
/usr/bin/pip
This is a simple python script:
cat /usr/bin/pip
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
So just change the shebang from #!/usr/bin/python2 to #!/usr/bin/python3.
Now pip is pointing to pip3.
pip --version
pip 18.0 from /usr/lib/python3.6/site-packages/pip (python 3.6)
Solution 2
Remove /usr/bin/pip make make a symbolic link from the wanted pip version to it instead.
sudo rm /usr/bin/pip
sudo ln -s /usr/bin/pip3.6 /usr/bin/pip
Since you uninstalled pip, this solution assumes you're only going to use pip3.
Open your terminal.
Create a simple link. To do that type:
sudo ln -s /usr/bin/pip3 /usr/bin/pip
Now when you type pip, it will invoke pip3.
Check that it worked by typing pip --version
pip --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
You're all set!
You can install pip after install pip3 by the below command:
pip3 install --upgrade pip
after that:
~ pip --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
You can write pip for pip3 after changing bashrc file in the home directory.
In mac -
Open bashrc file -
vim ~/.bashrc
Add this line at the end of the file -
alias pip="pip3"
Close the file. Don't forget to source this file in the terminal by
source ~/.bashrc
You are good to go. Now, whenever you will use pip in any command. it will be interpreted as pip3
You can check it by running the command -
pip --version
Pip is installed in /usr/bin/. If you don't have pip at all, I would suggest to install pip3 only. Make sure you don't need older version.
You can check available pip versions using following command.
ls /usr/bin/pip*
If you have multiple pip you need to prioritize your pip versions. I had only pip3 so I add it to the first priority. You can use following command and it is done.
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
You will get output as :
update-alternatives: using /usr/bin/pip3 to provide /usr/bin/pip (pip) in auto mode
Test now:
pip --version
You will get:
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
If you have other version for python2.7, you can use same update command and change last digit 1 to 2. This will make it second priority.
I believe one shouldn't do such a thing. Actually I would argue it's even better to not use the pip, pip3, etc. scripts ever. Instead one should always prefer the more explicit and surefire way of using pip's executable module for one specific Python interpreter:
path/to/pythonX.Y -m pip somecommand
References:
https://snarky.ca/why-you-should-use-python-m-pip/
https://snarky.ca/a-quick-and-dirty-guide-on-how-to-install-packages-for-python/
This can be done by simply creating an alias for the command.
To create an alias just type
$alias new_command="existing_command"
In your case,
$alias pip="pip3"
Although this isn't permanent. OT make it permanent edit your bashrc file
$ vim ~/.bashrc
an to the end of it append the line.
$alias pip="pip3"
It depends on how you manage your python versions (system, brew, pyenv, ...) and which python installation you are currently using.
For example if you use brew then creating a simlink is a good option:
ln -s -f /usr/local/bin/pip3 /usr/local/bin/pip
validate that pip uses the correct version:
which pip
will give you
/usr/local/bin/pip
Copy the pip3 file and rename as pip:
sudo cp /usr/bin/pip3 /usr/bin/pip
pip --version
and
pip3 --version
Works now.
How can I use pip in oh-my-zsh? I was trying to install nltk through pip, but it told me zsh: command not found: pip. When I check plugins under .oh-my-zsh/custom/plugins, there is a folder named pip. I don't know what the problem is.
Edit:
$ echo $PATH
/home/xxx/bin:/usr/local/bin:/home/xxx/bin:/home/xxx/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
$ type pip
pip is an alias for noglob pip
Maybe you have installed both python2 and python3. python3 may have been installed later.
You may try to use pip3 instead of pip.
First, input the command:
pip3 -V
If you see the version, the pip3 can be used.
Then you can input command line to install nltk:
pip3 install nltk
I got a way to help you use pip in zsh. We can use nano to edit files. In nano, ctrl+X to save and exit
In the ~ directory, input the command:
nano .bash_profile
You may see some codes like:
# Setting PATH for Python 3.5
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
Copy them and paste them to the end of .zshrc file by using command:
nano .zshrc
Then input the command:
pip -V
If you see the version, pip can be used.
In case you do
which pip
and it doesn't show the path, just do
which pip3
This will print the path which is /usr/local/bin/pip3
Then do open ~/.zshrc or nano ~/.bash_profile.
Make alias for pip like:
alias pip=/usr/local/bin/pip3
N.B: You copy that line above and paste in your .zshrc file.
After do source ~/.zshrc and close .zshrc
For me it's working to do
python -m pip install [package_name]
instead of
pip install [package_name]
If you installed python3.x, you should run with pip3(not pip)
So you are using oh-my-zsh framework for zsh or Z shell.
First, try the command:
pip3 -V
If you get something like this below, that means you have the pip3 package already and must be having python3 as well.
pip 22.0.4 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
Then edit your .zprofile instead of .bashprofile as you are using zsh. This is the command.
nano ~/.zprofile
Then it should have the two alias like this.
# Setting PATH for Python 3.10
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
export PATH
alias python=python3
alias pip=pip3
Make sure you save it. Exit and Re-open you terminal. Type the command:
pip -V
It should have the same result as the pip3 -V like this:
❯ pip -V
pip 22.0.4 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
Then you can use pip or pip3 interchangeably for installing your nltk package like this.
pip install nltk
I'm on MacOS and use ZSH. It seems pip 2.7 can't be found, although it is installed. I believe my paths to "pip" are linked wrong (I also have python3 and pip3 installed via brew).
To get around the issue I created an alias. If you don't have an .aliases file, create one in your homedir. Then open the file:
nano ~/.aliases
and add:
## PIP for python2.7 ##
alias pip="python -m pip "
You need to tell ZSH to pick up the alias file (assuming you don't have this setup already). Open your .zshrc:
nano ~/.zshrc
The add the following near the bottom of the file:
[ -f "$HOME/.aliases" ] && source "$HOME/.aliases"
From the terminal, run:
source ~/.zshrc
Or quit your terminal and reopen it.
Now you can run:
pip install <command>
Edit your rc file:
vim ~/.zshrc
Find the config plugins and delete the pip entry.
In a new terminal:
which pip
This will show you the real path of pip
If you're running into this issue, it probably is due to versioning complications. Python 2 versus Python 3 on your OS may be resolving unexpectedly. Below is a quick workaround to get you to functioning behavior.
Try using the below for Python 2:
python -m pip install <command>
Try using the below for Python 3:
pip3 install <command>
In my case my OS was Ubuntu 20.04 and the pip doesn't come with python.
So, i've installed pip through the command
sudo apt install python3-pip
and I'm done.
To ensure run pip -V or pip3 -V
My pip script is missing for some reason, so I have to install it.
$ python -m ensurepip --upgrade
More methods can be found here:pip installation
You should consider upgrading.
Enter this in your terminal
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10 -m pip install --upgrade pip
and then...
Type: pip -V
I'm running openSUSE 13.1 and I'm trying to install Python 3.5.1 in an virtualenv, because the system default is 3.3.5 and cannot be further updated with yast.
I have downloaded the source, stored it in folder ~/pysrc35 and created a new directory ~/localpython and installed it
./configure --prefix=/home/<user>/.localpython
make
make install
the python3 executable there works and the localpython/bin looks like
2to3 easy_install-3.5 idle3.5 pip3.5 pydoc3.5 python3.5 python3.5m python3-config pyvenv-3.5
2to3-3.5 idle3 pip3 pydoc3 python3 python3.5-config python3.5m-config pyvenv
Now I want to use pip, which seems to have been included in the installation, but when I run it I get the error:
ImportError: No module named 'pip'
I have already set $PYTHONHOME to /home/<usr>/localpython/bin/python3 and $PYTHONPATH to /home/<usr>/localpython/lib/python3.5/:/home/<usr>/localpython/lib64/python3.5/:/home/<usr>/localpython/include/python3.5m/, because I got errors on that before, but now I'm stuck with pip.
A great solution for this was using pyenv. This tool makes installing different python versions and virtual environments super easy. The only prerequisite is probably just having git installed. Then you clone it
git clone git://github.com/yyuu/pyenv.git ~/.pyenv
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
and run
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
eval "$(pyenv virtualenv-init -)"
in terminal. For Linux distributions other than openSUSE or Ubuntu ~/.bashrc might have to be replaced by
~/.bash_profile
. After restarting the terminal, run
pyenv install 3.5.x
to install whatever version you want (except 3.5.1 is not yet available). Then you should switch to your desired Python version, e.g. with
pyenv shell 3.5.x
to set the version for the current terminal session.
After that a virtual environment can be created and activated by
pyenv virtualenv /path/to/venv
pyenv activate /path/to/venv
See also this video for a comprehensive tutorial on using pyenv and in conjunction with django.
I've read the Pip related responses and still have this question. I'm trying to install pip on Mac OS X 10.6.8. In brief, I run sudo python get-pip.py and get an error that the pip Requirement is already up to date.
But I don't see a 'pip' executable file in the expected directory and can't run pip. Should there be a file called pip in the target pip folder?
More detail:
I'm following directions at: http://docs.python-guide.org/en/latest/starting/install/osx/
1) Used ez_install to install setuptools
2) bashed:
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py $
sudo python get-pip.py
Response is
Requirement already up-to-date: pip in /Library/Python/2.6/site-packages
... I can see a lot of files in this directory
3) I then try to use pip:
/Library/Python/2.6/site-packages/pip install virtualenv
Get back > pip: is a directory
4) I try
/ Library/Python/2.6/site-packages/pip/pip install virtualenv
-bash: /Library/Python/2.6/site-packages/pip/pip: No such file or directory
I look in the above path and can see the pip folder, with numerous files including __main__ and __init__ but no file named pip. Should there be one? if so, how might I clear out the old install and reinstall?
Thanks for any advice!
SL
Here's how I successfully installed pip from the Github repositories on Max OS X 10.9:
Get pip repository:
$ git clone https://github.com/pypa/pip
Perform setup:
$ cd pip
$ sudo python setup.py install
That's it. A test run should give something like:
$ pip -V
pip 1.6.dev1 from /Library/Python/2.7/site-packages/pip-1.6.dev1-py2.7.egg (python 2.7)
And my PATH environment variable is set to:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
echo export PATH="/usr/local/bin:$PATH" >> ~/.bash_profile