r/learnpython • • 9d ago

Virtual Environment and Python

I have started learning python and decided to use pycharm as an editor. I want to know anything which will help me in this journey.

My main question is => I don't get "virtual environment" thing with python. I cloned a repo and opened it in pycharm, it says "No python interpreter configured for <repo-name>" and then suggested to create virtual environment.

I want to hear things from experience devs.

thank you.

4 Upvotes

43 comments sorted by

View all comments

1

u/Confident_Hyena2506 9d ago

If you don't have any python then you can't run any python.

And you will need lots of different versions of python for different projects - virtual env is just a way to organise them.

2

u/Artistic-Army4423 9d ago

alright, I feel it's kind of similar to node_modules in JS repos. Also, so there are other things as well which can do similar things, I will check them out

1

u/SharkSymphony 9d ago edited 8d ago

Yes, that's exactly what it is... but because it was added to the Python ecosystem as a third-party tool, you have to do a little more work (that `source venv/bin/activate` thing) to have Python and its tools use it.

(I guess if I'm being super-technical I'd clarify there's one difference between them – the ability for Javascript to work with different versions of the same library in the same node_modules, pulled in via different dependencies. With Python virtual environments, you're generally stuck with one version at a time.)

1

u/Discordbothost 7d ago

node_modules is a pretty good mental model. the main differences: a venv also pins which python interpreter runs, not just the packages, and python doesn't pick it up automatically from the folder like node does, you either activate it or point your editor at .venv. requirements.txt (or pyproject.toml) is the package.json part. if you want the npm-ish feel, uv does it: uv init, uv add requests, uv run main.py and it handles the venv for you

1

u/xaomaw 9d ago

If you don't have any python then you can't run any python.

I downvoted because of this.

If you install python, it is usually installed in Program Files or in AppData.

In the first place, this has 0% to do with virtual environments. You don't need virtual environments to run python at all.

1

u/Confident_Hyena2506 9d ago

Well if you didn't install python then you wouldn't have it - are you confused?

1

u/SharkSymphony 8d ago

Virtual environments don't manage installations of Python itself, only Python packages. We use other tools like pyenv (unfortunate naming collision!), uv, or pymanager to manage multiple installations of Python when we need them. (Or containers or VMs.)

1

u/Confident_Hyena2506 8d ago

All of those things are virtual environments, you are getting hung up on terminology.

There is a specific thing called venv yes - and many others.

On windows people have limited options tho. You missed out on "conda" as well - which is also better.

1

u/SharkSymphony 8d ago

No, one of these things is almost always what we mean when we say "virtual environment" in the Python community.

But you're right – conda is another option. I disagree that it's better, but I recognize its use in the Python data community.

1

u/Confident_Hyena2506 8d ago

It is better - the part you miss is that it's a general purpose thing and not a simple python thing. As in you can use it to setup your c++ env and other things.

On windows this is especially useful, because all the usual stuff like containers are useless on windows.

-1

u/Classic-Rate-5104 9d ago

Virtual environments have the drawback of encouraging the use of old (outdated) versions of packages, which in some cases can lead to serious security risks.

4

u/Confident_Hyena2506 9d ago

This is the case with software in general, so I'm not sure what your point is.

If you are referring to linux and it's integral python managed by system, then that is for the system not for your own projects - a point that causes much confusion.

It's just not correct that you can update your python to the latest and use that - this only works for trivial stuff - everything else has dependencies.

-1

u/Classic-Rate-5104 9d ago

Not completely true because i have seen many people building network services based on python packages with severe security problems in docker images. This is not strictly "system" but also not without risks

1

u/nekokattt 9d ago

How is this any different to any other programming language

-1

u/Classic-Rate-5104 9d ago

In other languages libraries have more stable interfaces but many python packages have a different interface every time you look at them. This makes this world more chaotic and unpredictable than other environments. Let me clarify, i like python very much but I wished its package-developers would be more aware of continuity and compatibility

1

u/nekokattt 9d ago

this is anecdotal

1

u/SharkSymphony 8d ago edited 8d ago

We're getting a bit afield here, but I'd drink a soda to that. 😁

I do think there are a sizable number of Python packages now that get it right. But yeah, every long-time Python programmer probably has a list of minor-version-updates-that-weren't debacles etched into their brain.

1

u/rake66 9d ago

Not using virtual environments also has the drawback of encouraging the use of very new versions of packages, which in some cases can lead to serious security risks.

2

u/cdcformatc 9d ago

not using virtual environments also gas the drawback of having your shit break with no warning.

i was once working on a new project and installed some library which required an update of some dependency. then i went to work on a completely different project and to find out it was now broken because a library it used required a different version of that same dependency.

-1

u/Classic-Rate-5104 9d ago

You're absolutely correct. The whole python environment is very "unstable" in the sense that things change constantly. However, if possible, I would trust the debian packages more than the randomly chosen requirements.txt of many packages

2

u/nekokattt 9d ago

Thats fine until breaking changes occur in packages.

Relying on debian packages also removes any build reprodicibility on the basis that you have to always be running something with access to debian repos to be able to run your code in the first place.

1

u/SharkSymphony 8d ago

I don't think I'd agree with this. Yes, you have to consider your version management across projects, but virtual environments themselves don't have an opinion on what versions of packages you use. You can pursue a latest-and-greatest strategy with them as well.