python如何分发依赖包

在Python中,分发依赖包通常是指将一个项目所需的所有第三方库和资源打包成一个独立的文件,以便其他开发者可以轻松地安装和使用该项目,这可以简化项目的部署过程,确保所有依赖项都正确安装,并避免版本冲突,以下是如何使用Python分发依赖包的详细教程。

创新互联公司从2013年创立,是专业互联网技术服务公司,拥有项目成都网站制作、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元樊城做网站,已为上家服务,为樊城各地企业和个人服务,联系电话:028-86922220

1、创建虚拟环境

我们需要为项目创建一个虚拟环境,虚拟环境是一个独立的Python运行环境,它可以让我们为每个项目安装和管理不同的依赖库,使用虚拟环境可以避免不同项目之间的依赖库冲突。

在项目根目录下,运行以下命令创建虚拟环境:

python m venv venv

这将在项目根目录下创建一个名为venv的文件夹,其中包含Python解释器和相关依赖库,接下来,激活虚拟环境:

对于Windows系统:

venvScriptsactivate

对于Linux和macOS系统:

source venv/bin/activate

2、安装依赖库

在虚拟环境中,我们可以使用pip工具安装项目所需的所有依赖库,创建一个名为requirements.txt的文件,列出项目所需的所有依赖库及其版本。

numpy==1.21.0
pandas==1.3.0
matplotlib==3.4.2

运行以下命令安装这些依赖库:

pip install r requirements.txt

3、构建分发包

为了将项目及其依赖库打包成一个独立的文件,我们可以使用setuptoolswheel库,安装这两个库:

pip install setuptools wheel

接下来,在项目根目录下创建一个名为setup.py的文件,内容如下:

from setuptools import setup, find_packages
from wheel.bdist_wheel import bdist_wheel as bdist_wheel_original
import os
import sys
import platform
import shutil
import subprocess
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
from typing import List, Tuple, Union, Optional, Any, cast
from setuptools import Command, Extension, BuildMetaclass, build_ext, SetuptoolsError, InstallError, Distribution, EggInfo, get_metadata_version, load_entry_point, find_packages, EntryPoint, CommandExtensionBuilder, ExecutableCommand, PackageCommand, ZipFileCommand, tarfile, copytree, rmtree, install_headers, build_clib, build_scripts, build_py, build_ext_subpackage, build_clib_subpackage, build_scripts_subpackage, build_py_subpackage, build_ext_module, build_clib_module, build_scripts_module, build_py_module, build_ext_egg, build_clib_egg, build_scripts_egg, build_py_egg, build_ext_sdist, build_clib_sdist, build_scripts_sdist, build_py_sdist, build_ext_zipfile, build_clib_zipfile, build_scripts_zipfile, build_py_zipfile, build_ext_tarfile, build_clib_tarfile, build_scripts_tarfile, build_py_tarfile, build_ext_command, build_clib_command, build_scripts_command, build_py_command, build_ext:
    from setuptools.command import bdist_wheel as bdist_wheel2  # type: ignore[assignment]
    from setuptools.command import install as install2  # type: ignore[assignment]
    from setuptools.command import sdist as sdist2  # type: ignore[assignment]
    from setuptools.command import upload as upload2  # type: ignore[assignment]
    from setuptools.command import download as download2  # type: ignore[assignment]
    from setuptools.command import easy_install as easy_install2  # type: ignore[assignment]
    from setuptools.command import clean as clean2  # type: ignore[assignment]
    from setuptools.command import develop as develop2  # type: ignore[assignment]
    from setuptools.command import test as test2  # type: ignore[assignment]
    from setuptools.command import config as config2  # type: ignore[assignment]
    from setuptools.command import rebuild as rebuild2  # type: ignore[assignment]
    from setuptools.command import install_headers as install_headers2  # type: ignore[assignment]
    from setuptools.command import install_lib as install_lib2  # type: ignore[assignment]
    from setuptools.command import install_data as install_data2  # type: ignore[assignment]
    from setuptools.command import install_scripts as install_scripts2  # type: ignore[assignment]
    from setuptools.command import install as install3  # type: ignore[assignment]
    from setuptools.command import bdist as bdist3  # type: ignore[assignment]
    from setuptools.command import bdist_dumb as bdist4  # type: ignore[assignment]
    from setuptools.command import bdist_msi as bdist5  # type: ignore[assignment]
    from setuptools.command import bdist_rpm as bdist6  # type: ignore[assignment]
    from setuptools.command import bdist_wininst as bdist7  # type:ignore[assignment]
    from setuptools.command import register as register2  # type:ignore[assignment]
    from setuptools.command import upload_docs as upload3  # type:ignore[assignment]
    from setuptools.command import check as check2  # type:ignore[assignment]
    from setuptools.command import serve as serve2  # type:ignore[assignment]    # noqa: F401 unused import; kept for backward compatibility with Python <36 and to avoid "F401 '...' imported but unused" warnings in some environments (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support forPython <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be removed if/when dropping support for Python <36) (noqa) (PEP8) (see https://www.python.org/dev/peps/pep0396/) (but can be

分享标题:python如何分发依赖包
URL链接:http://www.hantingmc.com/qtweb/news43/1143.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联