我对python setuptools和dist还不太熟悉,我似乎无法导入一个c++包装器模块来使用这些函数。在
编译的.so文件显示在已安装的-文件.txt在pip安装之后,但是导入包装器却没有。在
设置.py
import subprocess
import os
from pathlib import Path
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
class CMakeExtension(Extension):
def __init__(self, name):
Extension.__init__(self, name, sources=[])
class CMakeBuild(build_ext):
def run(self):
for ext in self.extensions:
self.build_cmake(ext)
super().run()
def build_cmake(self, ext):
try:
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)
)
cwd = Path().absolute()
# these dirs will be created in build_py, so if you don't have
# any python sources to bundle, the dirs will be missing
build_temp = Path(self.build_temp)
build_temp.mkdir(parents=True, exist_ok=True)
extdir = Path(self.get_ext_fullpath(ext.name))
extdir.mkdir(parents=True, exist_ok=True)
pyenv_root = os.environ.get("PYENV_ROOT")
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}",
"-DCMAKE_BUILD_TYPE=Release",
"-DTRANSIT_INCLUDE_TESTS:BOOL=OFF",
]
if pyenv_root is not None:
cmake_args += [f"-DPYTHON_EXECUTABLE={pyenv_root}/shims/python"]
build_args = ["--config", "Release", "--", "-j2"]
env = os.environ.copy()
self.announce("Running CMake prepare", level=3)
subprocess.check_call(["cmake", cwd] + cmake_args, cwd=build_temp, env=env)
self.announce("Building extensions")
cmake_cmd = ["cmake", "--build", "."] + build_args
subprocess.check_call(cmake_cmd, cwd=build_temp)
setup(
name="bgtfs_py_lib",
version="3.2.2",
long_description="",
zip_safe=False,
install_requires=[
"redis==2.10.6",
"cffi==1.11.5",
"numpy==1.15.3",
"patricia-trie==10",
"PuLP==1.6.8",
"py-lz4framed==0.13.0",
"pycparser==2.19",
"pyparsing==2.2.2",
"pypng==0.0.18",
"pyproj==1.9.5.1",
"python-graph-core==1.8.2",
"pytz==2018.6",
"redis==2.10.6",
"requests==2.21.0",
"six==1.11.0",
"tabulate==0.8.2",
"unicodecsv==0.14.1",
"Unidecode==1.0.22",
],
ext_modules=[CMakeExtension("bgtfs_py_lib.bgtfs_py_lib_wrapper")],
cmdclass=dict(build_ext=CMakeBuild),
packages=find_packages(exclude=["tests"]),
package_data={"": "*.so"},
)
CMakeLists.txt文件
^{pr2}$我尝试使用virtualenv来隔离在我的项目中运行所需的模块。在
以下是文件结构:
.
|-- CMakeLists.txt
|-- README.md
|-- bgtfs_py_lib
| |-- __init__.py
| |-- bgtfs_handler
|-- cpp
| |-- BgtfsFeedHandler.cpp
| |-- BgtfsFeedHandler.h
| |-- bgtfsPyLibWrapper.cpp
| `-- pybindCasts.cpp
|-- deploy.sh
|-- make.sh
|-- pybind11
|-- setup.py
|-- submodules
|-- test.sh
`-- tests
|-- __init__.py
|-- __pycache__
|-- fixtures
|-- test.py
`-- test_functions.py
bgtfs_py_lib中的init.py文件如下所示。包装器的功能正在公开。在
import bgtfs_py_lib_wrapper as _bgtfs_py_lib
from bgtfs_py_lib.bgtfs_handler.bgtfs_handler import BgtfsHandler
在另一个项目中,使用git+ssh和egg进行pip安装。在
git+ssh://git@github.com/path/to/project.git@build/production/setup#egg=bgtfs_py_lib
当我ctrl+space在pyCharm中找到包装器模块并显示类。
该模块位于二进制Skeletons目录中,但是
将bgtfs_py_lib_wrapper导入为_bgtfs_py_lib只是不起作用,并引发以下异常:ModuleNotFoundError: No module named 'bgtfs_py_lib_wrapper'
我终于解决了
结果是CMakeLists.txt文件需要更改,因为cmake_参数和内部版本参数之间存在不一致。所以CMakeList.txt文件文件现在如下所示:
CMakeLists.txt文件
以及设置.py文件现在是:
设置.py
^{pr2}$扩展文件不会被输出到bgtfs_py_lib目录,而是被输出到虚拟环境中,项目本身是必需的
向Sergei大声呼喊,请他帮忙解决问题
相关问题 更多 >
编程相关推荐