频道
bg

Python 构建系统

coding四月 15, 20211mins
python

包管理H1

  • Conda是个跨语言的二进制环境、包管理器。
  • MiniConda、Anaconda是Conda的发行版本(安装器),MiniConda是Anaconda是只包含conda, Python以及它们的依赖的简化版本。
  • MiniForge是社区(conda-forge)驱动的Conda的发行版本(安装器),它使用conda-forge作为默认的channel

指定ArchH2

CONDA_SUBDIR

jsx

CONDA_SUBDIR=osx-arm64 conda create -n native

Conda ForgeH2

feedstock - the conda recipe (raw material), supporting scripts and CI configuration.

conda-smithy - the tool which helps orchestrate the feedstock. Its primary use is in the construction of the CI .yml files and simplify the management of many feedstocks.

conda-forge - the place where the feedstock and smithy live and work to produce the finished article (built conda distributions)

PIPH1

PIP是Python的包管理器,它从Python Package Index下载包。

指定仓库H2

可以用下面的方式从指定仓库安装

bash

pip install -i/--index-url <url> <package>

bash

export PIP_INDEX_URL=<url>
pip install <package>

或者通过直接配置

bash

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

它会把配置保存到用户配置下

bash

$HOME/.config/pip/pip.conf

因此也可以直接修改上面的配置文件,更多配置文件参考

上面的配置命令默认是—-user 参数,所以保存到用户配置下,还有其他的不同参数对应不同位置的配置文件

  • -g-lobal
  • --site

包安装位置H2

pip默认把包安装的site-packages 下,一般位置为prefix/lib/pythonX.Y/site-packages ,可以执行python -m site 来查看。

上面说的方式,一般可以理解成全局安装(如果Python本身是系统级别)。可以指定—-user 来参数来为当前用户安装

bash

pip install xxx --user

使用python -m site --user-site 来查看用户安装目录。

本地安装H3

如果全局安装包,不同项目使用不同的版本肯定会产生冲突。在Node中,我们可以选择安装到全局或者本地目录,类似的在Python中我们使用venv来实现本地安装。

venv可以成是一个隔离的site-packages 环境,通过下面的命令创建

bash

python3 -m venv --system-site-packages ./venv

指定--system-site-packages 后, venv的site-packages 同时会导入全局的site-packages

在venv环境下,pip会把包安装到<virtualenv_name>/lib/<python_ver>/site-packages 目录

激活本地环境

bash

source venv/bin/activate

添加二进制文件到PATH路径H3

jsx

PATH="$(python3 -m site --user-base)/bin:${PATH}"

独立包空间管理器的区别H2

  • venv Python自带的虚拟环境管理器,是Virtualenv的子集
  • Virtualenv 完整的虚拟环境管理器
  • pyenv Python的版本管理器
  • pyenv-virtualenv pyenv的包虚拟环境管理插件

pyenvH3

Github Checkout的配置

jsx

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
fi

依赖管理H1

项目一般需要声明文件来管理第三方依赖。在Python中,可以使用setup.py 或者requirements.txt 来管理第三方依赖。

requirements.txtH2

requirements.txt文件个格式如下

bash

#
####### example-requirements.txt #######
#
###### Requirements without Version Specifiers ######
nose
nose-cov
beautifulsoup4
#
###### Requirements with Version Specifiers ######
# See https://www.python.org/dev/peps/pep-0440/#version-specifiers
docopt == 0.6.1 # Version Matching. Must be version 0.6.1
keyring >= 4.1.1 # Minimum version 4.1.1
coverage != 3.5 # Version Exclusion. Anything except version 3.5
Mopidy-Dirble ~= 1.1 # Compatible release. Same as >= 1.1, == 1.*
#
###### Refer to other requirements files ######
-r other-requirements.txt
#
#
###### A particular file ######
./downloads/numpy-1.9.2-cp34-none-win32.whl
http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl
#
###### Additional Requirements without Version Specifiers ######
# Same as 1st section, just here to show that you can put things in any order.
rejected
green
#

关于依赖的版本声明

bash

SomeProject
SomeProject == 1.3
SomeProject >=1.2,<2.0
SomeProject[foo, bar]
SomeProject~=1.4.2

