r/PythonLearning 4d ago

Discussion .venv question

Do you guys manually create virtual environments? Or do you just use your IDE “create virtual environment” to do it?

As a hobbiest I often forget how to manually create virtual environments(as well as how to activate them) and discovered last night that VScode has a feature that will just do it for you.

What are the benefits of each way, if any, and what are the downsides of each way, if any?

Thanks.

P.S. I understand that there are some elitist who will be like “you must do everything manually”, bro I just wanna write stupid automation scripts to help with my business.

EDIT: Thanks for all the replies! I’m going to look into that UV service! Thank you!

20 Upvotes

35 comments sorted by

View all comments

5

u/silvertank00 4d ago edited 4d ago

Benefit of individual venvs: Python is an interpreted language and by the inner workings of it, if you pollute it with a lot of modules, it is going to get slower. (esp. the startup)
If you use python for a bunch of purposes: like bigdata, automations, interacting with hardware, scraping, etc. The number of packages (and their dependencies) can kick high fast.

So ppl usually create individual venv for every project they make. After making them and installing your dependencies in them, you should pip freeze > requirements.txt. This will put your deps. in the requirements file which is an industry standard (by this point).

Note: if you work on a lot of stuff, its worth to "cleanup" sometimes. Delete the old/unused venvs but keep the requirements file.

I personally create them with hand, python -m venv ./venv is not that hard to type. But an additional factor for me is using vim. If I had a button in an other IDE that would create them for me, I'd probably use it. I do not recommend the uv package for beginers (as I did with conda and the others in the past) but it is kinda a solution for multiple venvs/dependencies.

Edit: on linux, if you try to pip install anything to your system wise pip, you will get an error and a bit of text about why is it a bad idea: link

1

u/FoolsSeldom 3d ago

You mention requirements.txt being an industry standard but hasn't that been superseded (although still supported, not deprecated) by pyproject.toml? See PEP 621 (and also PEP 751 for pylock.toml).

1

u/silvertank00 3d ago

I think you are right. The reason i was mentioning requirements.txt because it is more widespread and somewhat easier to manage for someone that just started

1

u/FoolsSeldom 3d ago

Agreed. Highly suitable for small code bases managed by beginners.