Pip install python package for current user - python

Recently I delete some files related to python 2.7 and now I'm crazy. I want to use pip to install python package for current user rather global user.
➜ ~ where pip
/usr/local/bin/pip
➜ ~ pip -V
pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)
In the past, I can install requests easily by pip install requests. But now I have to sudo pip install requests or pip install --user requests.
I think because of pip's location I have to install python package within /Library/Python/2.7/..... As you see, it needs root permission. I know venv can help me but now I want to know how to intall python package for current user.

Use pip's --user option:
pip install --user package
According to pip's documentation:
--user
Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%Python on Windows. (See the Python documentation for site.USER_BASE for full details.)

Related

how to install Pip3 on windows 10?

I installed Python 3.10 today but when I try to run pip or pip3, the command prompt gives me an error. I tried following the instructions that the top answer in this question said. My complete path to the python interpreter is this:
C:\Users\User\AppData\Local\Microsoft\WindowsApps\python3.exe
In the WindowsApps directory, I'm supposed to have a Scripts folder. Strangely enough, I don't. Can someone please help me?
Check if pip3 is already installed
pip3 -v
if it is installed the output should be like that
C:\Python38\python.exe -m pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
...
...
Pip3 Upgrade
python -m pip3 install --upgrade pip
Pip3 Downgrade
python -m pip3 install pip==19.0
You can try python -m pip to use pip if it is installed.
If pip is not installed, you can always use python -m ensurepip --upgrade to install pip for your python installation.
Take a look at the following post
How can I install pip on Windows?
py -3 -m ensurepip

Does `pip install -U pip -r requirements.txt` upgrade pip before installing the requirements?

It seems to be common practice to set up a Python virtual environment using some variant of the following:
python -m venv venv && source ./venv/bin/activate
python -m pip install -U pip -r requirements.txt
What I hope the above command does is:
Upgrade pip first
Run the installation of the packages in requirements.txt
However, what actually seems to happen is:
Collects all packages, including newest version of pip
Installs them all together
The original/outdated version of pip is what actually runs the installs
And the new version of pip is not used until after this command
Question(s)
Is it possible to have pip upgrade itself and then install a requirements file, in one command?
Would this infer any particular benefits?
Should I switch to the following?
python -m venv venv && source ./venv/bin/activate
python -m pip install -U pip
python -m pip install -r requirements.txt
What is the optimal method to install requirements files?
I see people sometimes installing/upgrading wheel and setuptools as well
The answers to your questions are:
No. pip doesn't currently treat itself as a special dependency, so it doesn't know to install then execute itself, which is what it would need to do to overcome the problems you observed.
Updating pip in a separate step is indeed the recommended way to proceed.
You may from time to time see pip issue a message advising that a newer version is available. This happens a lot if you create them from a python with an outdated pip.
I had a situation similar to yours, I needed to first upgrade pip and then install a bunch of libraries in a lab that had 20 PCs. What I did was writing all the librarie's name in a requirements.txt file, then create a .bat file with two commands:
`python -m pip install --upgrade pip`
`pip install -r requirements.txt`
The first command for upgrading pip and the second one for installing all the libraries listed in the requirements.txt file.

"ImportError: cannot import name main" after upgrading to pip 10.0.0 for Python version 2.7.12 - Only one version of Python is installed

