distutils include_dirs有相对路径?

时间:2017-09-06 14:47:13

标签: python-3.x distutils

我有一个C ++包,我之前在Python扩展中构建了它。现在我想在distutils下正确组织它。 (在Windows 7上使用Python 3.6.2(一个全新的安装)。)这是我的setup.py,到目前为止:

from distutils.core import setup, Extension

drawbox_module = Extension(
    "drawbox", 
    include_dirs = ['..\Drawbox Library', '..\Drawbox Tools'],
    sources = ["PyDrawbox.cpp", 'Drawbox.Win.cpp'])

setup(name="drawbox", 
      version="1.0",
      ext_modules= [drawbox_module])

但是,当我实际尝试构建时,它似乎忽略了我的include目录,它们根本就不在它生成的命令行中:

C:\Users\Ken\Desktop\Drawbox\Python36 backup>py setup.py build
running build
running build_ext
building 'drawbox' extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /
nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Python36\include -IC:\Python36\include "-IC
:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Fil
es (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x8
6)\Windows Kits\10\include\10.0.14393.0\ucrt" "-IC:\Program Files (x86)\Windows
Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\includ
e\10.0.14393.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.14
393.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\winrt"
/EHsc /TpPyDrawbox.cpp /Fobuild\temp.win-amd64-3.6\Release\PyDrawbox.obj
PyDrawbox.cpp
PyDrawbox.cpp(2): fatal error C1083: Cannot open include file: 'Drawbox.h': No s
uch file or directory

包含文件就可以了:

C:\Users\Ken\Desktop\Drawbox\Python36 backup>dir "..\Drawbox Library"
 Directory of C:\Users\Ken\Desktop\Drawbox\Drawbox Library

07/21/2014  07:11 PM    <DIR>          .
07/21/2014  07:11 PM    <DIR>          ..
07/26/2001  08:45 AM             1,407 Color.cpp
05/19/2005  08:00 AM             5,129 Color.h
06/29/2002  11:33 AM           117,028 Drawbox.68K.Lib
01/23/2008  03:33 AM            29,915 Drawbox.h
. . .

为什么我的include目录被忽略了?使用这些目录的相对路径是否存在问题?

如果有人想要深入了解,这里是打开DISTUTILS_DEBUG的输出:

C:\Users\Ken\Desktop\Drawbox\Python36 backup>set DISTUTILS_DEBUG=1

C:\Users\Ken\Desktop\Drawbox\Python36 backup>py setup.py build
options (after parsing config files):
options (after parsing command line):
option dict for 'build' command:
  {}
running build
Distribution.get_command_obj(): creating 'build' command object
running build_ext
Distribution.get_command_obj(): creating 'build_ext' command object
building 'drawbox' extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /
nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Python36\include -IC:\Python36\include "-IC
:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Fil
es (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x8
6)\Windows Kits\10\include\10.0.14393.0\ucrt" "-IC:\Program Files (x86)\Windows
Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\includ
e\10.0.14393.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.14
393.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\winrt"
/EHsc /TpPyDrawbox.cpp /Fobuild\temp.win-amd64-3.6\Release\PyDrawbox.obj
PyDrawbox.cpp
PyDrawbox.cpp(2): fatal error C1083: Cannot open include file: 'Drawbox.h': No s
uch file or directory
Traceback (most recent call last):
  File "C:\Python36\lib\distutils\_msvccompiler.py", line 382, in compile
    self.spawn(args)
  File "C:\Python36\lib\distutils\_msvccompiler.py", line 501, in spawn
    return super().spawn(cmd)
  File "C:\Python36\lib\distutils\ccompiler.py", line 909, in spawn
    spawn(cmd, dry_run=self.dry_run)
  File "C:\Python36\lib\distutils\spawn.py", line 38, in spawn
    _spawn_nt(cmd, search_path, dry_run=dry_run)
  File "C:\Python36\lib\distutils\spawn.py", line 81, in _spawn_nt
    "command %r failed with exit status %d" % (cmd, rc))
distutils.errors.DistutilsExecError: command ['"C:\\Program Files (x86)\\Microso
ft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe"', '/c', '/nologo', '/Ox', '/W
3', '/GL', '/DNDEBUG', '/MD', '-IC:\\Python36\\include', '-IC:\\Python36\\includ
e', '"-IC:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE"', '"
-IC:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\INCLUDE"',
'"-IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\ucrt"', '"
-IC:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6.1\\include\\um"', '"-IC:\
\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\shared"', '"-IC:\
\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\um"', '"-IC:\\Pro
gram Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\winrt"', '/EHsc', '/T
pPyDrawbox.cpp', '/Fobuild\\temp.win-amd64-3.6\\Release\\PyDrawbox.obj'] failed
with exit status 2

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    ext_modules= [drawbox_module])
  File "C:\Python36\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Python36\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python36\lib\distutils\command\build.py", line 135, in run
    self.run_command(cmd_name)
  File "C:\Python36\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python36\lib\distutils\command\build_ext.py", line 339, in run
    self.build_extensions()
  File "C:\Python36\lib\distutils\command\build_ext.py", line 448, in build_exte
nsions
    self._build_extensions_serial()
  File "C:\Python36\lib\distutils\command\build_ext.py", line 473, in _build_ext
ensions_serial
    self.build_extension(ext)
  File "C:\Python36\lib\distutils\command\build_ext.py", line 533, in build_exte
nsion
    depends=ext.depends)
  File "C:\Python36\lib\distutils\_msvccompiler.py", line 384, in compile
    raise CompileError(msg)
distutils.errors.CompileError: command ['"C:\\Program Files (x86)\\Microsoft Vis
ual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe"', '/c', '/nologo', '/Ox', '/W3', '/
GL', '/DNDEBUG', '/MD', '-IC:\\Python36\\include', '-IC:\\Python36\\include', '"
-IC:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE"', '"-IC:\\
Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\INCLUDE"', '"-IC:
\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\ucrt"', '"-IC:\\
Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6.1\\include\\um"', '"-IC:\\Progr
am Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\shared"', '"-IC:\\Progr
am Files (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\um"', '"-IC:\\Program F
iles (x86)\\Windows Kits\\10\\include\\10.0.14393.0\\winrt"', '/EHsc', '/TpPyDra
wbox.cpp', '/Fobuild\\temp.win-amd64-3.6\\Release\\PyDrawbox.obj'] failed with e
xit status 2

1 个答案:

答案 0 :(得分:0)

您可能需要将include_dirs传递给setup方法,而不是Extension。至少在我的测试中,这种方式相对路径在cl.exe的命令行中正确显示。

此外,如果它们包含反斜杠=&gt;,请确保在路径前使用 r r"..\path\to\includes"

这来自我制作的cython模块,添加了特定的include_dir用于测试:

import sys
from distutils.core import setup
from Cython.Build import cythonize
import numpy as np

extension = cythonize("unpack.pyx", build_dir="build")[0]
extension.extra_link_args.append("/OUT:unpack.pyd")   # VCPP specific!

setup(
    name = 'unpack',
    include_dirs = [np.get_include(), "..\Drawbox Library"],
    ext_modules = [extension],
    script_args = ["build_ext", "--inplace"],    
)

这导致"-I..\Drawbox Tools"出现在cl.exe的命令行中,由distutils打印。