Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4151

Python • Re: Virtual environment insanity

$
0
0

1. install the venv without pip in it.
I'm not sure why in the context of the OP's needs you would want to do this but am willing to learn.
2. why backup the venv at all? All you need is a test file with the modules needed in the venv, which you can create with pip at any time you add/update/remove something from your venv with pip freeze.

pip freeze > requirements.txt

To install your dependencies:

pip install -r requirements.txt

With this you requirements.txt file becomes part of your source, you check that in and if you ever need to install your project again you get it from source and run pip install -r and you're done.
I don't have, and I don't think the OP, has a library management system. I'm at least just dealing with discrete files.

So my practice now is to create a directory vProj which holds my source files. I then create a venv1 sub-directory for the Virtual Environment. This puts the VENV environment and source files in the same parent directory.

My point is that I do a daily backup of my account to the cloud. I exclude things such as any directories starting with a period '.', log files etc. I now need to explicitly exclude the VENV directories.
The problem with all of this is not that it's complicated or black magic, or whatever else. It is that the user is usually not interested in finding out how to use the way they need to be used first, but just want to copy and paste something from the internet and get it to work. That only works until something changes (like the OS, python version, whatever else) and then all hell breaks loose.
If defense of the OP, the confusing part comes in when they are following directions for install modules/libraries that were not written for working in a VENV, especially when you are starting out.
The sudo part has nothing to do with pip or python or venv works at all. It is 100% on how sudo works. And here again, people just copy and paste from the web without understanding what it really does and why, so when it comes time to use it somewhere else it becomes "virtual environment insanity" instead of "I don't know how this works".
I ran into problems because of one project where the library had to installed by SUDO; it wasn't just a module and the Python script had to be run from SUDO. So there are devices out there that don't play nicely with VENV. Again, part of the learning curve.
I understand people just want to get things done, but as with anything else, a bare minimum of understanding is required if an exact set of instructions is provided that works at this point in time with the current system's configuration.

It's the way everything works: driving a car, using a spreadsheet, bee keeping, etc. You can certainly do all of those things without any prior knowledge or training too, but they also come with a lot of pain and insanity too.

Take the time to go through the link I sent. It right away shows how to create, activate and deactivate a venv. Which sounds like more magic than it is. The rest of the entire write up explains exactly what it really is, with code for the user to follow along and experiment with to actually understand what's really going on. It's just a set of folders, and a change of paths where python looks for things. That is all there is to it.
Yes, it's a good article but didn't cover my issues of backups and organizing files with the VENV.

Statistics: Posted by DS256 — Thu Jan 02, 2025 10:08 pm



Viewing all articles
Browse latest Browse all 4151

Trending Articles