python - 将命令行参数传递给 pip install

标签 python pip setup.py

我目前正在处理一个导入 Fortran 模块的 Python 项目。 setup.py看起来很像

from numpy.distutils.core import Extension
from numpy.distutils.core import setup

ext = Extension(
    name = "fortran_module",
    sources = ["fortran_module.f90"],
    extra_f90_compile_args = ["-some -compile -arguments"]
)

setup(
    ...,
    ...,
    ...,
    ext_modules = [ext],
    ...,
    ...,
    ...
)

我目前正在使用 pip install -e . 安装它,这工作正常。
但有时我需要更改 extra_f90_compile_args我想在安装过程中将它们作为命令行参数使用 pip而不是改变 setup.py文件。例如这样的事情:
pip install -e --compile_args="-O3 -fbacktrace -fbounds-check -fopenmp" .

是否可以通过 pip 传递命令行参数进setup.py文件?

与此相关,我还想将不同的设置选项传递给 setup.py .例如
pip install -e --setup=setup1 .

或者
pip install -e --setup=setup2 .

并取决于是否setup1setup2或者它们都没有通过,我想包含不同的 Fortran 源文件并使用不同的 extra_f90_compile_args 编译它们.

这可能吗?

编辑:让我们考虑以下示例:Fortran 模块与 OpenMP 并行化。现在我想让用户决定是否并行编译。也许 OpenMP 库不可用,用户需要编译串行版本。

我的 setup.py现在看起来如下
from numpy.distutils.core import Extension
from numpy.distutils.core import setup
import os
from setuptools.command.install import install as _install

extra_f90_compile_args = ""
extra_link_args = ""

class install(_install):
    user_options = _install.user_options + [('build=', None, None)]

    def initialize_options(self):
        _install.initialize_options(self)
        self.build = None

    def finalize_options(self):
        _install.finalize_options(self)

    def run(self):
        global extra_f90_compile_args
        global extra_link_args
        if(self.build == "parallel"):
            extra_f90_compile_args += "-fopenmp"
            extra_link_args += "-lgomp"
            os.makedirs("~/test/")
        _install.run(self)

ext = Extension(
    name="test_module.fortran_module",
    sources=["test_module/fortran_module.f90"],
    extra_f90_compile_args=[extra_f90_compile_args],
    extra_link_args=[extra_link_args]
)

setup(
    name="test_module",
    packages=["test_module"],
    ext_modules=[ext],
    cmdclass={'install': install}
)

如果我安装这个
pip install . --install-option="--build=parallel"

它正在执行 if 中的代码-堵塞。我创建了 test/目录只是为了检查这个。如 build未给出或与 parallel 不同, test/未创建目录。

但是,代码不是用 OpenMP 编译的。我认为这是因为Extension对象是在调用 setup() 之前创建的,在那里评估参数。我想先评估参数,然后创建 Extension对象取决于参数,然后调用 setup() .

我怎样才能做到这一 pip ?

最佳答案

处理两者 --install-option--global-option如果在我看来有多个选项,则似乎非常困惑。我要加this real life solution

取而代之的是环境变量,并在安装时设置它们。

在 setup.py 中:

import os
config_var = os.environ.get("MY_PARAM", None)
if config_var:
    do_something()
setup()

并像这样调用 pip:MY_PARAM="Some_value" pip install my_module

关于python - 将命令行参数传递给 pip install,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47210058/

相关文章:

python - pip 安装项目而不复制整个项目文件夹

Python pip 安装 : error: package directory 'b' does not exist

python - 运行实时输出到 Tkinter GUI 的进程

python - 使用selenium在.click()之后获取新的html

python - 导入错误没有名为 zlib 的模块(brew 安装了 python)

python - 即使我尝试更新它也无法更新 pip3

python - Python 3 中的 writerow 总是在原始行之间写一个空行,Python2 工作得很好。为什么?

python - 通过 SWIG 在 python 函数中使用 GList 数据类型

python - 无法使用 pip install 安装 pika

python - setup.cfg Python 项目的单一来源包版本