I got a message in my terminal while install a software : You are using pip version 8.1.1, however version 10.0.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
So I upgraded pip version: $ pip install --upgrade pip
Now I am getting this error:
~$ pip install ipython
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip import main
ImportError: cannot import name main
TL;DR
The 'ideal' solution (Ubuntu/Debian way):
$ python -m pip uninstall pip to uninstall the new pip 10 and retain your Ubuntu/Debian-provided patched pip 8. For a system-wide installation of modules use apt wherever possible (unless you are in a virtualenv), more on it below. In older Ubuntu/Debian versions, always add --user flag when using pip outside of virtualenvs (installs into ~/.local/, default in python-pip and python3-pip since 2016).
If you still want to use the new pip 10 exclusively, there are 3 quick workarounds:
simply re-open a new bash session (a new terminal tab, or type bash) - and pip 10 becomes available (see pip -V). debian's pip 8 remains installed but is broken; or
$ hash -d pip && pip -V to refresh pip pathname in the $PATH. debian's pip 8 remains installed but is broken; or
$ sudo apt remove python-pip && hash -d pip (for Python 3 it's python3-pip) -- to uninstall debian's pip 8 completely, in favor of your new pip 10.
Note: You will always need to add --user flag to non-debian-provided pip 10, unless you are in a virtualenv! Your use of pip 10 system-wide, outside of virtualenv, is not really supported by Ubuntu/Debian. Never sudo pip!
Details:
https://github.com/pypa/pip/issues/5221#issuecomment-382069604
https://github.com/pypa/pip/issues/5240#issuecomment-381673100
Ubuntu 16.04 with Python 2.7.12
Introduction:
Ironically, despite being suggested by pip itself to do such an upgrade via the pip install --upgrade pip command explicitely in the terminal (ugh!), upgrading it is not recommended in prepackaged GNU/Linux distributions. Ubuntu generally expects using APT package manager for any system-wide Python module updates/installs (including of pip itself), for example:
$ sudo apt-get update (resync Ubuntu package index files from sources)
$ apt-cache search <python-package-name> (full text-search on all available packages)
$ apt-cache show <python-package-name> (displays the package description)
$ sudo apt-get install python-numpy python-scipy python-matplotlib (easily installs heavy modules for data science, resolving all system dependencies automatically)
$ sudo apt-get install ipython (installs the IPython-notebook you were looking for)
$ sudo apt-get install python-pip (installs/upgrades pip to the latest version available in Ubuntu repository – usually slightly behind pypi.org, but it doesn't matter)
If you ever have to use pip install command on Ubuntu/Debian instead of apt-get install, please make sure it runs isolated and does not change the default system-wide Python packages (never use sudo with pip) – more on this below.
ImportError: cannot import name main
The error is caused by the pip install --upgrade pip command: that installs the latest pip version 10 alongside Ubuntu's default pip version from python-pip debian package from the OS distribution (the system Python installation), completely bypassing Ubuntu apt subsystem. It breaks the Ubuntu's default pip: the debian-patched launcher script from python-pip (system-installed to /usr/bin/pip*) tries to do import main() from your newly installed pip 10 library, but with a different import path, so it fails.
This error is discussed in more detail in a developer thread of the pip issue tracker, including a few proposed solutions, such as:
The $ hash -d pip command: when hash is invoked, the full pathname of pip is determined by searching the directories in $PATH and remembered. Any previously-remembered pathname is discarded. The -d option causes the shell to "forget" the remembered location of the given package name; or
Similarly, you can simply re-open a new bash session (a new terminal tab) to refresh pip pathname in $PATH; or
You could just use pip2 command (or pip3 for Python 3) instead of pip to invoke the older system-installed pip script /usr/bin/pip2 , whereas any pip launcher script located in $HOME/.local/bin dir (pip, pip2, pip2.7) will start your new user-installed pip 10 version;
You can also use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip, for example:
$ python2 -m pip install --user SomePackage # default Python 2
$ python2.7 -m pip install --user SomePackage # specifically Python 2.7
That is handy if you have several versions of Python and need an extension from PyPI for a specific Python version. The --user switch is only required when pip is used outside of virtualenv.
Or, uninstall one of the two pips – either user-installed or system-installed – to resolve the conflict:
$ python -m pip uninstall pip – to remove your manually-installed pip in favour of the previously installed Ubuntu-shipped version from python-pip debian package (python3-pip for Python 3); it is slightly older, but it finds and installs latest modules from PyPI just fine, and has a working pip command in the $PATH by default; or
$ sudo apt-get remove python-pip – to uninstall Ubuntu-provided pip in favour of your latest pip 10; if it is not accessible via the short pip command, just add your $HOME/.local/bin directory to your $PATH environment variable to use pip command (see above).
Note: Ubuntu 16.04 pip v8.1.1 and the latest pip v10.0.1 produce exactly the same PyPI index search results and can pull the same module versions;
Finally, you could ignore both pips altogether in favor of APT, and install Python packages system-wide from Ubuntu repo with:
$ apt search <python-package> # e.g. python-pandas
$ apt show <python-package> # e.g. python-flask
$ sudo apt install <python-package>
Packages prefixed with python- are for Python 2; with python3- are for Python 3.
Installation via apt-get may be what you need, in fact, python-packages from Ubuntu repository are preferred whenever possible, particularly in case of heavy system dependencies or when used system-wide. Of course, the amount of Python packages in Ubuntu repository (few thousand!) is relatively smaller compared to PyPI (and have only one version of them), because any OS repository is lagging slightly behind PyPI versions. But the upside of APT is that all the Ubuntu-provided packages underwent integration testing within Ubuntu, plus apt-get quickly resolves heavy dependencies like C extensions automatically. You will always get any required system libraries as part of the apt install, but with pip you have no such guarantees.
APT may not be an option, however, if you really need the latest (or certain older) package version, or when it can only be found at PyPI, or when modules need to be isolated; then pip is indeed more appropriate tool. If you use pip install command on Ubuntu instead of apt-get install, please ensure that it runs in an isolated virtual development environment, such as with virtualenv (sudo apt-get install python-virtualenv), or a built-in venv module (available in python3 only), or at a per-user level (pip install --user command option), but not system-wide (never sudo pip!).
Note: Using sudo pip command (with root access) should be avoided, because it interferes with the operation of the system package manager (apt) and may affect Ubuntu OS components when a system-used python module is unexpectedly upgraded, particularly by dependencies on another pip package. It is advised to never use Pip to change your system-wide Python packages, as these are managed by apt-get on Ubuntu.
I implemmented #catalinpopescu response from ImportError: cannot import name main when running pip --version command in windows7 32 bit
Find pip's path:
$ which pip
Modify file (choose your favorite editor):
$ sudo nano `which pip`
Then modify lines #catalinpopescu said:
Comment/replace lines:
from pip import main
sys.exit(main())
to:
from pip import __main__
sys.exit(__main__._main())
Immediately I upgrade to Pip Version 10.0.1, which appears it hasn't have this error.
try to upgrade the system pip
sudo pip install --upgrade pip
pip install --upgrade pip
this may be useful

Upgrade pip in Amazon Linux

I wanted to deploy my Python app on Amazon Linux AMI 2015.09.1, which has Python2.7 (default) and pip (6.1.1). Then, I upgraded the pip using the command:
sudo pip install -U pip
However, it seemed broken, and showed the message when I tried to install packages:
pkg_resources.DistributionNotFound: pip==6.1.1
I found out that pip remove the previous files located in /usr/bin/, and installed the new one in /usr/local/bin. Thus, I tried to specify the location by using the command:
sudo pip install -U --install-option="--prefix='/usr/bin'" pip
Nevertheless, it still installed the new one in /usr/local/bin. In addition to that, pip could not work well with sudo although it successfully installed. The error message :
sudo: pip2.7: command not found
Is there a way to properly manage pip?
Try:
sudo which pip
This may reveal something like 'no pip in ($PATH)'.
If that is the case, you can then do:
which pip
Which will give you a path like /usr/local/bin/pip.
Copy + paste the path to pip to the sbin folder by running: sudo cp /usr/local/bin/pip /usr/sbin/
This will allow you to run sudo pip without any errors.
Struggled with this for a while. Here's what I've found:
ec2_user finds the pip executable, but has a module import error due to other having no read/execute permissions on the pip folders in the /usr/local/lib/python2.7/site-packages folder. This is actually okay, since in most cases, pip installs fail when not run as root anyway.
sudo cannot find pip.
Entering root with sudo su - allows pip to be run without issue.
The reason sudo pip stops working after the upgrade, is because the executable (or symbolic link) is removed from /usr/bin. However, what remains is a file called pip-27, which contains the following:
#!/usr/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==6.1.1','console_scripts','pip2.7'
__requires__ = 'pip==6.1.1'
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('pip==6.1.1', 'console_scripts', 'pip2.7')()
)
So, that's where our error comes from, as the upgrade clearly doesn't clean this file up. Not entirely clear on where the name translation from pip to pip-2.7 occurs.
As mentioned in another answer, pip now exists in /usr/local/bin after the upgrade, which is no longer in the sudo secure path. You can add this path to the secure_path variable by running sudo visudo. Another alternative, if you'd prefer to not add that path to your secure_path is to make a symbolic link to the new pip executable in /usr/bin.
The problem is partly answered by your question. The Amazon AMI doesn't consider /usr/local/bin to be part of the root account's PATH. You can fix this by updating the root account's ~/.bashrc to include it.
Something like this...
export PATH=$PATH:/usr/local/bin
After struggling with this for hours and reading comments
which pip gave /usr/bin/pip , but the actual pip was located at /usr/local/bin/pip due to pip upgrade and clean up was not complete
So removing the pip in /usr/bin/
sudo rm /usr/bin/pip
and also adding the new pip to your export path
vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/bin
exit terminal, and login back
which pip should give /usr/local/bin/pip
pip install --upgrade pip
This works for me
sudo /usr/local/bin/pip install --upgrade pip
To add to angelokh
sudo `which pip` install --upgrade pip
I think the best strategy in this case is to manage pip is as part of a virtual environment using virtualenv rather than messing with the system-level version.
If you're OK with that, here's the basic idea:
Install the version of virtualenv packaged with the version of pip you are looking to upgrade to to the system-level pip (e.g. virtualenv==15.1.0 comes with pip==9.0.1):
$ sudo pip install -U virtualenv==15.1.0
You are using pip version 6.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting virtualenv==15.1.0
Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 135kB/s
Installing collected packages: virtualenv
Found existing installation: virtualenv 12.0.7
Uninstalling virtualenv-12.0.7:
Successfully uninstalled virtualenv-12.0.7
Successfully installed virtualenv-15.1.0
I used the virtualenv release notes to find out which version of pip corresponds to which version of virtualenv.
Create the virtual environment:
$ virtualenv myenv
New python executable in /home/ec2-user/myenv/bin/python2.7
Also creating executable in /home/ec2-user/myenv/bin/python
Installing setuptools, pip, wheel...done.
Activate the virtual environment and confirm the version and location of the upgraded pip:
$ source myenv/bin/activate
(myenv) $ pip -V
pip 9.0.1 from /home/ec2-user/myenv/local/lib/python2.7/dist-packages (python 2.7)
(myenv) $ which pip
~/myenv/bin/pip
This should allow you to install packages to this virtualenv using the pip version of your choice, without the need for sudo.
I think you've didn't installed the pythonXX-pip package.
I've upgraded mine straight to Python3.4, these commands works for me.
sudo su
yum install python34
yum install python34-pip

How do I update/upgrade pip itself from inside my virtual environment?

I'm able to update pip-managed packages, but how do I update pip itself? According to pip --version, I currently have pip 1.1 installed in my virtualenv and I want to update to the latest version.
What's the command for that? Do I need to use distribute or is there a native pip or virtualenv command? I've already tried pip update and pip update pip with no success.
pip is just a PyPI package like any other; you could use it to upgrade itself the same way you would upgrade any package:
pip install --upgrade pip
On Windows the recommended command is:
python -m pip install --upgrade pip
The more safe method is to run pip though a python module:
python -m pip install -U pip
On windows there seem to be a problem with binaries that try to replace themselves, this method works around that limitation.
In my case my pip version was broken so the update by itself would not work.
Fix:
(inside virtualenv):easy_install -U pip
I tried all of these solutions mentioned above under Debian Jessie. They don't work, because it just takes the latest version compile by the debian package manager which is 1.5.6 which equates to version 6.0.x. Some packages that use pip as prerequisites will not work as a results, such as spaCy (which needs the option --no-cache-dir to function correctly).
So the actual best way to solve these problems is to run get-pip.py downloaded using wget, from the website or using curl as follows:
wget https://bootstrap.pypa.io/get-pip.py -O ./get-pip.py
python ./get-pip.py
python3 ./get-pip.py
This will install the current version which at the time of writing this solution is 9.0.1 which is way beyond what Debian provides.
$ pip --version
pip 9.0.1 from /home/myhomedir/myvirtualenvdir/lib/python2.7/dist-packages (python 2.7)
$ pip3 --version
pip 9.0.1 from /home/myhomedir/myvirtualenvdir/lib/python3.4/site-packages (python 3.4)
In case you are using venv any update to pip install will result in upgrading the system pip instead of the venv pip. You need to upgrade the pip bootstrapping packages as well.
python3 -m pip install --upgrade pip setuptools wheel
for windows,
go to command prompt
and use this command
python -m pip install -–upgrade pip
Dont forget to restart the editor,to avoid any error
you can check the version of the pip by
pip --version
if you want to install any particular version of pip , for example version 18.1 then use this command,
python -m pip install pip==18.1
pip install --upgrade pip
In UBUNTU 18.04 I got the following error when I execute the above command:
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/bin/pip'
Consider using the `--user` option or check the permissions.
The below command solves my problem:
pip install --upgrade pip --user
Upgrading pip using 'pip install --upgrade pip' does not always work because of the dreaded cert issue: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version
I like to use the one line command for virtual envs:
curl https://bootstrap.pypa.io/get-pip.py | python -
Or if you want to install it box wide you will need
curl https://bootstrap.pypa.io/get-pip.py | sudo python -
you can give curl a -s flag if you want to silence the output when running in an automation script.
To get this to work for me I had to drill down in the Python directory using the Python command prompt (on WIN10 from VS CODE). In my case it was in my AppData\Local\Programs\Python\python35-32 directory. From there now I ran the command...
python -m pip install --upgrade pip
This worked and I'm good to go.
For linux
python3 -m pip install --upgrade pip
For windows:
Type Command Prompt in the Windows search box
In the Command Prompt, type cd\
Press Enter, and you’ll see the drive name C:\>
Locate your Python application path, which is the folder where you originally installed Python
Here is an example of a Python application path:
C:\Users\Ron\AppData\Local\Programs\Python\Python39
Once you retrieved the Python application path, type the following command in the Command Prompt:
cd followed by your Python application path
For our example:
C:\>cd C:\Users\Ron\AppData\Local\Programs\Python\Python39
Press Enter
Type python -m pip install --upgrade pip and press Enter
In my case this worked from the terminal command line in Debian Stable
python3 -m pip install --upgrade pip
Open Command Prompt with Administrator Permissions, and repeat the command:
python -m pip install --upgrade pip
pip version 10 has an issue. It will manifest as the error:
ubuntu#mymachine-:~/mydir$ sudo pip install --upgrade pip
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip import main
ImportError: cannot import name main
The solution is to be in the venv you want to upgrade and then run:
sudo myvenv/bin/pip install --upgrade pip
rather than just
sudo pip install --upgrade pip
I was in a similar situation and wanted to update urllib3 package.
What worked for me was:
pip3 install --upgrade --force-reinstall --ignore-installed urllib3==1.25.3
On my lap-top with Windows 7 the right way to install latest version of pip is:
python.exe -m pip install --upgrade pip
First, do this:
sudo apt install python3-pip python-setuptools-doc
Then, as a non-root user (NEVER, never run pip* as root!):
# N.B. bash shell works for this, I have never tested with other shells!
. ....your_virtualenv_folder/bin/activate
pip3 install -U pip
Note: -U is a synonym for --upgrade, as far as I know.
I had installed Python in C:\Python\Python36 so I went to the Windows command prompt and typed cd C:\Python\Python36 to get to the right directory. Then entered the python -m install --upgrade pip all good!
Single Line Python Program
The best way I have found is to write a single line program that downloads and runs the official get-pip script. See below for the code.
The official docs recommend using curl to download the get-pip script, but since I work on windows and don't have curl installed I prefer using python itself to download and run the script.
Here is the single line program that can be run via the command line using Python 3:
python -c "import urllib.request; exec(urllib.request.urlopen('https://bootstrap.pypa.io/get-pip.py').read())"
This line gets the official "get-pip.py" script as per the installation notes and executes the script with the "exec" command.
For Python2 you would replace "urllib.request" with "urllib2":
python -c "import urllib2; exec(urllib2.urlopen('https://bootstrap.pypa.io/get-pip.py').read())"
Precautions
It's worth noting that running any python script blindly is inherently dangerous. For this reason, the official instructions recommend downloading the script and inspecting it before running.
That said, many people don't actually inspect the code and just run it. This one-line program makes that easier.
I had a similar problem on a raspberry pi.
The problem was that http requires SSL and so I needed to force it to use https to get around this requirement.
sudo pip install --upgrade pip --index-url=https://pypi.python.org/simple
or
sudo pip-3.2 --upgrade pip --index-url=https://pypi.python.org/simple/
Head to your command prompt and type the following:
python -m pip install --upgrade pip
While updating pip in virtual env use full path in python command
Envirnments folder struture
myenv\scripts\python
h:\folderName\myenv\scripts\python -m pip install --upgrade pip
Very Simple. Just download pip from https://bootstrap.pypa.io/get-pip.py . Save the file in some forlder or dekstop. I saved the file in my D drive.Then from your command prompt navigate to the folder where you have downloaded pip. Then type there
python -get-pip.py
In linux
I will update with this code
sudo -H pip3 install --upgrade pip

Categories