简体   繁体   中英

Python manually install package to virtual environment with setup.py

I am developing using Python 3.7.

I'm trying to write a small Python package and install it into a virtual environment for testing. The library is structured like this:

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

When I create virtual environment and try to install:

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

The output is:

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

There is an egg file in site-packages but no directory containing any of the files from the my_package directory. This is my first time writing a Python package for distribution, and I'm using the Python documentation as a guide, but I'm not ready to upload anywhere and would like to test locally. Can someone let me know what steps I must take to be able to test my package locally in a virtual environment?

  • Create a source distribution and a built distribution of your project, with these commands:
    • (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
  • Note the two newly created archives in the dist/ directory
  • Try to install these distributions in fresh virtual environments:
    • 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

Once you're confident with this, you might want to look at tox to automate the testing of your project in multiple virtual environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM