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 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
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
I'm deploying a Django app to a dev server and am hitting this error when I run pip install -r requirements.txt:
Traceback (most recent call last):
File "/var/www/mydir/virtualenvs/dev/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
pkg_resources appears to be distributed with setuptools. Initially I thought this might not be installed to the Python in the virtualenv, so I installed setuptools 2.6 (same version as Python) to the Python site-packages in the virtualenv with the following command:
sh setuptools-0.6c11-py2.6.egg --install-dir /var/www/mydir/virtualenvs/dev/lib/python2.6/site-packages
EDIT: This only happens inside the virtualenv. If I open a console outside the virtualenv then pkg_resources is present, but I am still getting the same error.
Any ideas as to why pkg_resources is not on the path?
July 2018 Update
Most people should now use pip install setuptools (possibly with sudo).
Some may need to (re)install the python-setuptools package via their package manager (apt-get install, yum install, etc.).
This issue can be highly dependent on your OS and dev environment. See the legacy/other answers below if the above isn't working for you.
Explanation
This error message is caused by a missing/broken Python setuptools package. Per Matt M.'s comment and setuptools issue #581, the bootstrap script referred to below is no longer the recommended installation method.
The bootstrap script instructions will remain below, in case it's still helpful to anyone.
Legacy Answer
I encountered the same ImportError today while trying to use pip. Somehow the setuptools package had been deleted in my Python environment.
To fix the issue, run the setup script for setuptools:
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
(or if you don't have wget installed (e.g. OS X), try
curl https://bootstrap.pypa.io/ez_setup.py | python
possibly with sudo prepended.)
If you have any version of distribute, or any setuptools below 0.6, you will have to uninstall it first.*
See Installation Instructions for further details.
* If you already have a working distribute, upgrading it to the "compatibility wrapper" that switches you over to setuptools is easier. But if things are already broken, don't try that.
sudo apt-get install --reinstall python-pkg-resources
fixed it for me in Debian. Seems like uninstalling some .deb packages (twisted set in my case) has broken the path python uses to find packages
I have seen this error while trying to install rhodecode to a virtualenv on ubuntu 13.10. For me the solution was to run
pip install --upgrade setuptools
pip install --upgrade distribute
before I run easy_install rhodecode.
It also happened to me. I think the problem will happen if the requirements.txt contains a "distribute" entry while the virtualenv uses setuptools. Pip will try to patch setuptools to make room for distribute, but unfortunately it will fail half way.
The easy solution is delete your current virtualenv then make a new virtualenv with --distribute argument.
An example if using virtualenvwrapper:
$ deactivate
$ rmvirtualenv yourenv
$ mkvirtualenv yourenv --distribute
$ workon yourenv
$ pip install -r requirements.txt
In CentOS 6 installing the package python-setuptools fixed it.
yum install python-setuptools
After trying several of these answers, then reaching out to a colleague, what worked for me on Ubuntu 16.04 was:
pip install --force-reinstall -U setuptools
pip install --force-reinstall -U pip
In my case, it was only an old version of pillow 3.1.1 that was having trouble (pillow 4.x worked fine), and that's now resolved!
I had this error earlier and the highest rated answer gave me an error trying to download the ez_setup.py file. I found another source so you can run the command:
curl http://peak.telecommunity.com/dist/ez_setup.py | python
I found that I also had to use sudo to get it working, so you may need to run:
sudo curl http://peak.telecommunity.com/dist/ez_setup.py | sudo python
I've also created another location that the script can be downloaded from:
https://gist.github.com/ajtrichards/42e73562a89edb1039f3
Needed a little bit more sudo. Then used easy_install to install pip. Works.
sudo wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
sudo easy_install pip
I fixed the error with virtualenv by doing this:
Copied pkg_resources.py from
/Library/Python/2.7/site-packages/setuptools
to
/Library/Python/2.7/site-packages/
This may be a cheap workaround, but it worked for me.
.
If setup tools doesn't exist, you can try installing system-site-packages by typing virtualenv --system-site-packages /DESTINATION DIRECTORY, changing the last part to be the directory you want to install to. pkg_rousources.py will be under that directory in lib/python2.7/site-packages
the simple resoluition is that you can use conda to upgrade setuptools or entire enviroment. (Specially for windows user.)
conda upgrade -c anaconda setuptools
if the setuptools is removed, you need to install setuptools again.
conda install -c anaconda setuptools
if these all methodes doesn't work, you can upgrade conda environement. But I do not recommend that you need to reinstall and uninstall some packages because after that it will exacerbate the situation.
A lot of answers are recommending the following but if you read through the source of that script, you'll realise it's deprecated.
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
If your pip is also broken, this won't work either.
pip install setuptools
I found I had to run the command from Ensure pip, setuptools, and wheel are up to date, to get pip working again.
python -m pip install --upgrade pip setuptools wheel
You can use the command
sudo apt-get install --reinstall python3-pkg-resources
if you are using python3 , this was the case with me.
I ran into this problem after installing the latest Python version 3.10.4.
Somehow, the setuptools package and pip were deleted.
I used the following command to resolve the issue :
in [Windows]
py -m ensurepip --default-pip
For me, this error was being caused because I had a subdirectory called "site"! I don't know if this is a pip bug or not, but I started with:
/some/dir/requirements.txt
/some/dir/site/
pip install -r requirements.txt wouldn't work, giving me the above error!
renaming the subfolder from "site" to "src" fixed the problem! Maybe pip is looking for "site-packages"? Crazy.
For me, it turned out to be a permissions problem on site-packages. Since it's only my dev environment, I raised the permissions and everything is working again:
sudo chmod -R a+rwx /path/to/my/venv/lib/python2.7/site-packages/
I had this problem when I had activated my virtualenv as a different user than the one who created it. It seems to be a permission problem. I discovered this when I tried the answer by #cwc and saw this in the output:
Installing easy_install script to /path/env/bin
error: /path/env/bin/easy_install: Permission denied
Switching back to the user that created the virtualenv, then running the original pip install command went without problems. Hope this helps!
I had this problem today as well. I only got the problem inside the virtual env.
The solution for me was deactivating the virtual env, deleting and then uninstalling virtualenv with pip and reinstalling it. After that I created a new virtual env for my project, then pip worked fine both inside the virtual environment as in the normal environment.
Looks like they have moved away from bitbucket and are now on github (https://github.com/pypa/setuptools)
Command to run is:
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
If you are encountering this issue with an application installed via conda, the solution (as stated in this bug report) is simply to install setup-tools with:
conda install setuptools
On Windows, with python 3.7, this worked for me:
pip install --upgrade setuptools --user
--user installs packages in your home directory, which doesn't require admin privileges.
Apparently you're missing setuptools. Some virtualenv versions use distribute instead of setuptools by default. Use the --setuptools option when creating the virtualenv or set the VIRTUALENV_SETUPTOOLS=1 in your environment.
None of the posted answers worked for me, so I reinstalled pip and it worked!
sudo apt-get install python-setuptools python-dev build-essential
sudo easy_install pip
pip install --upgrade setuptools
(reference: http://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/)
In my case, I had 2 python versions installed initially and later I had deleted the older one. So while creating the virtual environment
virtualenv venv
was referring to the uninstalled python
What worked for me
python3 -m virtualenv venv
Same is true when you are trying to use pip.
I came across this answer when I was trying to follow this guide for OSX. What worked for me was, after running python get-pip, I had to ALSO easy_install pip. That fixed the issue of not being able to run pip at all. I did have a bunch of old macport stuff installed. That may have conflicted.
On windows, I installed pip downloaded from www.lfd.uci.edu/~gohlke/pythonlibs/ then encontered this problem.
So I should have installed setuptools(easy_install) first.
just reinstall your setuptools by :
$ sudo wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefefe74e
$ tar -zxvf setuptools-0.6c11.tar.gz
$ cd setuptools-0.6c11/
$ sudo python setup.py build
$ sudo python setup.py install
$ sudo pip install --upgrade setuptools
then everything will be fine.
I use CentOS 6.7, and my python was just upgrade from 2.6.6 to 2.7.11, after tried so many different answer, finally the following one does the job:
sudo yum install python-devel
Hope help someone in the same situation.
I ran into this problem after updating my Ubuntu build. It seems to have gone through and removed set up tools in all of my virtual environments.
To remedy this I reinstalled the virtual environment back into the target directory. This cleaned up missing setup tools and got things running again.
e.g.:
~/RepoDir/TestProject$ virtualenv TestEnvironmentDir
For me a good fix was to use --no-download option to virtualenv (VIRTUALENV_NO_DOWNLOAD=1 tox for tox.)
On Opensuse 42.1 the following fixed this issue:
zypper in python-Pygments