【问题标题】:Python manually install package to virtual environment with setup.pyPython 使用 setup.py 手动将包安装到虚拟环境
【发布时间】:2019-09-23 19:58:39
【问题描述】:

我正在使用 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 作为指南,但我还没有准备好在任何地方上传并想在本地测试。谁能告诉我必须采取哪些步骤才能在虚拟环境中本地测试我的包?

【问题讨论】:

    标签: python package setuptools


    【解决方案1】:
    • 使用以下命令创建项目的源代码分发和构建分发:
      • (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 以在多个虚拟环境中自动测试您的项目。

    【讨论】:

    • 感谢您的回答。我没有使用轮子方法,我可以使用python setup.py sdist 然后(sdist-env) C:\Users\jon\development\my_package_env>pip install dist\my_package-0.0.1.tar.gz 让它工作。
    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多