如果手动声明外,还可以通过执行freeze根据当前环境生成

bash

python -m pip freeze > requirements.txt

安装依赖H3

执行下面的命令安装requirements.txt 中声明的依赖

bash

pip install -r requirements.txt

setup.pyH2

setuptools 是现在用来管理项目的打包于分发的苦,它是distutils 的替代者。使用setuptools

时,使用下面的文件来进行配置管理。

setup.py

setup.py有两个主要作用

  1. 项目的配置文件,主要通过setup() 函数来配置
  2. 它是执行不同命令行任务的接口,python [setup.py](http://setup.py/) --help-commands

setup.cfg

setup.cfg包含setup.py中命令的默认选项

setup配置H3

依赖声明

添加install_requires

bash

setup(name='funniest',
version='0.1',
description='The funniest joke in the world',
url='http://github.com/storborg/funniest',
author='Flying Circus',
author_email='flyingcircus@example.com',
license='MIT',
packages=['funniest'],
install_requires=[
'markdown',
],
zip_safe=False)

Specifying Dependencies - Python Packaging Tutorial

setup.py的更多配置参考

setup命令H3

安装

bash

python setup.py install

开发

bash

python setup.py develop

打源码包

bash

python setup.py sdist

打wheel包

bash

python3 setup.py bdist_wheel --universal

更多关于installH3

执行过install后,模块就可以被其他程序引用了,因为setuptools将其添加到sys.path 中了,具体来讲,python会根据site-packages下的.pth 文件来设置sys.path

setuptools会使用easy_install 将模块添加到site-packages/easy-install.pth 中。

编写 python package 中的 setup.py 文件

依赖声明的对比H2

requirements.txt 只是一个手动的声明文件,而install_requires的metadata信息并pip用来分析并安装依赖。

因此requirements.txt 可以声明-index-url https://pypi.python.org/simple/ ,因此他们是Concrete依赖。而install_requires 必须是Abstract依赖,因为pip解析他们,它pip需要配合PyPI镜像。

参考:

requirement.txt和setup.py共存H3

依赖在两者中都声明有点重复,下面两种方式来解决

  1. requirements.txt 中声明. 来反转声明

    bash

    --index-url https://pypi.python.org/simple/
    [-e] .
  2. setup.py中使用parse_requirements

参考:https://stackoverflow.com/questions/14399534/reference-requirements-txt-for-the-install-requires-kwarg-in-setuptools-setup-py

分发包中的依赖H3

Conda

bash

blaze-core-0.8.0-np19py34_0.tar.bz2": {
"build": "np19py34_0",
"build_number": 0,
"date": "2015-04-27",
"depends": [
"cytoolz",
"datashape",
"flask 0.10.1",
"multipledispatch 0.4*",
"numpy 1.9*",
"odo 0.3.2",
"pandas",
"psutil",
"python 3.4*",
"requests",
"sqlalchemy",
"toolz"
],
"license": "BSD",
"md5": "b6560e12593bdd48c52b984ae4eb6776",
"name": "blaze-core",
"size": 327144,
"version": "0.8.0"
},

Wheel

METADATA

bash

Requires-Dist: tornado (>=6.1.0)
Requires-Dist: jupyter-core
Requires-Dist: jupyter-packaging (~=0.7)
Requires-Dist: jupyterlab-server (~=2.3)
Requires-Dist: jupyter-server (~=1.4)
Requires-Dist: nbclassic (~=0.2)
Requires-Dist: jinja2 (>=2.10)

egg

requires.txt

bash

python setup.py develop
running develop
running egg_info
writing requirements to funniest.egg-info/requires.txt
writing funniest.egg-info/PKG-INFO
writing top-level names to funniest.egg-info/top_level.txt
writing dependency_links to funniest.egg-info/dependency_links.txt
reading manifest file 'funniest.egg-info/SOURCES.txt'
writing manifest file 'funniest.egg-info/SOURCES.txt'
running build_ext

评论


新的评论

匹配您的Gravatar头像

Joen Yu

@2022 JoenYu, all rights reserved. Made with love.