Pip: ImportError: cannot import name 'sysconfig' (Ubuntu 16.04)

Created on 3 May 2018  ·  16Comments  ·  Source: pypa/pip

  • Pip version:
  • Python version: 3.6.5
  • Operating system: Ubuntu 16.04.4 LTS

Description:

Similar problem to #5356 but with a different version of Ubuntu. The solution proposed in #5356 doesn't solve the problem with this version of Ubuntu

After installing Python 3.6 from jonathonf repository in a clean version of Ubuntu, I've found the following error while trying to install Pip

Note that trying to install Pip with the Python version that comes by default (3.5.2 ) doesn't give any problem.

What I've run:

python3.6 get-pip.py

The error message:

Traceback (most recent call last):
  File "get-pip.py", line 20649, in <module>
    main()
  File "get-pip.py", line 197, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip._internal
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/__init__.py", line 42, in <module>
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/cmdoptions.py", line 16, in <module>
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/index.py", line 25, in <module>
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/download.py", line 35, in <module>
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/locations.py", line 10, in <module>
ImportError: cannot import name 'sysconfig'
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 "get-pip.py", line 20649, in <module>
    main()
  File "get-pip.py", line 197, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip._internal
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/__init__.py", line 42, in <module>
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/cmdoptions.py", line 16, in <module>
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/index.py", line 25, in <module>
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/download.py", line 35, in <module>
  File "/tmp/tmpodo51doi/pip.zip/pip/_internal/locations.py", line 10, in <module>
ImportError: cannot import name 'sysconfig'

Workaround I have found

  • Download Python source from https://www.python.org/
  • Decompress the source code
  • Install the following dependencies:
    sudo apt-get install zlib1g-dev (needed to compile Python)
    sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev (needed by Pip to make SSL requests)
  • Compile and install Python:
./configure
make
make install
  • Python 3.6 with Pip should be installed.
downstream auto-locked support

Most helpful comment

I also saw a similar error.

$ sudo python3.6 get-pip.py
Traceback (most recent call last):
  File "get-pip.py", line 20061, 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/tmpxvd4x9zn/pip.zip/pip/__init__.py", line 26, in <module>
  File "/tmp/tmpxvd4x9zn/pip.zip/pip/utils/__init__.py", line 23, in <module>
  File "/tmp/tmpxvd4x9zn/pip.zip/pip/locations.py", line 9, in <module>
ImportError: cannot import name 'sysconfig'

However, by installing python3-distutils it was possible to avoid this error.
sudo apt-get install python3-distutils

All 16 comments

If you care to read the description of my issue, you'll find that I did already look at that one. The solution proposed there is not applicable with this version of Ubuntu.

There is no need to be rude. But for me compiling python on your on it is not a solution.

That import error is the cause. I will try to reproduce later.

@jonbesga Thanks for the workaround, also experiencing this issue with the exact same versions of python 3.6 and ubuntu...

@Tarliton I agree that's is not a solution. I'll rename it as a workaround.

Regarding the rudeness, sorry about that, but telling me to see the issue that I've already referenced to in my issue denoted that you posted the comment without reading my issue in the first place.

@jonbesga my bad, but I should have said to try to run the commands the other issue mentioned.
Like:

python -c "from distutils import sysconfig"

I just did that in a VM with Ubuntu 16.04.2 and got no problem. And installed pip.
I will try later with 16.04.4.

I can confirm that in 16.04.4,

python3.6 -c "from distutils import sysconfig"

Produces the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'sysconfig'
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 "<string>", line 1, in <module>
ImportError: cannot import name 'sysconfig'

Tried installing Python 3.6 from ppa:deadsnakes/ppa instead of from ppa:jonathonf/python-3.6 and it's installing Pip correctly. It looks like the version of jonathonf it's broken in some way.

Edit: Just noticed that some comments in the Stack Overflow answer adress this problem with the jonathonf repository

I can confirm that removing ppa:jonathonf/python-3.6 and then adding ppa:deadsnakes/ppa and reinstalling python3.6 fixes this problem on Linux Mint 18.3.

Info on removing current PPA: https://askubuntu.com/a/173209/823909
Adding new Python3.6 PPA: https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get

@tomrunia I suggest you to change the link to https://askubuntu.com/a/310 as your current link is not pointing to the best answer.

I also saw a similar error.

$ sudo python3.6 get-pip.py
Traceback (most recent call last):
  File "get-pip.py", line 20061, 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/tmpxvd4x9zn/pip.zip/pip/__init__.py", line 26, in <module>
  File "/tmp/tmpxvd4x9zn/pip.zip/pip/utils/__init__.py", line 23, in <module>
  File "/tmp/tmpxvd4x9zn/pip.zip/pip/locations.py", line 9, in <module>
ImportError: cannot import name 'sysconfig'

However, by installing python3-distutils it was possible to avoid this error.
sudo apt-get install python3-distutils

@wakiyamap That's seems a solution that works for #5356 with Ubuntu 18.04 LTS. However, in Ubuntu 16.04.4 LTS that package has been renamed and doesn't solve the issue.

@jonbesga I used this version.
Ubuntu Xenial (16.04 LTS) (GNU/Linux 4.4.94-mainline-rev1 aarch64 )
ppa:jonathonf/python-3.6

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"

CPU is different, so It may not be related to this case.
sorry.

I had a similar problem on Ubuntu 16.04 (specifically "from distutils import dist, sysconfig
ImportError: cannot import name 'dist'") and "sudo apt-get install python3-distutils" solved it. Thank you!

@RandyRomero @wakiyamap thanks, apt install python3-distutils works for me too.

@jonbesga I'm on 16.04.4 LTS and it worked. EDIT - I'm using vagrant, did "vagrant box update" a moment ago, I'm on 20180504)

Generally, not a pip-related issue if you ask me, it has more to do with Ubuntu packaging

@wakiyamap Thanks, your solution apt install python3-distutils worked for me also.
I have just upgraded to Python 3.6.5 on Ubuntu 16.04.4 and imports for matplotlib and calls to pip all failed with the same error.

This fix needs to be widely disseminated.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings