有没有一种方法可以从应用程序中的代码安装python库?

时间:2020-06-17 12:17:52

标签: python installation pip

是否有一种无需使用终端命令即可安装opencv或任何其他python库的方法? 我用python开发了一个可双击运行的应用程序,我想使它适用于任何不知道如何安装python库的人。我想从代码安装我所有的库。 我已经尝试过了:

pip install pip,opencv-python
import cv2
#and a lot of code

但是我收到无效的syntaxError。

3 个答案:

答案 0 :(得分:1)

通过脚本通过子进程调用pip的命令行界面来从脚本安装软件包。

import pip


def install(package):
    if hasattr(pip, "main"):
        pip.main(["install", package])
    else:
        pip._internal.main(["install", package])


if __name__ == "__main__":
    install("opencv-python")

答案 1 :(得分:0)

您可以下一步:

<IfModule security2_module>
        # Default Debian dir for modsecurity's persistent data
        SecDataDir /var/cache/modsecurity

        # Include all the *.conf files in /etc/modsecurity.
        # Keeping your local configuration in that directory
        # will allow for an easy upgrade of THIS file and
        # make your life easier
        IncludeOptional /etc/modsecurity/*.conf
        Include /etc/modsecurity/rules/*.conf

        # Include OWASP ModSecurity CRS rules if installed
</IfModule>

答案 2 :(得分:0)

您的用户如何获得您的程序?如果使用setuptools创建软件包,则可以添加“ install_requires”元素并包括所有依赖项。安装后将它们添加到环境中。

import setuptools
from setuptools import setup

setup(
    name="programname",
    version="1.4.0",
    author="Dude",
    author_email="blah@blah",
    description="Some Program",
    long_description=open("readme.md").read(),
    license=open("license.md").read(),
    packages=setuptools.find_packages(),
    url="",
    install_requires=["psycopg2"],
    classifiers=[
        "Programming Language :: Python :: 3"
    ],
)