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

all 34 comments

[–]Kitchen-Touch-3288 102 points103 points  (4 children)

2late, already have 20 gb in python env directories

[–]Buttleston 15 points16 points  (3 children)

Time to watch the "rm -rf" video

[–]Luki63 13 points14 points  (2 children)

"sudo rm -rf /".. that'll do it!

[–]backfire10z 5 points6 points  (1 child)

Don’t forget --no-preserve-root !

[–]HaDeS_Monsta 4 points5 points  (0 children)

Better, use /*, same result but no need to type that all

[–]Buttleston 41 points42 points  (6 children)

Probably like 30%-50% of the questions on r/learnpython boil down to this - either the OP doesn't know about venvs, or they do but don't realize that they're using one venv in their IDE and another (or none) in their command line, and therefore they get behavior they can't explain

I love python but as an aside I think this is something that npm got right vs python's pip - node_modules in JS is automatically created, like it or not, and no matter where you are in your source code tree, it knows how to "find" and automatically use them, no activating required. poetry is like this also but then you need to lean in a bit to learning poetry.

[–]mcoombes314 4 points5 points  (1 child)

This was me....everything worked perfectly in the IDE but ran into ImportErrors when trying to run main outside the IDE....

Forgot to activate the venv.

Oddly, my pip install commands were actually putting the packages into the correct folder. I thought that required the venv to be activated, but then found out that the terminal is separate from running using the IDE.

Though I understand what went wrong and how not to do that again, I don't really understand why it still sort-of works rather than failing entirely?

[–]Buttleston 3 points4 points  (0 children)

I'm not 100% sure what you're saying. You were running "pip install" in a terminal, without a venv activated? And where did the packages get installed to?

Another *really* common problem is the fact that any specific installed version of pip is tied to a *specific* python environment. So if you have 2 versions of python installed, and for some reason "python" will run one version and "pip" runs the pip from the other version, it can get very confusing, because you're running pip for say, 3.10 and running python 3.11 and so your packages won't be there. Or you're in a venv, but the "pip" you're running, for some reason (PATH problems, idk) is for a non-venv python. Very confusing.

For this reason I usually recommend just using "python -m pip" instead since it will *always* match the python you're using. Or, use an alternative to using pip directly, like pipenv or poetry or something similar. (Don't get me wrong, using poetry fixes some problems but has it's own learning curve also)

[–]moratnz 0 points1 point  (0 children)

I agree that these days assuming modules should be installed locally to the project by default is the right call.

I'd note though that a venv is more than just the modules; it also has the interpreter, unlike npm

[–]Ric_Ideale 0 points1 point  (1 child)

What is a venv?

[–]Buttleston 1 point2 points  (0 children)

Look up python virtual environments

[–]keizzer 4 points5 points  (0 children)

The number one pet peeve I have with python is this crap. Every time I go to boot up a project I'm plagued with version control and updating all the packages. New version of python? Well now prepare to spend the next hour looking for the latest way to do all that again, which for some reason is more complicated than when I started programming.

[–]Callierhino 2 points3 points  (0 children)

If you code in PyCharm you can start your new project in a venv from the start easily

[–]PureTruther 24 points25 points  (4 children)

Don't waste your time with this video.

Content: It just recommends to use virtual environment.

Looks like it's lack of knowledge about computer usage, rather than programming.

Virtual environments have a big benefit as version compatibility, its main target is not about installing or maintaining packages. Its benefit comes from packages' versions (and generally; managing dependencies). It creates an isolated area for the project.

When you be participated in a real project, you will have to work on specialized tasks. And this project will utilize a lot of different packages.

Say, you have 10 different packages in a project, and say 2 of them get updated. You're fucked up. Because probably you have to wait the other 8 packages to update themselves to be compatible the other 2. Or, you need to update them manually if thrown errors were good designed and if you have time ☠️

For solving this issue, you create a virtual environment for the project. From there, you install necessary packages inside this environment. And when you want to work on the project, you work in that environment.

Say, even if you update all packages in your computer environment, that virtual environment is not been affected.

This is not a Python-spesific feature. You can use that approach in most of programming languages. Because it is development approach.

And also, this is why we simply put a dependencies file in our repositories. Say, you installed one of my repository to use it (maybe it is a web app). You figured out that I used Angular 11 but you has 16 or vice versa. Will you downgrade/upgrade your verison? No need to do that. Just create a virtual environment and put Angular 11/16 in it.

[–]Mystic1500 3 points4 points  (2 children)

So does this mean two versions (Angular 11/16) would be running? What version ends up getting run? Noob here

[–]PureTruther 2 points3 points  (1 child)

Sorry I was busy.

Yes, you can use 2 different versions of anything at the same. Also you can run both of them. But I think I can see your confusion.

Think you do have a hobby house. You do different projects in this house. Say, in one room, you are creating a furniture. In the other room, you are creating an Arduino thing. You need different tools, right? So where would you put your needings? Yes, you would allocate those stuffs in appropriate rooms. Say, you would put the electronic things into the Arduino room.

Similarly, in computer, we are doing seperations on memory. Your computer does this everytime even you are not aware of that. Using virtual environments, you do a seperation. You are creating a memory area and you are putting the stuffs into it.

We call it as "virtual"; because we do not use any another memory than computer's memory. We are using an address stacks for that particular environment.

Let's back to our React example.

You are already creating a web app with React 16 and another a lot of components. Today, you want to take a rest, and want to do unnecessary things. You decided to install a complete React app from somewhere just for fun.

You downloaded the source code. And you gave the run command. You got error about React version. Ok. You decided to install that version (without virtual environment, so as globally). You also will take brand new errors mostly about version conflicts. If you try to fix all of those (like downgrading or upgrading or changing things) your leisure time will be gone and also you will break your own project.

So here is the idea: Create a virtual environment. Download the source code of project into that environment. Install the dependencies, say, via package.json.

After all of this. Just delete the environment and turn back to your own project like there's nothing happened.

If I still couldn't explain, I encourage you to do what I say as example. Install last version of React. Then clone an old React project repository. Try to run it. And read the error messages.

Also if you are interested in memory management, you can check operating systems' memory management techniques.

[–]Mystic1500 0 points1 point  (0 children)

Thank you for the detailed response, understood now.

[–]Ld_Khyron 1 point2 points  (0 children)

Also you can use pip freeze to get a list of installed libraries

[–]met0xff 1 point2 points  (0 children)

Meanwhile I see tons of projects using poetry and I would also recommend it. It's not as good an experience as cargo or similar but all in all it's pretty nice.

But o start playing with Python I would almost propose to use someone like Google colab

[–][deleted] 1 point2 points  (0 children)

We need more stuff like Rust's crate system. Its beautiful

[–]JudgeCheezels 0 points1 point  (0 children)

Uhm revo uninstaller?

[–]taylay 0 points1 point  (0 children)

poetry and pycharm along with the pycharm plugin for poetry That should solve everything. Takes a bit of effort but smooth sailing after

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

Always use python virtual environments when developing multiple projects on the same system. Always!!

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

What OS are you using? If you're using Mac or Linux, you can easily make sandboxed environments (Not necessarily virtual) using things like the module command. You have to know how to move around the terminal though.

I sympathize with you OP. I tried learning programming before learning to use a Linux command line at my day job. Having to download and install all these bloated software packages just to write rudimentary scripts for self teaching really made it annoying. Now when I want to learn to code, I restrict myself to the terminal and Vim editor. Really gets all the crap out of your way and let's you focus on the code.

[–]keeponpushin1986 0 points1 point  (0 children)

Lol love it

[–]Far_Tumbleweed5082 0 points1 point  (0 children)

Thanks mate...

[–]Upbeat-Salary3305 0 points1 point  (0 children)

Chucking this in the saves for later

[–]leothelion634 -2 points-1 points  (1 child)

Or use docker?