Installing python-dev in virtualenv

Viewed 11493

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

5 Answers

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

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.

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

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

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/

Related