Python pip py31compat import error - python

When I try to install any package via pip on MacOS Sierra, I encounter the following error:
File "/usr/local/bin/pip", line 7, in <module>
from pip import main
File "/Library/Python/2.7/site-packages/pip/__init__.py", line 26, in <module>
from pip.utils import get_installed_distributions, get_prog
File "/Library/Python/2.7/site-packages/pip/utils/__init__.py", line 27, in <module>
from pip._vendor import pkg_resources
File "/Library/Python/2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in <module>
#_call_aside
File "/Library/Python/2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
f(*args, **kwargs)
File "/Library/Python/2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
dist.activate(replace=False)
File "/Library/Python/2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
declare_namespace(pkg)
File "/Library/Python/2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
_handle_ns(packageName, path_item)
File "/Library/Python/2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2091, in _handle_ns
loader.load_module(packageName)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 246, in load_module
mod = imp.load_module(fullname, self.file, self.filename, self.etc)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/PyObjCTools/__init__.py", line 1, in <module>
__import__('pkg_resources').declare_namespace(__name__)
File "/Users/jiaseny/Library/Python/2.7/lib/python/site-packages/pkg_resources/__init__.py", line 72, in <module>
from . import py31compat
ImportError: cannot import name py31compat
Trying sudo pip install pkg_resources, sudo pip install setuptools as suggested in ImportError: No module named py31compat
resulted in the same error.
Perhaps I should try uninstalling and reinstalling pip? But sudo pip uninstall pip and sudo easy_install pip resulted in the same errors...

Based on the path to your python, I can see you're not using the system python which is installed natively, nor are you using homebrew or pyenv, either of which I would highly recommend.
Pyenv is my preferred method of installation for development purposes. A manual install is straight forward and only mildly more difficult than with homebrew, and its one less thing relying on a relatively opaque box to function. The recommended install from the README is just:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
exec "$SHELL"
pyenv install 2.7.14
pyenv global 2.7.14
Alternatively if you use homebrew you can just brew install pyenv, and I believe it will set everything up for you, before the pyenv install.
Or if all you want is a somewhat more up to date python than the system one, install homebrew and brew install python.

In case you come here through Google, I had similar issue on MacOS. I tried the the accepted answer it did not work. But doing these upgrade resolved it for me:
pip install --upgrade pip
pip install --upgrade setuptools
pip install --upgrade distribute

Related

Cannot import 'name' error with pip3 on clean Ubuntu install

I have a clean install of ubuntu 16.04 on an EC2 instance. I've gone ahead and upgraded the Python version from 3.5 to 3.7. Unfortunately, this seems to have rendered pip3 non-functional. Despite definitely having pip3 installed:
~$ which pip3
/usr/bin/pip3
when I try to do anything with pip3, it shows the same error:
~$ pip3 --version
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ImportError: cannot import name 'main' from 'pip' (/usr/local/lib/python3.7/site-packages/pip/__init__.py)
I have tried upgrading pip via
sudo python3 -m pip install --upgrade pip
and getting pip from source via
curl https://bootstrap.pypa.io/get-pip.py | sudo python3
Neither has fixed the problem. Any ideas on what might be going on? I've managed to get replicate the error by undertaking the upgrade to 3.7 on a clean EC2 server, though I also was hitting the same thing on another EC2 instance when upgrading to 3.7. I'm guessing this has to do with ubuntu in that case--is there a reason that 3.7 isn't playing nicely with my pip?
Edit:
Suggested re-installing pip via
python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
That yields the following error trace:
ERROR: Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 188, in main
status = self.run(options, args)
File "/usr/local/lib/python3.7/site-packages/pip/_internal/commands/uninstall.py", line 48, in run
with self._build_session(options) as session:
File "/usr/local/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 108, in _build_session
index_urls=self._get_index_urls(options),
File "/usr/local/lib/python3.7/site-packages/pip/_internal/download.py", line 559, in __init__
self.headers["User-Agent"] = user_agent()
File "/usr/local/lib/python3.7/site-packages/pip/_internal/download.py", line 144, in user_agent
zip(["name", "version", "id"], distro.linux_distribution()),
File "/usr/local/lib/python3.7/site-packages/pip/_vendor/distro.py", line 122, in linux_distribution
return _distro.linux_distribution(full_distribution_name)
File "/usr/local/lib/python3.7/site-packages/pip/_vendor/distro.py", line 677, in linux_distribution
self.version(),
File "/usr/local/lib/python3.7/site-packages/pip/_vendor/distro.py", line 737, in version
self.lsb_release_attr('release'),
File "/usr/local/lib/python3.7/site-packages/pip/_vendor/distro.py", line 899, in lsb_release_attr
return self._lsb_release_info.get(attribute, '')
File "/usr/local/lib/python3.7/site-packages/pip/_vendor/distro.py", line 552, in __get__
ret = obj.__dict__[self._fname] = self._f(obj)
File "/usr/local/lib/python3.7/site-packages/pip/_vendor/distro.py", line 1012, in _lsb_release_info
stdout = subprocess.check_output(cmd, stderr=devnull)
File "/usr/local/lib/python3.7/subprocess.py", line 411, in check_output
**kwargs).stdout
File "/usr/local/lib/python3.7/subprocess.py", line 512, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.
It's possible that the system pip has been corrupted, maybe try un-installing/re-installing pip via python3:
python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
Pip developers say very clearly:
Only ever use your system package manager to upgrade the system pip
pip3 can be configured for old python. Use pythonx.x -m pip instead so you're sure that you're installing packages for the right python version.
I suggest installing pip the official way . Deb pip packages will have very old pip, and it's hard to update them without breaking anything. If you installed something like python3.7-pip I suggest removing it.
PS: I hope that by upgrading python you mean installing 3.7 besides system 3.5, because replacing python3.5 will likely break something in your OS.

