Python模块加载的时候,有三个库需要区分一下。
内置库
系统库,可以通过如下方式查看其位置。
| 12
 3
 
 | import osos.__file__
 /opt/anaconda3/envs/py310/lib/python3.10/os.py
 
 | 
说明内置库在/opt/anaconda3/envs/py310/lib/python3.10/
三方库
第三方开发公司开发的库,有些也是官方提供的。
可以通过如下方法查看库的位置。
| 12
 3
 
 | import pippip.__file__
 /opt/anaconda3/envs/py310/lib/python3.10/site-packages/pip/__init__.py
 
 | 
说明三方库的位置在/opt/anaconda3/envs/py310/lib/python3.10/site-packages/
本地库
有些库是自己开发的,通过设置PYTHONPATH来指定模块的位置。
环境变量可以通过os.environ来查看。
可以通过sys.path来动态添加地址。
模块demo
比如写个demo模块,首先创建一个demo文件夹。
模块文件夹中,需要添加__init__.py,系统即可认定为模块。
打包的时候,需要两个配置文件。这两个文件和demo文件夹在同一个目录下。
setup.py
| 12
 3
 
 | from setuptools import setup
 setup()
 
 | 
setup.cfg
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 
 | [metadata]name = demo
 version = 0.0.1
 author = Simon
 author_email = simon@email.com
 description = A demo module
 long_description = file: README.md
 long_description_content_type = text/markdown
 url = https://finolo.gy
 license = MIT
 license_files = LICENSE
 keywords =
 quant
 trading
 classifiers =
 Development Status :: 5 - Production/Stable
 Operating System :: Microsoft :: Windows
 Operating System :: POSIX :: Linux
 Operating System :: Darwin :: Macos
 Programming Language :: Python :: 3
 Programming Language :: Python :: 3.7
 Programming Language :: Python :: 3.8
 Programming Language :: Python :: 3.9
 Programming Language :: Python :: 3.10
 Topic :: Office/Business :: Financial :: Investment
 Programming Language :: Python :: Implementation :: CPython
 License :: OSI Approved :: MIT License
 Natural Language :: Chinese (Simplified)
 project_urls =
 Documentation = https://finolo.gy/
 
 [options]
 packages = find:
 include_package_data = True
 zip_safe = False
 install_requires =
 vnpy
 
 | 
执行安装命令
安装以后,demo模块就进入到site-packages里面去了。模块的名称可能是demo_module-0.0.1-py3.10.egg,以egg结尾,进入此目录后,会看到demo_module目录。这种方式即是我们所谓的源码安装。
也可以通过wheel方式打包。
首先需要安装模块,wheel
| 1
 | python setup.py bdist_wheel
 | 
会生成一个文件:dist/demo_module-0.0.1-py3-none-any.whl
这个时候发给别人,别人通过pip install就可以安装了。
安装成功后,site-packages目录下面直接就是demo_module。