简体   繁体   中英

Distribute Python applications “with” a venv

Say I have a Python app that will be used as a command line tool. This app has some external dependencies. How would I go about distributing this?

I know it's common to install Python stuff in a virtual environment (virtualenv or pyvenv), but if the app is to be used from the command line I don't want to have me or my users to activate the correct virtual environment every time they want to use my app.

Is there a solution to this? Or should I just put all dependencies in setup.py and leave it up to the user whether they create a virtual environment or not?

Use a setup.py and list the dependencies in install_requires .

Now to the part "How to distribute this?"

In our company we run our own pypi server. Every package that gets installed on our servers must be from our pypi server. No software gets downloaded directly from the internet to the server.

If you want to build an open source tool, you should upload it to the official pypi server.

The tool should not care about its environment. It should work in a virtualenv and outside.

Maybe the sampleproject helps you: https://github.com/pypa/sampleproject

I will mention an alternative solution with a requirements.txt file for pip . See the documentation:

https://pip.pypa.io/en/latest/user_guide.html#requirements-files

The user then knows what dependencies your app has and can easily install them into his/her virtual environment with pip install -r requirements.txt .

The file can easily be created with pip freeze > requirements.txt .

This kinda depends on who you think will be using your application. If you distribute it via pip your user should be capable to decide wether to use a virtualenv or not. If you want your users to simply download your script from a website (maybe even for windows) and it "simply works" you can bundle your dependencies (and maybe even a python interpreter) and then modify PYTHONPATH accordingly. Of course you could also use different ways to distribute eg a docker image .

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