News for hackers. Stuff that matters.
Re:Harlequin: SQL IDE for Your Terminal by whalesalad on Friday January 05, 2024 @04:30PM (#38885233)
requirements.txt is just a list of packages for pip. It doesn't even need to be called requirements.txt, it could be called poopoopeepee.txt. That name is just a convention.

You could do `for line in requirements.txt, pip install <line>`. They are ostensibly the same. It is not a magical lockfile. It is unix. It is just a list if packages. If you are in a virtualenv, you will be fine.

either activate the venv,

    source venv_directory/bin/activate
    pip install -r requirements.txt
or, skip the convenience and use the binary directly:

    venv_directory/bin/pip install -r requirements.txt
virtualenv activation just sets the $PATH to refer to those binaries. you can use them directly.

the production deployment for my core python app lives in /srv/app, the venv lives in /srv/venv

To update packages on the system, it is as simple as

   cd /srv/app
   /srv/venv/bin/pip install -r requirements.txt
Then to invoke this with the correct runtime, it is as easy as

   /srv/venv/bin/gunicorn ...
In this example I am running the gunicorn application server. This is running the specific gunicorn version installed into my virtualenv.

The name `/srv/venv` is my decision. You can call that whatever you want and put it wherever you want. For instance, if you have two projects called application-foo and application-bar, you can have the following:

    /srv/application-foo/app - the codebase (ie, the github repo)
    /srv/application-foo/venv - the corresponding virtualenv
    /srv/application-bar/app
    /srv/application-bar/venv
Some people will even put their venv dir inside of their source tree and exclude it from git (add to .gitignore), but I do not like this approach because my deployments destroy the app dir and unzip the build into that location on each deploy.

I cannot speak for every python-based project (distinction from pip package) out there. A lot of people do not know what they are doing, open source is literally all the rope to hang yourself with. Anything is possible, and people without engineering experience will glue things together without understanding how they work.

If you are installing something via pip, then yes, you can create N virtualenvs and use them however you want. They are 100% isolated environments.

If you are using homebrew, apt-get, dnf/yum, arch etc... then those are going to obviously vary from distro to distro and that is outside the scope of this discussion.

I try to stick to Python's native tools as much as possible. Using a distribution package is going to cause issues, for sure. IE, don't install `apt-get install python-pil`, use a virtual environment and `pip install PIL`

Re:Harlequin: SQL IDE for Your Terminal by pmarreck on Friday January 05, 2024 @04:40PM (#38885347)
alright, I will bookmark this and try this next time I want to play with a python project.

OK, how would I include all of these under the same PATH regime?

So for example say I want to run this project from a commandline location elsewhere... I'd only be able to have one venv activated at the same time in the same session, right?

I guess that's part of my issue with this. I want to be able to access 10 different Python projects' commands from the same command line at any time.

Re:Harlequin: SQL IDE for Your Terminal by whalesalad on Friday January 05, 2024 @04:50PM (#38885440)
To access 10 different commands at the same time, that is tricky but definitely doable.

First thing that comes to mind, you can use aliases.

To keep it simple, lets use 3 examples instead of 10: harlequin (this project), pgcli (https://www.pgcli.com/) and httpx (https://www.python-httpx.org/)

Setup a main home for all your venvs:

    cd ~
    mkdir venvs
Go into this dir, and create your venvs and install the packages

    cd venvs
    python -m venv harlequin
    ~/venvs/harlequin/bin/pip install harlequin
Now this binary is available at

    $ ~/venvs/harlequin/bin/harlequin
Repeat for the rest

    cd ~/venvs
    python -m venv pgcli
    ~/venvs/pgcli/bin/pip install pgcli

    cd ~/venvs
    python -m venv httpx
    ~/venvs/pgcli/bin/pip install httpx
Wash, rinse, repeat. Now you have all these binaries available and can alias them

    alias harlequin="~/venvs/harlequin/bin/harlequin"
    alias pgcli="~/venvs/pgcli/bin/pgcli"
    alias httpx="~/venvs/httpx/bin/httpx"
This is a pain in the ass though and usually simple CLI tools like this do not collide with each other. So that is why I say install globally, or install into your "global junk drawer" virtualenv.

Meanwhile, for actual projects that you are developing on, those would have their own isolated venv.

I have a junk drawer venv where I install tools like this. If something goes wrong, it is as simple as rm -rf the venv and make a new one. And then I have isolated ones for each of the actual systems I maintain. Again, I use pyenv for this to make it a little easier to manage in conjunction with their specific python versions such that I do not ever interact with my distribution's Python. This is cross platform so it works across mac, linux etc. Very easy workflow, isolated, safe, can get blown away and recreated in a heartbeat.

Re:Harlequin: SQL IDE for Your Terminal by gottoupvote on Saturday January 06, 2024 @01:38AM (#38889050)
Sounds like something https://www.gnu.org/software/stow/ could help with.
Re:Harlequin: SQL IDE for Your Terminal by pmarreck on Sunday January 07, 2024 @02:16PM (#38904221)
You know, at this point in the complexity story, you're literally at (or beyond) the level of Nix complexity which is the very solution that everyone who engages with this level of tooling seems to be trying to avoid, and since Nix solves this problem already definitively without having to jump through all these hoops, why don't Python projects just use Nix? Then they could all be colocated on the same machine and all be accessible from the same PATH and all have their specific dependencies none of which would ever collide with each other!

Like, you're LITERALLY making a FANTASTIC argument for Nix usage in the Python ecosystem, here. In fact I'm going to bookmark this conversation now because of how ridiculously complicated your answer is compared to just using Nix.

Here's the Nix whitepaper. It's 14 pages or so. Read it on your next lunch break.

https://edolstra.github.io/pubs/nspfssd-lisa2004-final.pdf