为什么`pyvenv`不安装`python-config`? [英] Why `pyvenv` does not install `python-config`?
问题描述
我在MacOS(10.11)下遇到了这个问题,但是在各种Linux下也遇到了同样的问题.我安装了官方" Python3软件包,它进入/Library/Frameworks/Python.framework/Versions/3.4
.(注意:以下示例使用Python 3.4,但该问题在3.5上仍然存在.由于缺少管理员权限,如果在3.6中已解决此问题,则我无法访问具有Python 3.6的计算机.)
I have run into this under MacOS (10.11), but have seen the same problem under various Linuxes as well. I installed the "official" Python3 package, it goes into /Library/Frameworks/Python.framework/Versions/3.4
. (Note: the examples below use Python 3.4, but the issue persists with 3.5 as well. I have no access to a machine that has Python 3.6 due to lack of admin privileges, should the issue have been solved in 3.6.)
我需要虚拟环境,并且需要 python-config
脚本来找出Python3使用的库,因为我的项目结合了Python和C ++代码.
I need virtual environments and I need the python-config
script to figure out which libraries Python3 uses because my project combines Python and C++ code.
如果我使用 virtualenv
设置虚拟环境,一切都很好:
If I set up the virtual environment with virtualenv
, everything is fine:
$ which virtualenv
/Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenv
$ virtualenv --python=$(which python3) vienv
Running virtualenv with interpreter /Library/Frameworks/Python.framework/Versions/3.4/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.4'
[...blabla...]
Installing setuptools, pip, wheel...done.
$ source vienv/bin/activate
(vienv) $ which python-config
/Users/XXXXX/DEV/STANDALONE/misc/python/vienv/bin/python-config
(vienv) $ python-config --libs
-lpython3.4m -ldl -framework CoreFoundation
但是, pyvenv
忘记在虚拟环境中设置 python-config
:
However, pyvenv
forgets to set up python-config
in the virtual environment:
$which pyvenv
/Library/Frameworks/Python.framework/Versions/3.4/bin/pyvenv
$ pyvenv pe
$ source pe/bin/activate
(pe) $ which python-config
/usr/bin/python-config # !!! Here's the problem !!!
(pe) $ python-config --libs
-lpython2.7 -ldl -framework CoreFoundation
换句话说,即使我激活了虚拟环境,系统默认的Python2 python-config
仍保留在我的 PATH
中.
In other words, the system default Python2 python-config
stays in my PATH
even though I activated the virtual environment.
现在您可以说:什么问题?使用 virtualenv
并完成它.但是,需要通过 pip
额外安装 virtualenv
,这需要我并不总是拥有的管理员权限. pyvenv
(OTOH)是Python3附带的,或者至少这是我的理解.
Now you could say: What's the problem? Use virtualenv
and be done with it. However, virtualenv
needs to be installed extra via pip
and this requires admin rights which I do not always have. pyvenv
, OTOH, comes with Python3, or at least that was my understanding.
您还可以说:为什么不使用 pip
在虚拟环境中安装 python-config
?原因如下:
You could also say: Why don't you just install python-config
in your virtual environment using pip
? Here's why:
(pe) $ pip install python-config
Requirement already satisfied (use --upgrade to upgrade): python-config in ./pe/lib/python3.4/site-packages
Cleaning up...
是的,有 package ,但是脚本本身未安装到虚拟环境的 bin
子目录中.
Yes, the package is there, but the script itself is not installed into the bin
subdirectory of the virtual environment.
摘要:我想配置我的项目,以便只能使用Python3标准模块/工具来安装它,并且它不依赖于诸如 virtualenv
之类的其他东西.而且我不想缠扰系统管理员:-)
Summary: I would like to configure my project so that it can be installed only using the Python3 standard modules/tools, and it does not depend on extra stuff such as virtualenv
. And I do not want to pester sysadmins :-)
问题:是否有一种变通办法来正确安装 pyvenv
python-config
?或者:如果我将C ++代码链接到虚拟环境中的特定Python3安装,还有另一种方法可以弄清楚应该使用哪些头文件和库?
Question: is there a workaround to get pyvenv
install python-config
properly? Or: is there another way to figure out which headers and libraries I should use if I link my C++ code against a particular Python3 installation in a virtual environment?
推荐答案
好吧,一年后,是时候回答我自己的问题了:-)
Well, after one year it's time to answer my own question :-)
此处遵循 python-config
脚本,该脚本由 virtualenv
安装到 $ {VENV}/bin
中.如果您使用 python3 -m venv $ {VENV}
,则只需将其手动复制到此位置,直到此问题得到解决(注意,存在
Here follows the python-config
script that is installed by virtualenv
into ${VENV}/bin
. If you use python3 -m venv ${VENV}
, then just copy it into this location manually until this issue gets fixed (NB there's a bug report from 2011 still without a fix as far as I can tell).
#!/usr/bin/env python3
"""
This python-config script was taken from a virtual environment
created by `virtualenv`.
The only change is the hash-bang line.
The user shall copy this to ${VENV}/bin during setup.
:author: unknown + AA
:date: 2018-02-23
"""
import sys
import getopt
import sysconfig
valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags',
'ldflags', 'help']
if sys.version_info >= (3, 2):
valid_opts.insert(-1, 'extension-suffix')
valid_opts.append('abiflags')
if sys.version_info >= (3, 3):
valid_opts.append('configdir')
def exit_with_usage(code=1):
sys.stderr.write("Usage: {0} [{1}]\n".format(
sys.argv[0], '|'.join('--'+opt for opt in valid_opts)))
sys.exit(code)
try:
opts, args = getopt.getopt(sys.argv[1:], '', valid_opts)
except getopt.error:
exit_with_usage()
if not opts:
exit_with_usage()
pyver = sysconfig.get_config_var('VERSION')
getvar = sysconfig.get_config_var
opt_flags = [flag for (flag, val) in opts]
if '--help' in opt_flags:
exit_with_usage(code=0)
for opt in opt_flags:
if opt == '--prefix':
print(sysconfig.get_config_var('prefix'))
elif opt == '--exec-prefix':
print(sysconfig.get_config_var('exec_prefix'))
elif opt in ('--includes', '--cflags'):
flags = ['-I' + sysconfig.get_path('include'),
'-I' + sysconfig.get_path('platinclude')]
if opt == '--cflags':
flags.extend(getvar('CFLAGS').split())
print(' '.join(flags))
elif opt in ('--libs', '--ldflags'):
abiflags = getattr(sys, 'abiflags', '')
libs = ['-lpython' + pyver + abiflags]
libs += getvar('LIBS').split()
libs += getvar('SYSLIBS').split()
# add the prefix/lib/pythonX.Y/config dir, but only if there is no
# shared library in prefix/lib/.
if opt == '--ldflags':
if not getvar('Py_ENABLE_SHARED'):
libs.insert(0, '-L' + getvar('LIBPL'))
if not getvar('PYTHONFRAMEWORK'):
libs.extend(getvar('LINKFORSHARED').split())
print(' '.join(libs))
elif opt == '--extension-suffix':
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
if ext_suffix is None:
ext_suffix = sysconfig.get_config_var('SO')
print(ext_suffix)
elif opt == '--abiflags':
if not getattr(sys, 'abiflags', None):
exit_with_usage()
print(sys.abiflags)
elif opt == '--configdir':
print(sysconfig.get_config_var('LIBPL'))
这篇关于为什么`pyvenv`不安装`python-config`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!