Python 使用 setup.py 手动安装包到虚拟环境

Python manually install package to virtual environment with setup.py

我正在使用 Python 3.7.

进行开发

我正在尝试编写一个小 Python 包并将其安装到虚拟环境中进行测试。该库的结构如下:

my_package/
    src/
        my_package/
            __init__.py
            utilities.py
            some_data_files/
    setup.py
    README.md
    ...

当我创建虚拟环境并尝试安装时:

(env) C:\Users\path\to\my_package python setup.py install

输出为:

running install
running bdist_egg
running egg_info
writing my_package.egg-info\PKG-INFO
writing dependency_links to my_package.egg-info\dependency_links.txt
writing top-level names to my_package.egg-info\top_level.txt
reading manifest file 'my_package.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'my_package.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
warning: install_lib: 'build\lib' does not exist -- no Python modules to install

creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\EGG-INFO
copying my_package.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying my_package.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying my_package.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying my_package.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist\my_package-0.0.1-py3.7.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing my_package-0.0.1-py3.7.egg
Copying my_package-0.0.1-py3.7.egg to c:\users\jon\development\my_package_env\env\lib\site-p
ackages
Adding my_package 0.0.1 to easy-install.pth file

Installed c:\users\jon\development\my_package_env\env\lib\site-packages\my_package-0.0.1-py3
.7.egg
Processing dependencies for my_package==0.0.1
Finished processing dependencies for my_package==0.0.1

site-packages 中有一个 egg 文件,但没有目录包含 my_package 目录中的任何文件。这是我第一次编写用于分发的 Python 包,我使用 Python documentation 作为指南,但我还没有准备好上传到任何地方并想在本地进行测试。有人可以告诉我必须采取哪些步骤才能在虚拟环境中本地测试我的包吗?

  • 使用以下命令创建项目的源代码分发和构建分发:
    • (env) C:\Users\jon\development\my_package_env>python3 setup.py sdist
    • (env) C:\Users\jon\development\my_package_env>python3 setup.py bdist_wheel
  • 注意 dist/ 目录中的两个新创建的档案
  • 尝试在新的虚拟环境中安装这些发行版:
    • C:\Users\jon\development\my_package_env>python3 -m venv sdist-env
    • C:\Users\jon\development\my_package_env>sdist-env\Scripts\activate.bat
    • (sdist-env) C:\Users\jon\development\my_package_env>pip install dist\my_package-0.0.1.tar.gz
    • (sdist-env) C:\Users\jon\development\my_package_env>deactivate
    • C:\Users\jon\development\my_package_env>python3 -m venv wheel-env
    • C:\Users\jon\development\my_package_env>wheel-env\Scripts\activate.bat
    • (wheel-env) C:\Users\jon\development\my_package_env>pip install dist\my_package-0.0.1-py3-none-any.whl

一旦您对此有信心,您可能希望查看 tox 以在多个虚拟环境中自动测试您的项目。