143 points · by surprisetalk · 2y ago · 281 comments
> You will not be forced to work inside something known as a “virtual environment.”
Oof, this terrible advice cancels out an otherwise reasonable post. Beginners who don't know what they're doing are the last people who should be `pip install -r requirements.txt`-ing into the system Python the way this article is recommending. That's not only going to make working on multiple projects nearly impossible (especially for the kind of beginning students who get recommended Anaconda, which are almost always the Data Science-y crowd using NumPy, Pandas, Scikit, etc, which are notoriously finicky with version conflicts), but it stands a good chance of breaking other Python-based system utilities in completely opaque ways. This sort of advice can fubar a naive user's entire workflow.
I know virtualenvs suck to explain to people, but in my opinion it needs to be done before you ever tell them about `pip install`.
The python packaging system has to be used in case studies of worst design decisions of all time.
That's not hard. Java's package management is well thought out and sane.
The advantage is that it's less "magic" than, say, npm. No need for rules saying how to find node_modules, you just have a PYTHONPATH env variable.
The Rust and Java approach is to do everything through a build tool's CLI, and I can't complain about that. It's probably the best compromise: Less magic than npm, and more user-friendly than Python.
The bar is so low, you have to excavate it from a landfill of rotten node_modules...
The Python community needs to solve this ASAP. This almost weekly pain point has turned a language I used to love in college into one of my most despised languages. The fact that the ML community uses this broken platform is infuriating.
Make a Python version 4 that focuses only on fixing the packaging. 100% per-project hermeticy. No global packages whatsoever. Solve just this issue and bring the entire ecosystem on board. Kill all the various virtualenvs, the anacondas, global packages. All of it.
Learn from Rust/Cargo. That project does it mostly right (sans lack of namespaces and reproducible builds).
Sounds like a skill issue that shouldn’t be seen in “10k+/seat/year software.” I wonder what other fails are in it.
After that you might understand the OP's viewpoint.
What made Virtual environments work for me was switching to a different Editor. I use Visual Studio Code currently it works well with Virtual Environments.
When you first create a new python file in a given "project folder" it prompts you to create a new Venv and when you switch project folders it remembers and restores the Venv for each project.
One of my work colleagues pointed me to VSCode - it streamlined a lot of python things for me.
If your students are disciplined about creating a new folder for each project managing virtual environments vscode could help them.
Only issue I have is my work office has a corporate proxy setup and pip needs certificate to connect (and if I work remotely I have to turn this setting off) I wrote a shell script to toggle between the two proxy settings. Not sure if university will have the same issue proxy issues but if so this would certainly be a pain point for many students.
It does answer the question of which text editor to use when the time comes to teach them about editing text files. For a course, it helps that it is free and available on both macOS and Windows.
I have a vague recollection that at some point I was testing without an install of Python and after I selected the Python extension for VS Code, it offered to install one for me. (I don't recall if this was on macOS or Windows and memory could be playing a trick on me.) But in any case, the people behind VS Code do seem to be trying very hard to make it easy for someone who is getting started.
I am a little uneasy about the nudges to use Copilot, but on balance, it might offer a better path for students who are getting started.
I think it is interesting to consider why Python is so popular despite such fiascos. The answers can be very informative but can also be giant red flags.
C/C++? Full of footguns. Java/C#? Too complicated for beginners. Pascal? Outdated. Etc.
I personally prefer C to teach first-year CS students but just as the lesser evil. A good first programming language is sorely lacking.
(Note that I'm talking about the imperative programming paradigm. The debate on whether one should start with functional programming is outside the scope of this comment).
can you? Because I can't.
e.g. "What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?" ( https://stackoverflow.com/questions/41573587/what-is-the-dif... )
what is the difference between conda and anaconda?
etc etc
The projects I work with have moved to it because it handles multiple languages (not just Python). It's made things easy to keep "the correct version of a language" that each project needs.
So far so good, anyway. :)
I could see giving them a remote access to a prebuilt env (which can be containerized) to be simpler to explain.
The entire problem of python an venv IMO comes from it not being the default.
For workstations, absolutely, go with virtual environments, it's the only way to go. One concern I've seen from some in the machine learning space is that rebuilding a virtual environment, for example if macOS upgrades the Python version, takes hours. That can be solved by using pyenv, then you can have multiple versions of Python and be free of Anaconda. I primarily use pyenv to be sure that I have the same Python version that ships with the server OS on my laptop.
If you are looking into a system you are unfamiliar with, where do you look first?... pip? pipx? homebrew?... or is it in anaconda? pyenv?... must be in the os package manager... apt? pacman?
Honestly, Maven and NPM look great compared to this mess.
[global]
break-system-packages = true
I would have preferred single-version-externally-managed to keep fond memories of setuptools alive.It becomes increasingly impossible to track down home directory pollution and config files in Python. Next step will be a Python registry on Linux. How about:
regedit VENV=/home/sjw/venv42 KEYWORD=single-version-externally-managed DWORD=0xbadbeehttps://packaging.python.org/en/latest/specifications/extern...
export PIP_REQUIRE_VIRTUALENV=trueThis is because Python doesn't have versioned imports, which means you can't have multiple versions of the same package in the same environment, but I like to dream about a world where this isn't the case. If instead of import foo we had import foo@x.y.z#optional-checksum, the Python world would be massively improved. It seems like it would be such a simple change too.
Rust has versioned imports. So you can import different versions of the same module (at least in your transitive dependencies).
While not familiar with macOS (topic of this article), I think the Mac installer works the same.
On Debian of course you can cause great damage because the system Python is used for critical system functions. This is silly, I think the system python should be an isolated install in /usr/sbin. Better yet, move back to Perl for the system.
I do use one piece of commercial software that includes Python. You can see the installer saying "installing Python." I suppose it should fall on the OS and software vendors to not abuse the infrastructure... "to whom much is given, much is expected," but maybe too much to expect.
Then I can pip install away care-free, and quickly run scripts / repls without having to activate an environment.
On macOS, there hasn't been a system Python since 12.3 came out in Jan of 2022. And as far as I know, there was never a system Python on Windows.
It may not be pre-installed, but if you install Xcode it will also install python3 in /usr/bin/ for you...
Xcode does put a binary with the name python3 into `/usr/bin`. This is not a system Python, but set that aside.
Can you explain how someone might pip install libraries to this instance of Python?
Arguably step one is: https://docs.anaconda.com/anaconda/install/uninstall/
Or for miniconda: https://gist.github.com/pulkitgangwar/421a1af800c5a9c6d4a77b...
Then, adopt `uv` instead, for all your Pythons: https://docs.astral.sh/uv/
The 2024-08-24 update finally does "all the things" — unified Python packaging: https://astral.sh/blog/uv-unified-python-packaging
. . .
PS. I noticed this original article's subsequent blog post gets into "env as code" but for "one tool to rule them all" one may want to consider a tool such as `mise` instead: https://mise.jdx.dev/about.html
uv looks great, but its coming from a VC-backed company with no apparent way to monetise. Discussion here:
You have no idea what Astral’s other (future) plans include. For example, what if they are working on all this tooling so they can start providing “verified, reproducible Python packages” as a service, to mitigate against supply chain attacks?
Telling people not to use Astral’s products because they are VC-backed is silly, especially since everything they’ve done so far is permissively licensed.
I think there is a consensus about what a student should end up knowing. Even if they are not going to become developers, they should be able to edit text files; they should be able to run commands from the terminal; they should understand PATH. They should create a virtual environment every time they start a new project.
Where we might disagree is how to get them to the point where they know all those things.
I offered to write this post for a colleague who is now facing the issue I faced when I taught last spring. My only goal was to help the students who can't get started running Python from python.org. When I say that they don't know what an editor is or how to use the command line, I just taking those as the facts on the ground. What I didn't say (at least not very clearly) is that they need to learn these other things. I did hint in the end that some of these probably need to come before learning to use a virtual environment. I know this is controversial.
Because the first post was narrowly focused, I wrote a subsequent blog post called "Environment as Code" that is more specific about the goal and offers a specific sequence to follow to get there.
If you have any reactions, I'd be interested to hear them. In a deep sense, I think that the issue here is how to free students from the GUI. If we can do this, it will change how they interact with the computer for the rest of their lives. If there is a better way, I'll be happy to support it.
On Unix, it is even trivial to have parallel installations.
Build two pythons:
./configure --prefix=/home/foo/a && make && make install
make distclean
./configure --prefix=/home/foo/b && make && make install
Install packages: /home/foo/a/bin/python -m pip install bar
/home/foo/b/bin/python -m pip install quux
This is completely isolated. You can do the same by using the Windows installer to install to different directories. If an installation breaks, remove it and reinstall.My experience is that people who recommend various environment software often like strict bureaucratic procedures, or have a financial interest in pushing the software or are simply not experienced and do what they are told.
The general observation I would add is that these change with the level of experience of the person writing the code. I found it helpful and harmless to avoid them when I was experimenting. Now, I use them automatically.
I know that it made people angry, but I think it was reasonable to say that if someone does not know how to edit a file and is not comfortable using the terminal, they are not ready for virtual environments.
Re separation, another option is the one I recommend in the "Environment as Code" post: Right now, I'd suggest installing
- Python3.12
- Python3.11
- Python3.10
Then use edits to `.zprofile` to specify which will be used in any terminal session. It does require the use of an editor to make changes to `.zprofile`, which is where the official versions put the lines that add things to the user PATH. I think it is very helpful to get people familiar with how profiles set PATH for any terminal session and how easy it is to control by commenting out or commenting back in lines in `.zprofile`.
Later, one can introduce them to `.zshrc`.
Dealing with environments and understanding how different parts of the filesystem relate to each other is its own pretty steep learning curve. You wouldn't want them to get tripped up on that while they are learning to program, so I think I would opt to teach the two as entirely different concepts and not mix them at first.
No idea if an appropriate IDE/plugin combination exists! Surely there is one.
A second alternative is the Idle environment that is included with every official install of Python from python.org. I have not used it much and I've never tried to teach a course with it. But it comes from the most trustworthy source in this complicated environment.
I'd be very interested in hearing about experience using these to start learning from the very beginning or using them to teach a course for people who are just getting started.
> Although one can install a plain-vanilla Python and all required libraries by hand, we recommend installing Anaconda ...
In a novice course, if the official version of Python and its tools all work, why is this organization recommending a product from a for-profit organization that requires acceptance of some complex and potentially costly provisions in its terms of use?
conda create -n "myenv" python=3.8
conda activate myenv
Then just use pip in your environment, if you don’t like conda packages. Not sure what the paragraph about being able to install multiple Python versions after getting rid of Anaconda is about.The sentence says "install and run". The problem is with "run."
On macOS, most students who install Anaconda accept the default, which is to autoactivate the base conda environment every time someone starts a terminal session. As a result, the instructions for starting an official Python don't work.
If you use Anaconda on Windows, you will not be able to test the effects of autoactivate.
It looks like
conda config --set auto_activate_base false
would be another fix for that. Or adjusting .condarc manually if you insist on not using the CLI. However I think teaching students about (virtual) environments will require them to know at least some CLI basics anyway.And as I've indicated in other responses, I absolutely agree that students need to learn how to edit files and run commands from the terminal. The only question concerns the order in which to teach these basics.
But please understand the facts. The majority of students I've encounter who have Anaconda installed on a Mac, had totally given up on the possibility of running an official version of Python. This, by the way, is a big indictment of the Anaconda Navigator, which encourages students to keep using a GUI instead of mastering files and shell commands.
Part of why I recommend the basics:
- an official python - pip and pypi.org - venv from the standard library
is that it gets them out of this pattern of this dependency on a GUI. This, by the way, also gives me some hesitation about VS Code. It too reinforces the GUI as the way to set up a working environment.
If you have license issues with the official Anaconda repos (talked about in other comments), switch to the conda-forge channel instead, it doesn’t have these restrictions: https://anaconda.org/conda-forge/python/files
Hyper-specialisation to the point where you "know python" but do not "know environment" (be it environment variables, virtual environments, shells or operating systems) seems like a rather pointless exercise.
If, however, someone wants to scope their class to teaching only a single thing and dismiss everything else and stick knowledge in a silo, why not provide them with a preconfigured option? This is an avenue that works with many languages, with or without GUIs, and with many steps or very few:
Maybe a Docker Container and a free download of Rancher Desktop (for any major OS) is a good option. You give them 1 link and 1 command (or just a picture of the GUI!) and you're good to go.
If that doesn't work because you're using some sort of fancy editor that doesn't work with containers, the fancy editor (like PyCharm Community) usually comes with a built in option to spawn a managed python environment just for your project. So that's a second great option to "solve" this problem that shouldn't exist in the first place.
If both of these options are too 'big', there is the super simple option of not involving the local computer at all. Stick them in a webbrowser. From CodeSpaces to just Python playgrounds, there are a ton of indestructible options to pick from. Works for other languages as well.
All of this will allow anyone to not learn anything about where a language and runtime sits or how to make it do what you need it to do, and instead you can write a bunch of code that doesn't interact with the world at all, but still get a certificate that says that some course was followed.
Despite the myth, it is not hard to use unless you need to package something complicated or messy. Then it may become hard.
The advantage of Nix is total version reproducibility and declarativeness. It's quite reassuring to know things are exactly the same on all computers.
The problem is that everything works--until it doesn't. And then nobody knows how to fix it.
You wind up needing a single person who serves as local "nix tech support" who then hates life because he's a high end devloper dealing with nix n00bs all day long.
Programming language-specific dependency tools succeeded due to their user friendliness, however abysmal they are in multi-language / OS dependent environments (and I despise them for that reason). Cargo.toml, go.mod, conan.txt, Dockerfile etc. all are can be understood by basically all members of a dev team. You cannot say the same for Nix for most devs in a team.
I wish though, that at my job people were as far as using Nix oder Guix though. Still a long way off of that. Wish people would explore more on and off the job, to realize the potential.
Most of package managers don't offer that guarantee. Only Guix has a similar level of assurance?
Nix openly recognizes outputs are different per platform, x86_64-linux is different from aarch64-darwin. Impossible not to, as ultimately architectures may behave differently.
I feel somewhat blessed that I get to work on tools at a company that can support a dedicated tools team. We are free to use the best tool for the job without needing to worry about popularity contests, and nix has truly been a game changer for us.
Frankly, the biggest problem is that it needs some better docs, i.e. some serious corporate funding to get them written.
But seriously, let's say you have a Python project (like a Django application with a couple requirements in your `requirements.txt`), what is the least disruptive way to get it working? Many things I've looked at involved doing one-time transformations of requirements files or otherwise easy-to-desync things...
It’s also excellent documentation, written by someone who clearly knows their audience. It keeps all operations in GUI-land, which most users consider more safe. It avoids almost all technical explanations.
Folks responding with “Terminal is easier” are missing context. You’re not the audience. The fact that you can come up with seven solutions for this proves that :)
"Many of these students have not used the command line."
I'd suggest that we are failing to teach students learning to code, the basics of how to use computers. Pre-requisite should be a class on how to use the command line.
The CLI is literally just: You write a command and a thing happens. That isn't a thing that people get hung up on. The main weirdness you need to tell them about is how to select/copy/paste and use the history. Throw in a lesson about how paths work. Sure the commands need to be remembered but for a start you basically just need pwd, cd and python/python3.
You are not doing your students a service if you try to teach them programming by shielding them from anything that could give them a feel for the systems they develope for.
Which is unfortunately exactly what enables so many tech support scams, so I'm not sure it's a pattern worth reinforcing.
In addition to that, while it might still be helpful for completely non-technical users, I really don't think "avoiding the terminal and edits to files" is a desirable goal for anyone studying either Python or data analytics.
Most people learn computers because they want to get better at a task, like programming. The point of this post is to help people get unstuck so they can begin learning at all. Adding more gates in front isn’t going to help.
Beyond the attention seeking 'tips of the day' kind of popups in random places and stages that derails you all the time (I mean: ALL the time) but at least so tiny little fragments of wisdom that you reamin the same clueless, well inside the error of measurement in cluelesness. Only good for putting The Product into the focus (Am I nice, eh? What a cool feature I have, eh?! Brilliant am I, eh?!) instead of the need of the users.
Of course I'm quite a bit biased because the majority of these people are coming to me for help...
Back to Anaconda. It's great system, and the problem is people learn how to use ML or whatever using Anaconda, and then when they get into the workplace, you need a license.
I really wish organizations would just buy licenses for the users that want them, it's so much of a pain to work around not having Anaconda. Their prebuilt packages are pretty helpful and time-saving, even thought you "don't need it" and you can get along with using just pip, but you'll have to provide workarounds for projects that use Conda.
I had a pretty rough time with Anaconda Team Edition when it was rolled out by my employer. The security team required that any package with a high CVE score be filtered out of the on-prem repository. Fair enough, but it just led to packages and dependencies being constantly broken. IIRC, it wasn't possible to give normal users the ability to view CVE scores, which just compounded the issue.
I do not recommend pip in serious projects, as it lacks the above. Here's things that I've seen:
- add dependencies and not save them in the requirements file - forget to activate cenv and install packages globally - different package versions across deployments due to absence of lock file - main dependencies mixed with dev dependencies
When I talked to them four years ago, they agreed we were good to use it for free, no problem.
The dollar figure they're asking for would make it the single most expensive software product we would be licensing in our enterprise, by a lot. The deadline is absurdly soon for such a big deal. And they opened discussion in an incredibly hostile manner and have made no attempt to work with us.
So, I'm helping lead the effort to completely purge them from our ecosystem. On the one hand, I'm sad because their stuff is pretty good. On the other hand, their behavior is bad and the product isn't better by the amount they're asking for.
Good riddance.
With $$ for unlicensed period too?
Wow this is so deliberately ambiguous about universities with more than 200 employees. Shameful.
But: what's sauce for the end user goose is sauce for the developer gander. Developers are users. Give developers good visual tools. Sadly I don't think there is a visual front end to git,, for example, that is any good among the handful I have tried and the dozens that attempt to make git safe, discoverable, and visually obvious.
Python is often billed as a good language for beginners because it does not require the author to keep track of curly braces or line-ending semicolons, and it lets the author focus less on syntax and more on reasoning with code.
Wouldn't it be nice if the beginner could focus on learning Python instead of having to learn stupid minutiae that only exist because we're still trying to maintain backwards compatibility with the VT220?
Octave, Matlab, Mathematica, Julia, R are superior for small programs with just a couple of functions.
Escaping the stranglehold of Python would be the next project!
Sure, but I would argue that overwhelming someone with all those topics at the beginning is counterproductive.
I learned programming (in BASIC, in 1981) before I heard about the filesystem or shell. Making more complex programs do interesting work was my reason for learning more about the innards of computer systems.
As an analogy- It may be just as easy to do, but I’ll never bother to change the spark plugs in my car - I’ll have someone knowledgeable do it. Do I think it’s hard? No. Do I think I have better ways to spend my time? I do.
There's just a lot of people that did not grow up using computers the same way as many here. Whether it's they grew up on mobile devices or have only ever used a GUI.
I don't think it's reasonable to expect a developer to not understand command lines but plenty do not. There's whole populations of very junior programmers that have zero experience on the command line. Hell I know people with decades of Unix experience that don't know basic CLI stuff or reach for the GUI to install packages or manage things.
macOS (and the entire Unix universe) is a shitty environment for people who rarely / don't need terminal but whose job could be improved by a lot if a scripting language is used.
Microsoft used to champion intermediate users who need more power than mindlessly clicking web pages. They are now turning their products yet another copy of the other systems where you are presented with a cliff as the learning curve.
I disagree completely that a simple programming environment should require terminal access. Terminals are unforgiving for mistakes that a beginner makes. Most of the terminal capabilities should be done in GUIs. One should be able to access all files, edit them, change the OS environment variables and create programming projects in GUIs. We achieved that in 90s already. Why should we go back?
What does "Unix experience" even mean if you can't sling the basics in the shell?
Based on limited testing on Windows, Anaconda does not seem to do anything comparable on that OS.
As engineer of many many years now all I can say with certainty is that confusion, failure, research and eventually small successes have been the central theme of working with computers. I personally love the struggle and mystery but its not for everyone. At the end of the day its about what you want to get out of it. If you want to write programs without learning about the terminal go for it, but I would say there are better gilded cages with GUIs that help you get a lot more done within that constraint.
If you want a deeper understanding, I think its more helpful to admit you can't cheat the process of learning. Slow is the only speed.
Might be a lost cause in the tiktok era, but the text / command line / unix pipes paradigm of computing is extremely powerful and it would be a blessing if educators would find way to make it attractive to students...
https://blog.glyph.im/2023/08/get-your-mac-python-from-pytho...
But those might be too late if the students run into these issues during their very first weeks. And Python is pretty decent for Hello World kinds of programming-adjacent courses.
Anaconda is a distribution, which to my understanding, includes an official version of Python. Anaconda includes its own way of doing things to solve a lot of common problems people face.
Personally, I don’t use Anaconda any longer because I ran into so many issues over so many years with conda, their package manager. I’m also almost competent enough to deal with a lot of the problems that anaconda tries to solve. This likely isn’t the case for beginners.
Virtual environments are great and, I’d argue, what people should be using 99% of the time.
Try installing a specific version of an R package from more than 2 years ago, it's an exercise in frustration,
This is exactly why you should learn to use your tools properly, learn just a tiny tiny tiny bit of posix shell (which works for ALL posix shells sh, tcsh, bash, zsh etc...) so that you can understand what the heck is going on.
Like seriously, this is day one stuff. Your shell loads a configuration file and you can set programs to run on start, or configure shell options, set aliases etc... This is nuts man.
in fact I was so baffled that I wrote my own article response https://sweetbbak.github.io/post/response-to-mac-anaconda/
1) How can we get a specific group of students who feel helpless started on the path to learning?
2) What should we teach students?
In the post linked here, I tried to be clear that these are instructions for a very specific group of students who are stuck and don't know what to do. I agree that they are not the right instructions to offer to the typical reader of this site.
The comment and the second post that it references address what we should teach.
True, MacPorts isn’t the official Python installer, but macOS doesn’t have an official Python anyway.
These instructions are a fish, and don't really any is the core principles and knowledge that a developer should really know.
Yes, it fixes the problem but doesn't really address the "Why?" part at all.
There are a ton of subtleties in the build toolchain even within a single OS type, and these will lead to downstream frustrations with packages that just bundle pre-compiled versions of C/C++ libraries.
The conda approach treats the underlying libraries as first-class citizens in the package ecosystem; tracks their interdependencies; and most packages are built in such a way that they are relocatable on your filesystem and don't require system privileges to install. conda and the various packages in the conda universe (whether official ones from Anaconda or the community-built ones in conda-forge) all make it so that this baseline hard problem is mostly solved across all major OSes, and solved in a relatively consistent way.
You can think of conda as a cross-platform, cross-architecture, multi-language, userspace package manager. It grew into this because numerical Python's package ecosystem is so horribly complex and the users span such a huge set of install environments, that conda ended up having to be a generic rpm/brew/apt kind of thing.
After taking a course using Python?
i've never understood the reasoning for anything but that
Terminal can be scary, but “open terminal from Applications/Utilities” and typing “rm ~/.zsh” is objectively easier. Also that’s not even the best solution, just a far better way to do this solution.
Also, “mv ~/.zsh ~/zsh-anaconda”.
do people really want to do things they don't understand at all?
But really, for his use cases, he should be using Julia (for DSGE modeling) or else R for stats.
Still, I applaud his willingness to try new things, if more economists were like him, we might finally break the stranglehold of stata and matlab in econ.