pkg_resources.DistributionNotFound: The 'pipenv==2018.10.13' distribution was not found and is required by the application

I've reinstalled pip and pipenv due to some broken package with ansible. Now, it seems like my pip dependencies are all screwed. Any suggestion or help is greatly appreciated.
$ which python2
/usr/local/bin/python2
$ which python3
/usr/local/bin/python3
$ which pipenv
/usr/local/bin/pipenv
Traceback (most recent call last):
File "/usr/local/Cellar/pipenv/2018.10.13/libexec/bin/pipenv", line 6, in <module>
from pkg_resources import load_entry_point
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3123, in <module>
#_call_aside
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3107, in _call_aside
f(*args, **kwargs)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3136, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 580, in _build_master
return cls._build_from_requirements(__requires__)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 593, in _build_from_requirements
dists = ws.resolve(reqs, Environment())
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 781, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pipenv==2018.10.13' distribution was not found and is required by the application
I had the same problem. You should reinstall pipenv using the same package manager you used the first time.
If the installation was done using pip, then:
pip uninstall pipenv
pip install pipenv
If you are using brew, then you must run the commands exposed by Andrei
brew uninstall pipenv
brew install pipenv
To check if pipenv installation was successfully completed, run:
pipenv --version
From the comments, alternatively use:
brew reinstall pipenv
This should fix the problem too:
pipenv --rm
pipenv check
pipenv sync
I run into this problem from time to time when updating python using anaconda.
if you get this kind of "pkg_resources.DistributionNotFound: The 'filelock<4,>=3.4.1' distribution was not found and is required by virtualenv" error you can also solve it by
navigating to the folder and then run "pip install pipenv"
first go to the virtual env directory by typing below command on the terminal
cd ~/.local/share/virtualenvs/
then check for a directory having project name as prefix, and open terminal inside that directory and type
mkdir bin; cp local/bin/python bin/
after this go the the your project directory and run pipenv install again.

ImportError: No module named urllib3 when installing pip

I have an issue with virtualenvwrapper that requires reinstalling pip as the first step, and I am getting a strange complaint about urllib3:
$ python get-pip.py
Traceback (most recent call last):
File "get-pip.py", line 19177, in <module>
main()
File "get-pip.py", line 194, in main
bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 82, in bootstrap
import pip
File "/tmp/tmppQTQty/pip.zip/pip/__init__.py", line 16, in <module>
File "/tmp/tmppQTQty/pip.zip/pip/vcs/mercurial.py", line 9, in <module>
File "/tmp/tmppQTQty/pip.zip/pip/download.py", line 39, in <module>
File "/tmp/tmppQTQty/pip.zip/pip/_vendor/requests/__init__.py", line 61, in <module>
File "/tmp/tmppQTQty/pip.zip/pip/_vendor/requests/packages/__init__.py", line 29, in <module>
ImportError: No module named urllib3
when I search the pip file get-pip.py, I don't see any reference to urllib3.
I have python2.6, py2.7, and py3.4 on the machine
cchilders:~/Downloads
$ which python
/usr/local/bin/python
cchilders:~/Downloads
$ which pip
/usr/local/bin/pip
cchilders:/usr/local/bin
$ ls
2to3 easy_install-2.7 ipython pip3 smtpd.py
celery fop ipython2 pip3.5 uncompyle2
celerybeat grunt jsonlint pydoc virtualenv
celeryd idle pbr python virtualenv-clone
celeryd-multi ipdb pip python2.6 virtualenvwrapper_lazy.sh
charm iptest pip2 python2.6-config virtualenvwrapper.sh
easy_install iptest2 pip2.7 python-config
Python is defaulting to 2.6.9 which isn't what I want:
cchilders:/usr/bin
$ python
Python 2.6.9
I do not have anaconda on the machine, which caused someone else's problem. Any advice what to do now appreciated, thank you
Very first you should to know what default python version you are using then, suppose if you are using python2.7 then try to find installed pip packages under that python version using pip freeze or pip freeze | grep -i urllib3
i guess urllib3 is not installed
wget https://pypi.python.org/packages/3b/f0/e763169124e3f5db0926bc3dbfcd580a105f9ca44cf5d8e6c7a803c9f6b5/urllib3-1.16.tar.gz#md5=fcaab1c5385c57deeb7053d3d7d81d59
tar xvf urllib3-1.16.tar.gz && cd urllib3-1.16/
python setup.py install

