python如何分发依赖包

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

python如何分发依赖包
(图片来源网络,侵删)

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

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/467542.html

(0)
未希新媒体运营
上一篇 2024-04-12 18:45
下一篇 2024-04-12 18:46

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入