18

I am trying to install mysqlclient for python in my virtualenv. It fails with the following:

 #include "Python.h"

                ^

compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

After some research, i found out that i require python-dev installation. I have it installed in my main directories (i.e /usr/bin ... ) but its not installed virtualenv but each time i type:

sudo apt-get install python-dev

I get the following response:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 453 not upgraded.

Showing its availability, outside the virtualenv mysqlclient installs properly. The issue is how to rectify python-dev installation into the virtualenv

12
  • @downshift doesn't work .. python-dev executable not found. Commented Aug 14, 2017 at 18:45
  • 1
    @downshift python-dev is not an executable but a set of *.h header. Commented Aug 14, 2017 at 18:54
  • 3
    @Subomi.js What version of Python? For Python 3 you need python3-dev. Commented Aug 14, 2017 at 18:56
  • 2
    @Subomi How will apt-get install python3-dev add header files to the virtual environment? I am facing a similar issue where I require python3-dev for a virtual environment. Please let me know if you were able to solve this and how. Thanks. Commented Sep 18, 2018 at 1:00
  • 1
    @saharudra hi, once you activate your virtualenv. It changes your PATH variables and so when you do apt-get intsall python3-dev you're installing it into the proper location where the python3 compiler can locate the headers. Commented Sep 18, 2018 at 11:07

6 Answers 6

4

I am currently having issue in Ubuntu 20 where the default python is python 3.8. This causes issue when I try to install Pillow in a virtualenv created by pipenv that need to use python 3.9.

The simplest solution I found so far is to install python-dev for 3.9

sudo apt install python3.9-dev

Previously I install python3-dev which always default to 3.8

Sign up to request clarification or add additional context in comments.

Comments

4

pyenv (https://github.com/pyenv/pyenv) is simply magic as it allows you to install different versions of Python, dev included (list available versions with pyenv install --list). Sometimes I run into problems of the "Python library not found" sort and Ubuntu suggests building with "--enable-shared". This can be done with pyenv as follows:

CONFIGURE_OPTS=--enable-shared pyenv install 3.9-dev

It is also worth checking out pyenv-virtualenv (https://github.com/pyenv/pyenv-virtualenv).

Comments

1

Maybe not the best way but my solution was installing miniconda. You can also probably try the larger Anaconda distribution. I believe these distributions have built-in python-dev.

Comments

0

This is because virtualenv not link include and lib directory to virtualenv's directory. I solve this by using anaconda virtual env. wish helps.

Comments

0

On Ubuntu 16.04, I followed these instructions to successfully set up a Python virtual environment with python3-dev.

1 Comment

apt-get install build-essential libssl-dev libffi-dev (from the linked article) solved the issue but it downgraded GCC version, bringing another problem.
-4

I believe python-dev will already be installed. you may not be including the header files path while compiling.

u can search for Python.h by:

find <venv folder> | grep Python.h

If it is not able to find the file in your virtual environment, a simple command like below should install it:

pip install python-dev (or python2/3.x-dev)

Once u have the path, you need to compile your file by adding that path, for example if you are using conda:

gcc <<your prev command args>> -I/home/<user>/anaconda3/envs/<<vinv_name>>/include/python3.7m/

If you are using a make utility you can also set the path in C_INCLUDE_PATH (or CPP_INCLUDE_PATH if using c++) by using export as:

export C_INCLUDE_PATH=/home/<user>/anaconda3/envs/<<vinv_name>>/include/python3.7m/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.