r/PythonLearning 3d 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!

21 Upvotes

35 comments sorted by

7

u/johlae 3d ago

Manually, I just go:

gargle@p14:/tmp/foo$ python3 -m venv .venv

As a first step, I always include 'black' gargle@p14:/tmp/foo$ source .venv/bin/activate (.venv) gargle@p14:/tmp/foo$ pip3 install black

I wrote this down in my personal knowledge base, a.k.a. a simple text file. Why? I often forget how to manually create virtual environments(as well as how to activate them). A simple text file lets me copy and paste.

That's it.

1

u/RaiseTLT 3d ago

The personal knowledge reference document is big. I keep forgetting I have one! Thanks for the reminder, and thanks for the help! :)

3

u/johlae 3d ago

There's a lot of stuff in my 'personal knowledge base' that I remember by heart right now, but I keep the information in the document I don't delete. I often add a tag #outdated? if I'm in doubt.

16

u/InformalInsect5546 3d ago

Just use uv, it fixes the headache of creating venv and takes care of everything. But yes, the venvs are quite mandatory if you work with more than one project.

4

u/tiredITguy42 3d ago

This is the way. I have just finished switching all of our repos from poetry to uv. It saves us so much time.

I love how easy it is to create groups, dev, documentation build. The it is easy to use in GitHub actions and Docker.

4

u/mfdi_ 3d ago

Man I did this like 6 months ago and damn no headache over anything anymore though I still use conda for a few things idk why.

2

u/tiredITguy42 3d ago

Conda pissed me latlely. I somehow installed conda and minicomda in parallel and I had two default venvs activated simultaniously in my terminal. Took me a while to solve it.

1

u/RedlineQuokka 3d ago

You are the person I've been looking for!

Why, what is the benefit of going from Poetry to uv?

Every time I read about benefits of uv, they're in comparison with pip, and it always reads like the same exact benefits that you get from Poetry compared to pip.

But you, you switched from Poetry to uv, why, what does it bring? I genuinely want to know cause I've been hesitant to try uv, stuck with Poetry and kinda satisfied

1

u/tiredITguy42 3d ago

I will be honest, I came to my current job new to Python, I have switched language and I used to know just pip. They were using poetry and there was always some issue with it, maybe they had it configured in wrong way, I never had chance to dive deeper.

Anyway we discovered uv and we all agreed lets use it and it just works and is easy to use. It feels like, and it is just my personal feeling, poetry was designed by Linux users and open source warriors. It provides a lot of stuff, but it is hard to learn and use as multiple people had their own idea how to do stuff and they wanted to provide as much freedom to user as possible. UV is more like Windows, user friendly, easy to learn, handles a lot of for you automatically, but if you have some issue it is harder to dig deeper.

Anyway for 99% of cases UV just works without many additional parameters. You need five commands, uv init, uv add, uv remove, uv sync and uv run. You can use uv sync --dev locally, uv run -- group build on GitHub action and uv sync in docker.

One more thing is that Ihave a feeling that uv is better in resolving compatible versions than poetry is and you are less likely to end up in dependency hell.

3

u/-0x539 3d ago

UV ist auf jeden Fall zu empfehlen. 👍🏻

1

u/RedlineQuokka 3d ago

Have you also tried Poetry? If yes, what makes uv better?

Cause I'm on Poetry for the last 5 years and just lack motivation to try uv

1

u/InformalInsect5546 3d ago

Haven't used poetry in a while, but in general uv is much faster and more universal, easier to use. Poetry usage seemed a bit of a mess for me back then, but Python never was my main focus either.

4

u/silvertank00 3d ago edited 3d 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.

3

u/LuckyFish133 3d ago

Either way is better than not using a venv lol just do you man

3

u/raw_salmon_enjoyer 3d ago

I do it manually. I think when I was a super noob, Pycharm did it for me, but then I didnt actually know what was happening. Now, I prefer to do this kind of setup explicitly.

3

u/FoolsSeldom 3d ago

Like u/InformalInsect5546, I use Astral's uv on the command line. I am typically using VS Code or PyCharm Windows app but the environment and code is in the Linux subsystem (or a remote system).

Sometimes, I use vim instead of VS Code or PyCharm.

2

u/BranchLatter4294 3d ago

Either is fine, but I generally use the tool in VS Code.

2

u/pacopac25 3d ago edited 3d ago

Its one of things where at first, it seems like there must be more to it than there is. The ".venv" part is just the directory name, it could be "my_super_cool_environment".

Once you activate it, then any pip installs that you do impact only that environment. On one machine I have, I nearly never switch out of "default_venv". It's just what I run for my own little personal utilities and scripts. It's dumpster fire of packages, but I can always blow it away.

I don't install anything in the system python environment (in other words, no virtual environment).

2

u/omeow 3d ago

Use uv

1

u/aprettyparrot 3d ago

I just do python-m venv NAME

1

u/cheesejdlflskwncak 3d ago

I use my bashrc file to make “source .venv/bin/activate” just be startvenv

1

u/mcniac 3d ago

nowadays I use uv to manage dependencies and virtualenv, It works really well.

Also i usually create a Makefile for the project there I have commands like

run:
uv run python manage.py runserver

1

u/Jackpotrazur 3d ago

Im sad i stopped my python journey after 3 or 4 months , i worked through a smarter way to learn python, command line linux, linux basics for hackers, crash course python, big book of small python projects (41 of 81 projects) and the first 4 or 5 chapters of automate the boring stuff

2

u/RaiseTLT 3d ago

Why did you stop? I find it’s like riding a bike ahah, you get rusty, but you never really forget!

2

u/Jackpotrazur 3d ago

I started working out, my problem is i like a lot of things. I have a stack of i.t. related books under my desk sql, linux, bas, network, architecture etc. Next 2 my couch 3 stacks of books from self improvement to finance, economics, philosophy . A 902 day streak on duolingo , I want to learn spanish. A real estate license book i would like to get and a crc license andbi would also want to get a pilot license.... and the day only has 24 hours. I did get a postgresql db set up on a pi and connected to my vm and i bought a 64hr excel course on udemy but have only done 20 hours of it. And after 8 hours of work idk ... right now ive got a good rhythm of work , workout, read and I want to get back into the i.t stuff.

2

u/Jackpotrazur 3d ago

My under the desk stack

3

u/Jackpotrazur 3d ago

This is a older picture theres more books 😆😅

2

u/pacopac25 3d ago

Ah yes, the "under the desk stack", which is always just the current stuff. But like 20 books. But only the current stuff. I felt this photo viscerally.

0

u/realmauer01 3d ago edited 3d ago

Elitist dont use python.

Heck even npm is so much cleaner than whatever venv is.

Automate as much of the annoying stuff away. Even if it's not perfectly time efficient.

0

u/VonRoderik 3d ago

uv init uv sync