python3 pip broken. missing distutils - python

I have been trying to get pip with python3 on WSL ubuntu 16.04 and have had no success. I have been running sudo apt install --reinstall python3-pip, but still have issues getting pip to work properly.
$ pip3
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 14, in
<module>
from pip.utils import get_installed_distributions, get_prog
File "/usr/lib/python3/dist-packages/pip/utils/__init__.py", line 23, in
<module>
from pip.locations import (
File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in
<module>
from distutils import sysconfig
ImportError: No module named 'distutils'
Similar with sudo
$ sudo pip3
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 14, in
<module>
from pip.utils import get_installed_distributions, get_prog
File "/usr/lib/python3/dist-packages/pip/utils/__init__.py", line 23, in
<module>
from pip.locations import (
File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in <module>
from distutils import sysconfig
ImportError: No module named 'distutils'
Also I noticed that I seem to have two versions of python3 installed:
$ ls /usr/lib | grep python
python2.7
python3
python3.5

According to Python 3 documentation, distutils apparently is not deprecated, BUT, my install of 2.7 has disutils, but 3.6 does not.
When I read:
https://docs.python.org/3/installing/
It seemed to say that distutils was being deprecated, but it was not worded clearly if they were talking about distutils or something else.
If anyone knows the definitive answer, please let us know.

So after a lot of pain, I finally gave in. I had made things worse and corrupted my entire python3 installation. I had corrupted too many files trying to fix everything. I ended up uninstalling WSL/ubuntu and reinstalling clean. I made of backup a few things first.
Unfortunately, this seems to be the best option if you have really messed this up, like I did. Being more careful now, python3 is working just fine, with pip. Make sure you follow official docs when you install.

Related

Python modulenotfound error - Is there a "pip" alternative way to install python modules

I am trying to run a program (obviously written in python) and it requires some python modules. One of the missed modules is aiomultiprocess. I tried installing it with pip install aiomultiprocess pip3 install aiomultiprocess python3.7 -m pip install aiomultiprocess but they all come back with the same error message:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/setuptools/__init__.py", line 19, in <module>
from setuptools.dist import Distribution
File "/usr/local/lib/python3.7/site-packages/setuptools/dist.py", line 34, in <module>
from setuptools import windows_support
File "/usr/local/lib/python3.7/site-packages/setuptools/windows_support.py", line 2, in <module>
import ctypes
File "/usr/local/lib/python3.7/ctypes/__init__.py", line 10, in <module>
from _ctypes import Union, Structure, Array
File "/usr/local/lib/python3.7/_ctypes.py", line 5, in <module>
import ffi
ModuleNotFoundError: No module named 'ffi'
No ffi module?! Let's get it!
I looked it up and figured it's a foreign library that come, in the case of python, as 2 forms: CFFI & LibFFI, but "ffi" doesn't exist for python. I installed cffi libffi and added them to this path /usr/local/lib/python3.7/site-packages/, but it changed nothing.
So my question is there another way to install python modules without the need of pip??!

Using Pip and Importing Packages in Python 3 on Ubuntu

I'm on Lubuntu (a minimal build of Ubuntu). I installed Python 3.6.1 with:
sudo apt-get install python3.6
I want to use Twilio, so I tried this:
pip install twilio
It said pip wasn't recognized, so I ran this, followed by the above again:
sudo apt install python-pip
I fired up Python 3.6 and tried importing it, but that didn't work out:
>>> from twilio.rest import Client
ModuleNotFoundError: No module named 'twilio'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I'm new to python3 (which I want to use for the secrets module), Ubuntu, and twilio, so I'm not really sure what's going on here. I'd think if it was as simple as a module not being found, the stack trace would be much shorter... I'm not sure if the stack trace is so long because maybe Python 3 produces longer ones than 2 did, or Ubuntu is trying to do something weird because both Python 2 and 3 are installed (and perhaps two versions of 3 at that), or if maybe this is the work of twilio?
Try the following to download pip:
wget https://bootstrap.pypa.io/get-pip.py
Then install pip by running:
sudo python3 get-pip.py
Then you can install twilio with pip
sudo pip3 install twilio

How to import python3 lib in python3.6

I have virtual environment created for python 3.6. And I have install python3-pyqt5.qtwebkit package with apt-get. The package have installed in global python3.5 and its working, but if I try to import it from venv python3.6 there is import error... How can I install this package in my virtual environment? Or if this is not possible how can I use global package installed in 3.5 in my virtualenv 3.6 python.
Running with global python3.5
(virtualenv)
~/vcs-ss/sst master ✗ 5d ▴ ⚑ ✚ ◒
▶ python3.5 -c 'import PyQt5.QtWebKit; print(PyQt5.QtWebKit.__file__)'
/usr/lib/python3/dist-packages/PyQt5/QtWebKit.cpython-35m-x86_64-linux-gnu.so
Running with python3.6 from venv
(virtualenv)
~/vcs-ss/sst master ✗ 5d ▴ ⚑ ✚ ◒
▶ python3.6 test/e2e/browser.py
Traceback (most recent call last):
File "test/e2e/browser.py", line 8, in <module>
from PyQt5.QtWebKit import *
ModuleNotFoundError: No module named 'PyQt5.QtWebKit'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "test/e2e/browser.py", line 8, in <module>
from PyQt5.QtWebKit import *
ModuleNotFoundError: No module named 'PyQt5.QtWebKit'
In order to use system packages inside a virtual environment, you have to use --system-site-packages option when creating virtual env:
virtualenv --python=python3.6 --system-site-packages env
The same switch is available in Python3's venv.
Much better option is (if applicable in your case) to install all packages inside your virtual env, using pip.
If you look at the contents of the package (using dpkg --listfiles python3-pyqt5.qtwebkit) you'll probably see something like this:
/.
/usr
/usr/lib
/usr/lib/python3
/usr/lib/python3/dist-packages
/usr/lib/python3/dist-packages/PyQt5
/usr/lib/python3/dist-packages/PyQt5/QtWebKit.cpython-35m-x86_64-linux-gnu.so
/usr/lib/python3/dist-packages/PyQt5/QtWebKit.pyi
/usr/lib/python3/dist-packages/PyQt5/QtWebKitWidgets.cpython-35m-x86_64-linux-gnu.so
/usr/lib/python3/dist-packages/PyQt5/QtWebKitWidgets.pyi
/usr/share
/usr/share/doc
/usr/share/doc/python3-pyqt5.qtwebkit
/usr/share/doc/python3-pyqt5.qtwebkit/copyright
/usr/share/doc/python3-pyqt5.qtwebkit/changelog.Debian.gz
The package installs binary modules compiled for a specific python version (in this case: cpython-35m), you won't be able to use those from a python3.6 installation/venv directly because of the incompatible binary names.
For that to work you'd need to symlink all the .so files in the PyQt5 directory to your venv and there change cpython-35m-x86_64-linux-gnu to cpython-36m-x86_64-linux-gnu. Same for the sip libraries, as those are requiered by PyQt5. And that will only work if the ABI is compatible between the two versions.
A quick test with the versions mentioned above shows it works at least for simple scripts, but I can't give any guarantee.

Python pip2 fails - ImportError: No module named moves

I can't run pip2, which is installed from its Arch Linux package:
$ pip2
Traceback (most recent call last):
File "/usr/bin/pip2", line 5, in <module>
from pkg_resources import load_entry_point
File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 47, in <module>
from pkg_resources.extern.six.moves import urllib, map, filter
ImportError: No module named moves
I reinstalled python2-pip2 and python2-setuptools with no results. This has been a problem for months.
While I don't understand the issue, it is possible to reset it by removing locally (--user) installed packages:
Move all packages installed as the user:
mv ~/.local/lib/python2.7/site-packages ~/site-packages-bak
or try to pinpoint the problematic files. In my case, for some reason I had a python file and its pyc file lying around the site-packages directory and just moving them solved my issue:
mv ~/.local/lib/python2.7/site-packages/six.py ~/six.py-bak
mv ~/.local/lib/python2.7/site-packages/six.pyc ~/six.pyc-bak

ImportError: No module named 'nose'

I tried to help('modules') and there was no nose module.
I even tried pip install nose, sudo pip install nose, and etc..
When ever I tried to install nose with command line, it will throw out
SyntaxError: invalid syntax.
Traceback (most recent call last):
File "C:\Python34\plot_cluster_iris.py", line 31, in <module>
from sklearn.cluster import KMeans
File "C:\Python34\lib\site-packages\sklearn\cluster\__init__.py", line 6, in <module>
from .spectral import spectral_clustering, SpectralClustering
File "C:\Python34\lib\site-packages\sklearn\cluster\spectral.py", line 13, in <module>
from ..utils import check_random_state, as_float_array
File "C:\Python34\lib\site-packages\sklearn\utils\__init__.py", line 11, in <module>
from .validation import (as_float_array, check_arrays, safe_asarray,
File "C:\Python34\lib\site-packages\sklearn\utils\validation.py", line 17, in <module>
from .fixes import safe_copy
File "C:\Python34\lib\site-packages\sklearn\utils\fixes.py", line 18, in <module>
from .testing import ignore_warnings
File "C:\Python34\lib\site-packages\sklearn\utils\testing.py", line 36, in <module>
from nose.tools import assert_equal
What should I do?
I think your pip3 and python3 versions are mismatched, possibly due to multiple versions of Python 3 installed.
Run pip3 --version to find the exact Python version that pip3 is associated with and the location of site-packages directory.
Then, run python3 and look at the location of site-packages using:
import site
site.getsitepackages()
If you see different directory locations then that's your problem - multiple versions of Python 3. If it is indeed the problem, remove both the versions, re-install the correct one and then install your packages.
I hope this helps!

Categories