Installing gunicorn once on the OS and reuse it between venv ?
from SpongeB0B@programming.dev to python@programming.dev on 08 Aug 2024 09:21
https://programming.dev/post/17866168

Hi,

I use gunicorn in my venv

I have quite few venv that run gunicorn.

I would like to reuse gunicorn for other venv

I launch my web application like this

#PWD = venv dir
source ./bin/activate
gunicorn A_WebApp:app
#A_WebApp is my python file A_WebApp.py

I supposes that gunicorn is a shell program ? if yes I should use $PATH ?
or gunicorn is a Python program only ? and then what I should do to use gunicorn in another venv ?

Thanks.

#python

threaded - newest

fraksken@infosec.pub on 08 Aug 2024 09:45 next collapse

It’s possible to make the venv portable, versionize it, archive it and deploy at boot the latest version. It’s architecture dependent though, so if you deploy on multiple archs, you will need to build for each.

Edit: gunicorn is part of the venv. All you need to do after deploying gunicorn is activating the venv and running your server.

You can also have the archive tun through several vulnerability checkers.

SpongeB0B@programming.dev on 08 Aug 2024 10:52 collapse

I don’t want to make the venv portable…
I want to use the gunicorn that is installed in one venv accessible to other venv

onlinepersona@programming.dev on 08 Aug 2024 12:25 next collapse

I don’t think that’s possible without some dirty, dirty hacks i.e adding the right paths from the other venv to your running process. Do you want dirty hacks? Because that’s just asking for trouble. If you have an application that requires libA-v1 and the gunicorn venv uses libA-v2, you’re going to have a conflict at runtime.

I supposes that gunicorn is a shell program ?

source ./bin/activate
which gunicorn # outputs the path to gunicorn
less `which gunicorn` # reads gunicorn

gunicorn takes a module and a module name with a variable name. Modules are found by searching in specific paths. You can add to that search path by modifying PYTHONPATH. How it works is explained here (quite wordy).

To know which path to add to PYTHONPATH, you can either read .bin/activate and figure it out, or run something like bash -c “source ./bin/activate ; env” and it’ll list all the environment variables. You can then expand (not replace) the environment variables of the current environment with those of the other environment - either in bash or in python - up to you.


As I said, dirty dirty and honestly I’d just install gunicorn in every venv then you’re done with it. But if you really want to, try what I explained and see how it works for you. It’s good to experiment and find out first hand.

Anti Commercial-AI license

walthervonstolzing@lemmy.ml on 08 Aug 2024 14:39 collapse

Wouldn’t enabling the –system-site-packages flag during venv creation do exactly what the OP wants, provided that gunicorn is installed as a system package (e.g. with the distro’s package manager)? docs.python.org/3/library/venv.html

Sharing packages between venvs would be a dirty trick indeed; though sharing with system-site-packages should be fine, AFAIK.

onlinepersona@programming.dev on 08 Aug 2024 21:27 collapse

Hadn’t considered that. It might be the solution @SpongeB0B@programming.dev is looking for. Good shout.

Anti Commercial-AI license

rglullis@communick.news on 08 Aug 2024 16:00 collapse

Why? How many kilobytes of disk space are you going to save from that?

_____@lemm.ee on 08 Aug 2024 16:53 next collapse

If A_WebApp is truly the python file and app is some python symbol then you should be able to make gunicorn in another venv and call your script as such: path/to/venv/bin/python gunicorn A_Web_App:app

SpongeB0B@programming.dev on 09 Aug 2024 07:25 collapse

Thank you ! it works !

Actually this is working :

path/to/venv/bin/gunicorn A_Web_App:app

Some other poster, claim it’s dirty… but which problems could it generate ? (if any)

Thanks all !!!

_____@lemm.ee on 09 Aug 2024 12:37 collapse

I don’t think it’s dirty if you manage your requirements across these environments with care, but I’d ask for what they mean

FizzyOrange@programming.dev on 08 Aug 2024 21:49 next collapse

I wouldn’t recommend it. Installing Python packages not in a venv is asking for trouble. Why do you care anyway?

NostraDavid@programming.dev on 10 Aug 2024 21:36 collapse

Use pip to install pipx, use pipx to install gunicorn to make it available globally. Pipx is meant to install applications as it will install each in their own venv, whereas pip will install them in a single global env.

Makes sure gunicorn isn’t installed in your venvs, so when you run them, they’ll use the pipx installed one.