简体   繁体   English

在 virtualenv 中安装 python-dev

[英]Installing python-dev in virtualenv

I am trying to install mysqlclient for python in my virtualenv.我正在尝试在我的 virtualenv 中为 python 安装 mysqlclient。 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.经过一番研究,我发现我需要安装 python-dev。 I have it installed in my main directories (ie /usr/bin ... ) but its not installed virtualenv but each time i type:我将它安装在我的主目录中(即 /usr/bin ... )但它没有安装 virtualenv 但每次我输入:

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.显示其可用性,在 virtualenv 之外 mysqlclient 正确安装。 The issue is how to rectify python-dev installation into the virtualenv问题是如何将 python-dev 安装纠正到 virtualenv

This is because virtualenv not link include and lib directory to virtualenv's directory.这是因为 virtualenv 没有将 include 和 lib 目录链接到 virtualenv 的目录。 I solve this by using anaconda virtual env.我通过使用 anaconda 虚拟环境解决了这个问题。 wish helps.希望有帮助。

在 Ubuntu 16.04 上,我按照这些说明使用 python3-dev 成功设置了 Python 虚拟环境。

Maybe not the best way but my solution was installing miniconda .也许不是最好的方法,但我的解决方案是安装miniconda You can also probably try the larger Anaconda distribution.您也可以尝试更大的 Anaconda 发行版。 I believe these distributions have built-in python-dev.我相信这些发行版都内置了 python-dev。

I am currently having issue in Ubuntu 20 where the default python is python 3.8.我目前在 Ubuntu 20 中遇到问题,其中默认的 python 是 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.当我尝试在需要使用 python 3.9 的pipenv创建的 virtualenv 中安装 Pillow 时,这会导致问题。

The simplest solution I found so far is to install python-dev for 3.9到目前为止我发现的最简单的解决方案是为 3.9 安装 python-dev

sudo apt install python3.9-dev

Previously I install python3-dev which always default to 3.8以前我安装python3-dev ,它总是默认为 3.8

I believe python-dev will already be installed.我相信已经安装了 python-dev。 you may not be including the header files path while compiling.编译时可能没有包含头文件路径。

u can search for Python.h by:您可以通过以下方式搜索 Python.h:

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:获得路径后,您需要通过添加该路径来编译文件,例如,如果您使用的是 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:如果您使用的是 make 实用程序,您还可以使用 export as 在 C_INCLUDE_PATH(或 CPP_INCLUDE_PATH,如果使用 C++)中设置路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM