Subomi
Subomi

Reputation: 410

Installing python-dev in virtualenv

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

Upvotes: 18

Views: 19976

Answers (6)

wutangforever
wutangforever

Reputation: 246

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).

Upvotes: 4

wiz_lee
wiz_lee

Reputation: 938

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

Upvotes: 4

user1953366
user1953366

Reputation: 1611

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/

Upvotes: -4

Jeremee
Jeremee

Reputation: 41

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.

Upvotes: 1

Little Brain
Little Brain

Reputation: 2857

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

Upvotes: 0

Andrew Wang
Andrew Wang

Reputation: 26

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

Upvotes: 0

Related Questions