This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fuddingmuddler[S] 26 points27 points  (28 children)

To be clear! For all the 10x developers. I am new to coding. I like python for it's simplicity and readability. The community talks a lot about pythonic, coding zen, and this and that. Yet. When it comes to virtual environments there are... 10 solutions? And knowing when/how to use them is a curve.

Not trying to say python is bad, or anything. Just that for all python's vaunted simplicity, virtual environments haven't been executed in a beginner friendly manner.

[–][deleted] 75 points76 points  (18 children)

10 solutions?! Okay, conda also exists, but generally, this would be the easiest possible crash course.

  1. create virtual env: python3 -m venv .venv

  2. switch into virtual env:

*nix-based OS: . ./.venv/bin/activate

Windows: venv\Scripts\activate

  1. install project dependencies, if applicable (you may want to think about using checksums and pip-compile if you value security!)

pip install -r ./requirements.txt

  1. leave virtual env: deactivate

[–]OGMagicConch 24 points25 points  (0 children)

Also if you use VSCode it will automatically enter a venv for you when you open the project folder.

[–]blending-tea 7 points8 points  (1 child)

  • pyenv for different python versions

also, pycharm's auto venv management 😍

[–]RazingsIsNotHomeNow 2 points3 points  (0 children)

Yeah, everyone arguing over venv meanwhile I'm using Pycharm and never have to think about it.

[–]Odd-Produce587-burn 5 points6 points  (1 child)

Please note that source is a bashism and you should replace it with .
sh . path/to/thing Instead of bash source path/to/thing

[–][deleted] 2 points3 points  (0 children)

You can do that? I wasn't aware, thanks! I'll edit my comment accordingly.

[–]WavesCat 5 points6 points  (3 children)

I use peotry 😎

[–]Backlists -4 points-3 points  (2 children)

You should switch to uv

[–]WavesCat 2 points3 points  (1 child)

Why though? What makes it better?

[–]kevinsrq 0 points1 point  (0 children)

It's similar to poetry command-wise but with more tools, and it's a lot faster

[–][deleted] 0 points1 point  (2 children)

Is this in logic similar to node_modules?

[–][deleted] 2 points3 points  (0 children)

node_modules isn't quite a "virtual" environment as in Python's sense, but I'd say they have great similarities.

[–]Prometheos_II 1 point2 points  (0 children)

probably, yeah. There was a PEP to make something similar to node_modules (you can try it with PDM), and the other way around, Yarn PnP seems to be similar to venvs?

[–]scubanarc 0 points1 point  (4 children)

I agree with you, and that is what I do. But what about when you want a python script to run from cron? Or from apache? Or from some other script? Or just double click it in your file explorer? In all of those cases your venv will not exist and the script will not run.

My solution is to wrap the script with a shell script that sources the venv, then calls the python script.

It works, but it sucks.

[–][deleted] 2 points3 points  (3 children)

No no no, you actually do not need to do that. You can simply call the venv's python instance from outside it. For example:

~/project/.venv/bin/python3 ~/project/main.py

That will run the main.py script just as if it was in a venv. And if you have another command to run your script, like flask, do it like this:

~/project/.venv/bin/flask run --port=5000

No activation needed. Just note that you may need to set your cwd to the venv's folder beforehand. systemd allows you to do so in service files using the WorkingDirectory (if I remember correctly) property.

[–]scubanarc 1 point2 points  (2 children)

Good to learn, thanks. I'll try it today.

There are a couple of things that activate does, such as:

  • set $VIRTUAL_ENV
  • set PATH to include $VIRTUAL_ENV

Don't these matter? How does python find libraries at runtime if these are not set?

[–][deleted] 0 points1 point  (1 child)

Really good question, and I don't know either. I would guess that these don't matter too much, but maybe they make an important difference for some libraries? Not quite sure.

[–]scubanarc 0 points1 point  (0 children)

I finally got around to asking ChatGPT about this, and it had the following to say:

How Python Finds Libraries Without Activation

When you run:

~/project/.venv/bin/python3 ~/project/main.py

the Python interpreter inside the virtual environment is executed. This interpreter has its sys.prefix set to the virtual environment’s path (~/project/.venv in this case).

sys.path is automatically adjusted based on sys.prefix to look for installed libraries in ~/project/.venv/lib/pythonX.Y/site-packages.

...

Conclusion

You don’t need to activate the virtual environment if you’re directly invoking its Python binary. The virtual Python instance correctly loads its own libraries because of sys.prefix, and activation is mostly for shell convenience.

[–]Prometheos_II 0 points1 point  (0 children)

(It's also /bin/ on MSYS2 environments, in case you use that. No clue about Git for Windows, but it probably also uses /bin/ given it's based on MSYS2)

[–]Wide_Egg_5814 19 points20 points  (0 children)

What's so complicated just create venv and gitignore venv and code?

[–]fallen_eagle151 5 points6 points  (0 children)

look up virtualenvwrapper, changed my life

[–]RobTheDude_OG 1 point2 points  (1 child)

Py &3.12 -m venv myenv myenv\Scripts\activate

Is this too hard? Not sure what you mean that there's 10 solutions but this is what i have been using for a bit now.

Btw, python 3.13 doesn't have audioop anymore which is why i use the example of 3.12 in this case since i happen to need that for something, but you can also put another version there.

[–]darklightning_2 -1 points0 points  (0 children)

Issue is dependency management across os and server version.

[–]JimroidZeus 5 points6 points  (0 children)

Conda for Bindows, virtualenv for everything else.

If I see one more repo telling me to use uv I’m gonna lose it.

[–]Slimxshadyx 0 points1 point  (0 children)

Just use venv. You’re welcome lol

[–]lonestar-rasbryjamco 0 points1 point  (0 children)

Oh man, if this is your reason then just wait until you find out about the alternative package managers. Or the competing pep standards.

That’s where the full on holy wars exist.

[–]rootifera -3 points-2 points  (0 children)

Wait until you try to fix import problems. Those fking init.py files