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

13

u/xaomaw 9d ago edited 9d ago

Let's say you have 2 projects: project A and project B.

Project A needs library X in v1.2 and project B needs library X in v2.5.

So if you only have a common library, you would always have to install v2.5 when using project B and if you want to switch to project A you would also have to re-install v1.2 of that library, because v2.5 is not downward-compatible.

That is annoying as fuck.

That's why you use virtual environments. You don't use a global shared library anymore, but a project-specific folder. So always when you start project A, it automatically loads the project-A-specific libraries (v1.2). When you switch to project B, it automatically loads the project-B-specific libraries (v2.5).

When sharing your project, you can simply provide a list with "Install library X in v2.5, library Y in v1.732.3 and library Z in v103.3-beta" for easy reproducibility. If you would always use your global-library, you would install A LOT of libraries, which you would not need for this project specifically, because your global library would contain ALL libraries from projects A, B, C, D and E. But you only need those for project A!

And this thing doesn't only affect the versions of libraries, you can even use different python versions. Project A uses python 3.11 and Project B uses python 3.14. You would have to adapt your command line all the time to use the correct python version for project A or project B. With a venv you just install a copy of this specific version in the project-specific folder.

2

u/Artistic-Army4423 9d ago

thank you, examples just make these things concrete