正确 setup.py 混合 Python 和 C++
correct setup.py for mixing Python and C++
我正在尝试混合使用这两种语言,并且我正在遵循 pybind here. I actually checked 提供的很好的示例来改进它,这样我就可以在编译函数不可用时回退到 Python 函数'存在。我现在遇到的问题是我的 configure.py
没有构建正确的包。让我开发:我的代码结构是这样的:
$ tree .
.
├── AUTHORS.md
├── CMakeLists.txt
├── LICENSE
├── MANIFEST.in
├── Makefile
├── README.md
├── conda.recipe
│ ├── bld.bat
│ └── ...
├── docs
│ ├── Makefile
│ └── ...
├── cmake_example
│ ├── __init__.py
│ ├── __main__.py
│ ├── geometry
│ │ ├── __init__.py
│ │ ├── triangle.py
│ │ └── ...
│ ├── quadrature
│ │ ├── __init__.py
│ │ ├── legendre
│ │ └── ...
│ └── utils
│ ├── __init__.py
│ ├── classes.py
│ └── ...
├── pybind11
│ ├── CMakeLists.txt
│ └── ...
├── setup.py
├── src
│ └── main.cpp
└── tests
└── test.py
我在这里放置省略号是为了简化目录结构,但您可以看到有几个模块。现在我的 setup.py
文件看起来像这样
import os
import re
import sys
import platform
import subprocess
import glob
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from distutils.version import LooseVersion
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
if platform.system() == "Windows":
cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
if cmake_version < '3.1.0':
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable]
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
build_args += ['--', '-j2']
env = os.environ.copy()
env['CXXFLAGS'] = '{} -DVERSION_INFO=\"{}\"'.format(env.get('CXXFLAGS', ''),
self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
kwargs = dict(
name="cmake_example",
ext_modules=[CMakeExtension('cmake_example._mymath')],
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
packages='cmake_example',
)
# likely there are more exceptions
try:
setup(**kwargs)
except subprocess.CalledProcessError:
print("ERROR: Cannot compile C accelerator module, use pure python version")
del kwargs['ext_modules']
setup(**kwargs)
这是我从 中提取的。当我尝试使用 python setup.py bdist_wheel
构建轮子,然后使用 pip install .
安装时,我无法使用我的代码,因为它抱怨找不到包:
>>> import cmake_example
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/aaragon/Local/cmake_example/cmake_example/__init__.py", line 11, in <module>
from .geometry import Triangle
ModuleNotFoundError: No module named 'cmake_example.geometry'
如果我在 setup.py
列表中手动添加 packages=['cmake_example', cmake_example.geometry]
那么它就可以工作,但我认为这不是正确的方法,因为很难跟上添加新模块。我在某个地方看到我可以替换该行并使用 setuptools 的 findpackages
,但此函数不会将 cmake_example
添加到模块中,因此它仍然会中断。做我想做的事情的正确方法是什么?
If I manually add in setup.py the list with packages=['cmake_example', cmake_example.geometry] then it works, but I don't think this is the right way to do it because it would be super hard to keep up with adding new modules.
要么您手动进行,要么在 难以跟上添加新模块 时 setuptools.find_packages
。像这样使用:
from setuptools import setup, find_packages
setup(
name="HelloWorld",
version="0.1",
packages=find_packages(),
)
我正在尝试混合使用这两种语言,并且我正在遵循 pybind here. I actually checked configure.py
没有构建正确的包。让我开发:我的代码结构是这样的:
$ tree .
.
├── AUTHORS.md
├── CMakeLists.txt
├── LICENSE
├── MANIFEST.in
├── Makefile
├── README.md
├── conda.recipe
│ ├── bld.bat
│ └── ...
├── docs
│ ├── Makefile
│ └── ...
├── cmake_example
│ ├── __init__.py
│ ├── __main__.py
│ ├── geometry
│ │ ├── __init__.py
│ │ ├── triangle.py
│ │ └── ...
│ ├── quadrature
│ │ ├── __init__.py
│ │ ├── legendre
│ │ └── ...
│ └── utils
│ ├── __init__.py
│ ├── classes.py
│ └── ...
├── pybind11
│ ├── CMakeLists.txt
│ └── ...
├── setup.py
├── src
│ └── main.cpp
└── tests
└── test.py
我在这里放置省略号是为了简化目录结构,但您可以看到有几个模块。现在我的 setup.py
文件看起来像这样
import os
import re
import sys
import platform
import subprocess
import glob
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from distutils.version import LooseVersion
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
if platform.system() == "Windows":
cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
if cmake_version < '3.1.0':
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable]
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
build_args += ['--', '-j2']
env = os.environ.copy()
env['CXXFLAGS'] = '{} -DVERSION_INFO=\"{}\"'.format(env.get('CXXFLAGS', ''),
self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
kwargs = dict(
name="cmake_example",
ext_modules=[CMakeExtension('cmake_example._mymath')],
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
packages='cmake_example',
)
# likely there are more exceptions
try:
setup(**kwargs)
except subprocess.CalledProcessError:
print("ERROR: Cannot compile C accelerator module, use pure python version")
del kwargs['ext_modules']
setup(**kwargs)
这是我从 python setup.py bdist_wheel
构建轮子,然后使用 pip install .
安装时,我无法使用我的代码,因为它抱怨找不到包:
>>> import cmake_example
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/aaragon/Local/cmake_example/cmake_example/__init__.py", line 11, in <module>
from .geometry import Triangle
ModuleNotFoundError: No module named 'cmake_example.geometry'
如果我在 setup.py
列表中手动添加 packages=['cmake_example', cmake_example.geometry]
那么它就可以工作,但我认为这不是正确的方法,因为很难跟上添加新模块。我在某个地方看到我可以替换该行并使用 setuptools 的 findpackages
,但此函数不会将 cmake_example
添加到模块中,因此它仍然会中断。做我想做的事情的正确方法是什么?
If I manually add in setup.py the list with packages=['cmake_example', cmake_example.geometry] then it works, but I don't think this is the right way to do it because it would be super hard to keep up with adding new modules.
要么您手动进行,要么在 难以跟上添加新模块 时 setuptools.find_packages
。像这样使用:
from setuptools import setup, find_packages
setup(
name="HelloWorld",
version="0.1",
packages=find_packages(),
)