I have ubuntu 11.10. I apt-get installed pypy from this launchpad repository: https://launchpad.net/~pypy the computer already has python on it, and python has its own pip. How can I install pip for pypy and how can I use it differently from that of python?
Quoting (with minor changes) from here the pypy website:
If you want to install 3rd party libraries, the most convenient way is
to install pip:
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ ./pypy-2.1/bin/pypy get-pip.py
$ ./pypy-2.1/bin/pip install pygments # for example
In order to use it nicely, you might want to add an alias into e.g. ~/.bashrc:
alias pypy_pip='./pypy-2.1/bin/pip'
Where the actual pip executable is located has to be taken from the output of pypy get-pip.py
To keep a separate installation, you might want to create a virtualenv for PyPy. Within the virtualenv, you can then just run pip install whatever and it will install it for PyPy. When you create a virtualenv, it automatically installs pip for you.
Otherwise, you will need to work out where PyPy will import from and install distribute and pip in one of those locations. pip's installer should do this automatically when run with PyPy. Be careful with this option - if it decides to install in your system Python directories, it could break other things.
if you want to use pip with pypy:
pypy -m pip install [package]
pip is included with pypy so just target pip with the -m flag
The problem with pip installing from the pypy (at least when installing pypy via apt-get) is that it is installed into the system path:
$ whereis pip
pip: /usr/local/bin/pip /usr/bin/pip
So after such install, pypy pip is executed by default (/usr/local/bin/pip) instead of the python pip (/usr/bin/pip) which may break subsequent updates of the whole Ubuntu.
The problem with virtualenv is that you should remember where and what env you created.
Convenient alternative solution is conda (miniconda), which manages not only python deployments: http://conda.pydata.org/miniconda.html.
Comparison of conda, pip and virtualenv:
http://conda.pydata.org/docs/_downloads/conda-pip-virtualenv-translator.html
Related
On Ubuntu 10.04 by default Python 2.6 is installed, then I have installed Python 2.7. How can I use pip install to install packages for Python 2.7.
For example:
pip install beautifulsoup4
by default installs BeautifulSoup for Python 2.6
When I do:
import bs4
in Python 2.6 it works, but in Python 2.7 it says:
No module named bs4
Alternatively, since pip itself is written in python, you can just call it with the python version you want to install the package for:
python2.7 -m pip install foo
Use a version of pip installed against the Python instance you want to install new packages to.
In many distributions, there may be separate python2.6-pip and python2.7-pip packages, invoked with binary names such as pip-2.6 and pip-2.7. If pip is not packaged in your distribution for the desired target, you might look for a setuptools or easyinstall package, or use virtualenv (which will always include pip in a generated environment).
pip's website includes installation instructions, if you can't find anything within your distribution.
You can execute pip module for a specific python version using the corresponding python:
Python 2.6:
python2.6 -m pip install beautifulsoup4
Python 2.7
python2.7 -m pip install beautifulsoup4
In Windows, you can execute the pip module by mentioning the python version ( You need to ensure that the launcher is on your path )
py -2 -m pip install pyfora
You can use this syntax
python_version -m pip install your_package
For example. If you're running python3.5, you named it as "python3", and want to install numpy package
python3 -m pip install numpy
Have tried this on a Windows machine and it works
If you wanna install opencv for python version 3.7, heres how you do it!
py -3.7 -m pip install opencv-python
Alternatively, if you want to install specific version of the package with the specific version of python, this is the way
sudo python2.7 -m pip install pyudev=0.16
if the "=" doesnt work, use ==
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev=0.16
Invalid requirement: 'pyudev=0.16'
= is not a valid operator. Did you mean == ?
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev==0.16
works fine
If you have both 2.7 and 3.x versions of python installed, then just rename the python exe file of python 3.x version to something like - "python.exe" to "python3.exe". Now you can use pip for both versions individually. If you normally type "pip install " it will consider the 2.7 version by default. If you want to install it on the 3.x version you need to call the command as "python3 -m pip install ".
Python 2
sudo pip2 install johnbonjovi
Python 3
sudo pip3 install johnbonjovi
For Python 3
sudo apt-get install python3-pip
sudo pip3 install beautifulsoup4
For Python 2
sudo apt-get install python2-pip
sudo pip2 install beautifulsoup4
On Debian/Ubuntu, pip is the command to use when installing packages
for Python 2, while pip3 is the command to use when installing
packages for Python 3.
for python2 use:
py -2 -m pip install beautifulsoup4
I faced a similar problem with another package called Twisted. I wanted to install it for Python 2.7, but it only got installed for Python 2.6 (system's default version).
Making a simple change worked for me.
When adding Python 2.7's path to your $PATH variable, append it to the front like this: PATH=/usr/local/bin:$PATH, so that the system uses that version.
If you face more problems, you can follow this blog post which helped me - https://github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence-exactly-for-centos-machine-only
As with any other python script, you may specify the python installation you'd like to run it with. You may put this in your shell profile to save the alias. The $1 refers to the first argument you pass to the script.
# PYTHON3 PIP INSTALL V2
alias pip_install3="python3 -m $(which pip) install $1"
I'm using Ubuntu 22.04, which comes with python 3.10.4.
Some packages do not have recent pip packages, so I needed install from an older pip. This sequence worked for me.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9
sudo apt install python3.9-distutils
python3.9 -m pip install onnxruntime-gpu
Folder location: /usr/local/lib/python3.8
Package: python3.8 -m pip install <package_name>
I had Python 2.7 installed via chocolatey on Windows and found pip2.7.exe in C:\tools\python2\Scripts.
Using this executable instead of the pip command installed the correct module for me (requests for Python 2.7).
I think the best practice here is not to use the system python or install any system python package (no apt install). That is just the way to trouble.
Instead, build the required Python version from source, get it installed in /usr/local/... . Then use pip to install packages for that. It is really not that hard to build Python from source on Ubuntu.
sudo apt install build-essential
download the source from https://www.python.org/downloads/source/
unpack the file downloaded: tar xf <filename>
cd <directory> - change into the directory created.
./configure
make
sudo make install
Then check /usr/local/bin for a pip script tied to that version. Use that to pip install whatever you need. Also find the particular executable for the python version in that directory. You might have to shuffle things a bit if you get lots of versions.
Again, do not mess with system python.
I am trying to run a webpage using python flask and connecting it with the database of MySQL and while installing MySQL packages I'm receiving this error.
I'm doing this on ec2 Linux AWS.
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
So, here we have Python 2.7.12 in Ubuntu 16.04 ec2 machine, and get ImportError: cannot import name main when trying to use pip. It's 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 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 a versioned pip2 command (or pip3 for Python 3) instead of pip to invoke the older system-installed launcher /usr/bin/pip2 , whereas any pip script located in $HOME/.local/bin dir (pip, pip2, pip2.7) will invoke 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, such as your MySQL-python module (MySQLdb) or a Flask-MySQL, for a specific Python version. The --user switch is only required 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> # or apt-cache search in older Ubuntu
$ apt show <python-package> # e.g. python-flask
$ sudo apt install <python-package> # or sudo apt-get install
Packages prefixed with python- are for Python 2; with python3- are for Python 3.
Standard apt-get installation method may be what you need. For example, in your case:
python-mysqldb - Python interface to MySQL <- a fork of MySQLdb == MySQL-python
python-flask-sqlalchemy - SQL Alchemy support
python-pymysql - pure Python MySQL driver
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 the system libraries you need 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 only 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 have to use pip install command on Ubuntu instead of apt-get install, please ensure it runs in an isolated virtual development environment, such as with virtualenv (sudo apt-get install python-virtualenv), or using 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) on Ubuntu/Debian 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 system-wide Python packages, as these are managed by apt-get on Ubuntu.
These steps worked for me.
1- Uninstall the pip update from python.
2- Uninstall pip package from your Ubuntu.
3- Check that pip binary is not longer in your system.
python -m pip uninstall pip
apt remove python-pip
whereis pip
4- Download and install pip. (credits for VanDragt.com)
wget https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py
sudo python3 /tmp/get-pip.py
pip install --user pipenv
pip3 install --user pipenv
echo "PATH=$HOME/.local/bin:$PATH" >> ~/.profile
source ~/.profile
whereis pip
Now you should be able to install any pip package you want.
My cent, I had the same ImportError: cannot import name main.
My system is a Linux Ubuntu distro, I have executed this command:
python -m pip uninstall pip
This has removed one local (for the user) pip version.
I had already an older pip/pip2 system executable (apt-get installed in ancient times) that worked like a charm.
As suggested in pip's github issue
The temporary fix is -
Edit your /usr/bin/pip file and comment the line importing main and edit it
#from pip import main
from pip._internal import main as main
Worked perfectly for me.
Note - this is a temporary fix. Wait for team pip to fix this.
OR
from pip import main
if __name__ == '__main__':
sys.exit(main())
to this:
from pip import __main__
if __name__ == '__main__':
sys.exit(__main__._main())
As suggested in this SO answer.
Try this
Check the python version you use
# Python --version
and try installing for eg if your version is 2.7
#python2.7 <package name>
Will work fine......
I have faced the similar issue after pip 19 upgrade. So I did the following to fix the problem.
pip install --upgrade pip==9.0.3
instead of
pip install -U pip
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
I have a macbook pro, and I have Python 2.7,3.3 and 3.4 and Anaconda 2.7 installed, and I am having a hard time managing all these multiple Python distributions. These are the problems I am facing :
pip install installs for anaconda 2.7 by default, how do I make it install for PYthon 2.7 ?
how do I make pip3 install work for different Python distributions? i.e is there some way I can use something like pip3 install -v 3.3.6 or something similar.
how do I find a workaround for these problems. I wish to know the answer in both the basic sense (how Python and Anaconda work, and what happens when I use pip install, and how do I use these to solve my problem), and also in the practical sense (is there some simple way to manage this problem).
To install packages for specific python versions, it is easiest to run python with the -m flag. The -m flag will run the specified module as a script. Calling python -m pip install _package_ will run pip for whatever python you specify.
Examples:
python -m pip install _package_
python3 -m pip install _package_
python2.7 -m pip install _package_
python3.7 -m pip install _package_
On Ubuntu 10.04 by default Python 2.6 is installed, then I have installed Python 2.7. How can I use pip install to install packages for Python 2.7.
For example:
pip install beautifulsoup4
by default installs BeautifulSoup for Python 2.6
When I do:
import bs4
in Python 2.6 it works, but in Python 2.7 it says:
No module named bs4
Alternatively, since pip itself is written in python, you can just call it with the python version you want to install the package for:
python2.7 -m pip install foo
Use a version of pip installed against the Python instance you want to install new packages to.
In many distributions, there may be separate python2.6-pip and python2.7-pip packages, invoked with binary names such as pip-2.6 and pip-2.7. If pip is not packaged in your distribution for the desired target, you might look for a setuptools or easyinstall package, or use virtualenv (which will always include pip in a generated environment).
pip's website includes installation instructions, if you can't find anything within your distribution.
You can execute pip module for a specific python version using the corresponding python:
Python 2.6:
python2.6 -m pip install beautifulsoup4
Python 2.7
python2.7 -m pip install beautifulsoup4
In Windows, you can execute the pip module by mentioning the python version ( You need to ensure that the launcher is on your path )
py -2 -m pip install pyfora
You can use this syntax
python_version -m pip install your_package
For example. If you're running python3.5, you named it as "python3", and want to install numpy package
python3 -m pip install numpy
Have tried this on a Windows machine and it works
If you wanna install opencv for python version 3.7, heres how you do it!
py -3.7 -m pip install opencv-python
Alternatively, if you want to install specific version of the package with the specific version of python, this is the way
sudo python2.7 -m pip install pyudev=0.16
if the "=" doesnt work, use ==
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev=0.16
Invalid requirement: 'pyudev=0.16'
= is not a valid operator. Did you mean == ?
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev==0.16
works fine
If you have both 2.7 and 3.x versions of python installed, then just rename the python exe file of python 3.x version to something like - "python.exe" to "python3.exe". Now you can use pip for both versions individually. If you normally type "pip install " it will consider the 2.7 version by default. If you want to install it on the 3.x version you need to call the command as "python3 -m pip install ".
Python 2
sudo pip2 install johnbonjovi
Python 3
sudo pip3 install johnbonjovi
For Python 3
sudo apt-get install python3-pip
sudo pip3 install beautifulsoup4
For Python 2
sudo apt-get install python2-pip
sudo pip2 install beautifulsoup4
On Debian/Ubuntu, pip is the command to use when installing packages
for Python 2, while pip3 is the command to use when installing
packages for Python 3.
for python2 use:
py -2 -m pip install beautifulsoup4
I faced a similar problem with another package called Twisted. I wanted to install it for Python 2.7, but it only got installed for Python 2.6 (system's default version).
Making a simple change worked for me.
When adding Python 2.7's path to your $PATH variable, append it to the front like this: PATH=/usr/local/bin:$PATH, so that the system uses that version.
If you face more problems, you can follow this blog post which helped me - https://github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence-exactly-for-centos-machine-only
As with any other python script, you may specify the python installation you'd like to run it with. You may put this in your shell profile to save the alias. The $1 refers to the first argument you pass to the script.
# PYTHON3 PIP INSTALL V2
alias pip_install3="python3 -m $(which pip) install $1"
I'm using Ubuntu 22.04, which comes with python 3.10.4.
Some packages do not have recent pip packages, so I needed install from an older pip. This sequence worked for me.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9
sudo apt install python3.9-distutils
python3.9 -m pip install onnxruntime-gpu
Folder location: /usr/local/lib/python3.8
Package: python3.8 -m pip install <package_name>
I had Python 2.7 installed via chocolatey on Windows and found pip2.7.exe in C:\tools\python2\Scripts.
Using this executable instead of the pip command installed the correct module for me (requests for Python 2.7).
I think the best practice here is not to use the system python or install any system python package (no apt install). That is just the way to trouble.
Instead, build the required Python version from source, get it installed in /usr/local/... . Then use pip to install packages for that. It is really not that hard to build Python from source on Ubuntu.
sudo apt install build-essential
download the source from https://www.python.org/downloads/source/
unpack the file downloaded: tar xf <filename>
cd <directory> - change into the directory created.
./configure
make
sudo make install
Then check /usr/local/bin for a pip script tied to that version. Use that to pip install whatever you need. Also find the particular executable for the python version in that directory. You might have to shuffle things a bit if you get lots of versions.
Again, do not mess with system python.