Error: "No module named _markerlib" when installing some packages on virtualenv

I can't install some packages on virtualenv because of this error.
I tried to install:
pip install pyups==0.4.4
and
pip install --upgrade distribute
and they give me the error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-tLx1oC/distribute/setup.py", line 58, in <module>
setuptools.setup(**setup_params)
File "/usr/lib/python2.7/distutils/core.py", line 151, in setup
dist.run_commands()
File "/usr/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/usr/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "setuptools/command/egg_info.py", line 177, in run
writer = ep.load(installer=installer)
File "pkg_resources.py", line 2241, in load
if require: self.require(env, installer)
File "pkg_resources.py", line 2254, in require
working_set.resolve(self.dist.requires(self.extras),env,installer)))
File "pkg_resources.py", line 2471, in requires
dm = self._dep_map
File "pkg_resources.py", line 2682, in _dep_map
self.__dep_map = self._compute_dependencies()
File "pkg_resources.py", line 2699, in _compute_dependencies
from _markerlib import compile as compile_marker
ImportError: No module named _markerlib
I tried also to install markerlib with
pip install markerlib
But the error continues.
$ easy_install distribute
or build from source works for me
I try the following command
easy_install distribute
pip install --upgrade distribute
which works.
install setuptools:
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-19.2.zip
unzip setuptools-19.2.zip
cd setuptools-19.2
python setup.py install
Although the virtualenv docs says --distribute and --setuptools options are now legacy, when I created the environment with :
virtualenv myenv --distribute
the error was gone.
This sound silly. But It works for me. I am using ubuntu. While Installing virtualenv through apt-get install I faced problems. But I got working with pip install virtualenv.
sudo pip uninstall virtualenv
sudo apt-get purge python-virtualenv
sudo pip install virtualenv

Creating virtualenv using altinstall

I have just done a fresh install of Linux Mint 17.3. It comes with both python 2.7 and 3.4. I usually work with multiple versions of python so I just do an altinstall and then for each project I create a virtualenv using the desired version of python. However im running into issues with the newly installed OS. First a few things i've already done following the install:
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install python-virtualenv
sudo apt-get install python-pip
I did an altinstall of python3.3.5:
downloaded the source tarball
./configure --with-zlib
sudo make
sudo make altinstall
I then tried creating a virtualenv in a new folder to test:
virtualenv -p python3.3 venv
This gave an error:
no module named zlib
I've had this issue in the past, so I did:
sudo apt-get install python-dev
sudo apt-get install zlib1g-dev
Now when I create the virtualenv the zlib error is gone, however im getting a new error and I can't seem to figure out how to fix it:
Running virtualenv with interpreter /usr/local/bin/python3.3
Using base prefix '/usr/local'
New python executable in venv/bin/python3.3
Also creating executable in venv/bin/python
Installing setuptools, pip, wheel...
Complete output from command /home/vega/Documents...8/venv/bin/python3.3 -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip wheel:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-7.1.2-py2.py3-none-any.whl/pip/__init__.py", line 15, in <module>
File "/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-7.1.2-py2.py3-none-any.whl/pip/vcs/subversion.py", line 9, in <module>
File "/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-7.1.2-py2.py3-none-any.whl/pip/index.py", line 30, in <module>
File "/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-7.1.2-py2.py3-none-any.whl/pip/wheel.py", line 35, in <module>
File "/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-7.1.2-py2.py3-none-any.whl/pip/_vendor/distlib/scripts.py", line 14, in <module>
File "/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-7.1.2-py2.py3-none-any.whl/pip/_vendor/distlib/compat.py", line 66, in <module>
ImportError: cannot import name HTTPSHandler
----------------------------------------
...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 2363, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 832, in main
symlink=options.symlink)
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 1004, in create_environment
install_wheel(to_install, py_executable, search_dirs)
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 969, in install_wheel
'PIP_NO_INDEX': '1'
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 910, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /home/vega/Documents...8/venv/bin/python3.3 -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip wheel failed with error code 1
I read somewhere that this might have something to do with openssl so I did:
sudo apt-get install openssl
sudo apt-get install libcurl4-openssl-dev
No luck, still having the same issue.
I let someone else do the work, so I don't have to worry about building alterante versions myself from source.
I've had really good luck using Felix Krull's "deadsnakes" PPA for installing alternate Pythons on Ubuntu. Would this work for Mint 17? (based on Ubuntu Trusty)
'deadsnakes PPA' packages versions of Python: 2.3, 2.4, 2.5, 2.6, 2.7, 3.1, 3.2, 3.3, 3.4, 3.5 ... all available to install from apt. Once installed, You can manage versions and dependencies with virtualenv and pip.
Installing Python 3.5 from deadsnakes PPA:
$ sudo add-apt-repository ppa:fkrull/deadsnakes
$ sudo apt-get update
$ sudo apt-get install python3.5 python3.5-dev
The PPA maintainer has maintained these for quite a long time and updates with each Ubuntu release.
https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes

Categories

Resources