virtualenv
搭建 venu虚拟环境
python3 -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
python3 -m pip install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple
virtualenv --version
D:\python3.7\Scripts
D:\python27\Scripts\virtualenv.exe
创建虚拟环境
virtualenv project_name
pip是哪个python环境安装的,那虚拟环境默认就是这个python的环境。
激活使用
Scripts目录,里面有个activate.bat文件直接输入activate指令
\Scripts>activate
使用里面的pip安装第三方包
deactivate
.whl
wheel介绍
.whl文件(WHL file)也称为轮子(wheel),
这是用于python分发(distribution)的标准内置包格式(standard built-package format)
它包含安装所需的所有文件和元数据(metadata)。.whl文件使用zip进行压缩
还包含有关此wheel文件支持的Python版本和平台的信息。
即装即用格式(ready-to-install format),
允许在不构建源代码分发(without building the source distribution)的情况下运行安装包。
本质上是zip文件,
这些.whl文件可以使用解压缩选项(unzip option)或标准解压缩软件应用程序(如WinZIP和WinRAR)解压缩。
.whl文件按照以下约定命名:
{dist}-{version}(-{build})?-{python.version}-{abi}-{platform}.whl
之前使用pip安装过Python包,那么我们的系统上很可能已经有轮子(wheel)被安装过。pip是安装wheel的包管理器。
通过pip安装已下载的.whl文件:pip install <filename>.whl ,
安装包后,我们可以执行Python shell并尝试导入包:import whl_dist_name
wheel的直接好处是我们与其他人共享我们的包,他们不必担心构建它。他们只需pip install后即可使用该软件包。它也是一个更小的共享文件(与所有源代码相比),
安装速度更快,因为它不需要运行安装脚本。
wheel的类型:
(1).universal wheel:
包含py2.py3-none-any.whl。
它在任何操作系统和平台上都支持Python 2和Python 3。
(2).pure-python wheel:
包含py3-none-any.whl或py2-none-any.whl。
它支持Python 3或Python 2,但不能同时支持两者。
它在其它方面与universal wheel相同,但它会被标记为py2或py3而不是py2.py3标签。
(3).platform wheel:
支持特定的Python版本和平台。
3. 创建wheel:
(1).将所有模块(python脚本)、包(包含模块的文件夹/目录)保存在父目录中。
随意命名根目录,通常与项目相关。
(2).最好创建一个空的名为__init__.py文件,
并将此__init__.py文件放在所有包目录和子包目录下。无需将其保存在根目录中。
(3).创建一个名为setup.py的文件并将其放在根目录中。
此脚本的内容至少应包括:distribution name, version number, and list of package names
(4).转到你可以运行python和pip命令的命令提示符。
在命令提示符下更改目录并导航到放置setup.py的项目的根目录,
执行以下命令:扩展名为.whl的文件将在根目录下自动创建的子目录中创建,
名为dist。
wheel中并不包含setup.py,wheel不必运行setup.py脚本。
注:需提取安装wheel setuptools:
pip install wheel setuptools,在conda中默认是安装的
(yujing) PS C:\Users\amingMM\Desktop\yujing\Scripts> pip list
Package Version
---------- -------
pip 22.3.1
setuptools 65.6.3
wheel 0.38.4
python setup.py bdist_wheel --universal # universal wheel
python setup.py bdist_wheel # pure-Python wheel
通过conda在虚拟环境base下创建一个wheel,取名为testwheel目录组织结构
setup.py内容
import setuptools
setuptools.setup(
name="testwheel",
version="1.0.0",
author="fengbingchun",
author_email="fengbingchun@163.com",
description="test wheel",
packages=setuptools.find_packages(),
url="https://github.com/fengbingchun",
license="MIT",
python_requires=">=3.8"
)
testwheel目录下的__init__.py是个空文件,math目录下的__init__.py内容如下:
from .math_add import *
from .math_sub import *
math_add.py内容如下:
def add3(a, b, c):
print("call add operation: three parameters ...")
return (a+b+c)
def add2(a, b):
print("call add operation: two parameters ...")
return (a+b)
行如下命令生成wheel,此wheel仅限于在Python3上执行,将终端定位到setup.py目录下
执行完上述命令后会产生3个新的目录,
build, dist, testwhell.egg-info,
各个目录的内容如下所示,
生成的whell在dist目录下,全名为testwheel-1.0.0-py3-none-any.whl,
只需将此文件分发出去,其他人安装后即能使用。
导入使用wheel:
如果你想在项目中安装已经安装过的wheel文件,需要更新此wheel的版本号。
如果版本号保持不变,pip将不会安装它。或者先卸载已安装的whell: pip uninstall testwheel
from testwheel.math import math_add, math_sub
也可使用--force-reinstall 反复的调整wheel的内容
pyvenv.cfg
home = d:\python27
implementation = CPython
version_info = 2.7.13.final.0
virtualenv = 20.15.1
include-system-site-packages = false
base-prefix = d:\python27
base-exec-prefix = d:\python27
base-executable = d:\python27\python2.exe
home, 指明虚拟环境所复制的系统python executable的路径
include-system-site-packages
include-system-site-packages为true表明会使用系统中的python的site-packags。
此外,重要的一点是搜索路径的先后顺序,当该选项为true时,
会先搜索虚拟环境中安装的包,其次才会去系统site-packages中寻找。