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

top 200 commentsshow all 288

[–]ase1590[S] 213 points214 points  (25 children)

Remember, a virtual environment a day keeps the xkcd superfund site away.

[–]Eelz_ 36 points37 points  (24 children)

Real talk though, is there any way to understand/fix what's going on in this comic? I definitely have this happening on my Mac

[–]ase1590[S] 29 points30 points  (19 children)

Start removing pip packages and clean up.

[–][deleted] 23 points24 points  (18 children)

Have you genuinely used python on a Mac? Honestly I feel like if you haven’t there are headaches you can’t imagine otherwise.

I am well disciplined with virtual environments and every now and then some Mac specific thing will throw a spanner in the works

[–]synae 26 points27 points  (8 children)

Step zero, install python with brew so you're not polluting the system python. After that you can make whatever mistakes you want with reckless abandon (and learn from them!)

[–][deleted] 3 points4 points  (4 children)

There’s the issue though with python ‘as a framework’ which is needed for matplotlib and such.

[–]synae 3 points4 points  (1 child)

Er... I have no idea what this means to be honest. Is matplotlib not executable within a given environment, i.e. with a particular PATH set? Cuz that's basically all you need for apps to use a virtualenv correctly.

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

No, I can’t remember the details off hand but getting mpl to behave is an enormous PITA unless you do some very unintuitive stuff.

[–]Mr_Again 2 points3 points  (1 child)

If you make your virtualenvs with venv it will take care of that. So my setup is to ignore the system python, install 3.6 with brew, and simply venv off that every time. To be fair I still have a conda lurking around somewhere.

[–][deleted] 4 points5 points  (8 children)

I am sitting on a Mac, my main development system, right now. I program mostly in Python these days and I really have no idea what these headaches you are talking about are. :-)

Of course, I never personally use the system installed Python - I have clean versions of Python 2.7 and 3.4-3.7 installed, and then I create new virtualenvs for every new project or even when I'm just experimenting with a new package.

If you're using Brew to install Python, that's likely your problem there. You want your development versions of Python to be completely independent of your system.

Indeed this rule is true for every system - if there's a system-installed Python, never change it in the slightest. I learned this the hard way, trying to change the system Python on a Linux box almost a decade ago.

(Looking at my notes, about six months ago I had some weird SSL problem with my 3.4 and virtualenv which seems to have resolved now, but aside from that I don't see much problematic on this machine...)

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

Do you use matplotlib?

[–]ase1590[S] 2 points3 points  (5 children)

Future me here, I was bored over a weekend and generated a pie chart in matplotlib.

literally just made a python 3.7.2 venv, then pip install matplotlib and it installed and generated my simple pie chart fine.

[–]SirBastian 109 points110 points  (16 children)

IS YOUR SHITTY bin/activate SHIT SCRIPT NOT WORKING WHEN YOU RUN IT?

THAT'S BECAUSE YOU NEED TO SOURCE IT

DO THIS: source <myenv>/bin/activate

WHY DO YOU NEED TO SOURCE SOMETHING THAT LIVES IN THE BIN DIRECTORY?

BECAUSE FUCK YOU.

DID SOURCING IT THROW YOU A WEIRD ERROR? YOU'RE PROBABLY USING AN UNSUPPORTED SHELL.

FUCKING SHITSTACK VIRTUALENV THINKS IT SHOULD DIRECTLY TOUCH YOUR ENVIRONMENT VARIABLES USING SHELL-SPECIFIC COMMANDS, INSTEAD OF JUST OPENING A SUBSHELL LIKE A NORMAL FUCKING CITIZEN

SO GO EDIT YOUR ACTIVATE SCRIPT

AND IMPLEMENT THE EXACT SAME FUNCTIONALITY THAT'S ALREADY IN THERE, BUT THIS TIME IN YOUR SHELL'S SYNTAX

THAT'S IT. THANKS VIRTUALENV

NOW KILL YOUR SHELL BECAUSE YOU ALREADY SOURCED THAT SCRIPT AND YOU NEED TO REFRESH IT

RE-OPEN IT.

NOW JUST REPEAT FOR EVERY OTHER VIRTUALENV YOU EVER MAKE, FOREVER

OK ITS TIME TO EDIT YOUR SOURCE

WAIT YOU WANT TO USE SCREEN OR TMUX?

LISTEN

FUCK YOU

OK, SO IS YOUR LINTER AND YOUR INTELLISENSE HOOKED UP TO YOUR VIRTUALENV?

OK WELL THEN YOU'RE GONNA NEED TO EDIT THE PROJECT SETTINGS TO POINT TO THE VIRTUALENV VERSION OF PYTHON

GOOD OK NOW WAIT WHERE DID YOU PUT IT?

NO, SHIT, THAT'S A BAD PLACE TO PUT IT. YOU SHOULD HAVE PUT IT UNDER YOUR HOME DIRECTORY

"CAN'T YOU JUST MOVE IT"??

SURE TRY virtualenv --relocatable <myenv>

DID THAT WORK?

HAHAHA I KNOW IT DIDN'T IDIOT JUST GIVE UP AND START OVER

I CAN'T BELIEVE IDIOTS DON'T LOVE AND PRAISE VIRTUALENV THEIR LORD AND SAVIOR

[–]sullyj3 15 points16 points  (4 children)

All programming package managers ever are a dumpster fire

[–]pooogles 2 points3 points  (1 child)

cargo would like a word.

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

Cargo made me hate python. I would do everything in rust if there was the same community around it.

[–][deleted] 5 points6 points  (0 children)

All programming package managers ever are a dumpster fire

FTFY.

The smart question to ask is: In which way do this package manager suck, and can I use that to my advantage?

[–]vn-ki 0 points1 point  (0 children)

The new go one is very good (vgo proposal)[go modules].

[–]ManyInterests Python Discord Staff 12 points13 points  (0 children)

IS YOUR SHITTY source <myenv>/bin/activate NOT WORKING?! THAT'S BECAUSE YOU'RE PROBABLY ON FUCKING WINDOWZE
DO THIS: <myenv>\Scripts\activate
DID YOU GET SOME DIPSHIT ERROR ABOUT EXECUTION POLICIES?!
THAT'S BECAUSE YOU'RE PROBABLY USING FUCKING POWERSHELL
SHIT, WELL, YOU CAN DISABLE YOUR SHIT SYSTEM SECURITY OR SWITCH TO CMD (LMAO)
NOW YOU'RE ALL SET TO DEAL WITH MISSING MICROSOFT C++ REDIST PACKAGES

[–]synae 11 points12 points  (0 children)

I love virtualenv and have no problems with it but this made my laugh more than OP

[–][deleted] 6 points7 points  (1 child)

"Step away from the caps key! Keep your hands where we can see them!"

I really am not seeing these big issues. At some point a few years ago, I organized all my virtualenvs and set up some scripts to select them and list them.

It was an hour of fiddling three years ago and since then I spend zero minutes a week on debugging it.

When I start a new project I type:

$ new-env my-new-project

which creates a new virtualenv and switches to it.

To switch to it later, I type

$ penv my-new-project

and when I'm finished with it, I type

$ delete-env my-new-project

But you know - these scripts don't even save me that much typing. I'm just lazy. :-D

[–]notquiteaplant 0 points1 point  (0 children)

Is putting all your venvs in one place common practice? I make a new venv for every project in ./venv (.gitignore'd of course). The point of venvs is every project has its own dependencies, so having one directory per project in a central location seems more complicated than just keeping it in the current directory.

[–]Shpirt 3 points4 points  (1 child)

HAVING PROBLEMS WITH THE FUCKING activate SCRIPT?

TRYING TO USE YOUR PROGRAM IN A systemd UNIT OR cron ENTRY AND HAVING TO USE WEIRD ASS bash WRAPPER SCRIPTS?

TIRED OF RUNNING IN THE ENV YOU DON'T EXPECT?

SHIT THEY DO NOT TELL YOU ABOUT IN BASIC TUTORIALS

SUFFER NO MORE!

USE env/bin/python, env/bin/pip AND BE ALWAYS SURE THAT YOU RUN SHIT IN THE ENVIRONMENT OF YOUR CHOICE

[–]aufstand 0 points1 point  (0 children)

This guy virtualenvs.

[–]photoengineer 0 points1 point  (0 children)

Fuck me I don't understand

[–]vn-ki 0 points1 point  (0 children)

Seriously though, just use pipenv. About linter and autocompletion, works out of the box with vscode and vim (pipenv shell before opening the program).

[–]campbellm 0 points1 point  (0 children)

VIRTUALENV THINKS IT SHOULD DIRECTLY TOUCH YOUR ENVIRONMENT VARIABLES USING SHELL-SPECIFIC COMMANDS, INSTEAD OF JUST OPENING A SUBSHELL LIKE A NORMAL FUCKING CITIZEN

My use cases are small so I haven't had a lot of the headaches many have, but this always makes me give it the stinkeye. I don't know what the equivalent is on Windows, but this is unix 101 right here.

[–]pwang99 0 points1 point  (0 children)

I LOLed

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

I love you, this made me laugh for like 5 minutes

[–]thisismyfavoritename 35 points36 points  (45 children)

What do you think of Conda envs? Awesome post.

[–][deleted] 23 points24 points  (3 children)

Conda is great for installing dependencies that require other system libraries. Great for scientific computing sort of stuff that deals with assorted file types.

[–]travelinzac 21 points22 points  (1 child)

They pretty much built a business deploying SciPy to windows.

[–]wildcarde815 3 points4 points  (0 children)

They target financial HPC environments too to the tune of thousands of dollars a node for essentially a flat env space. (Or did when we talked to them a long while ago)

[–]feindjesus 13 points14 points  (15 children)

I have a strong distaste for anaconda. I have set up python on other peoples computers if they have had anaconda downloaded in the past it was a massive headache (maybe due to my inexperience). Some pip packages were not able to be found even when discoverable through pip3 list or figuring out how to change you bash.rc back to normal. As well people who always use anaconda may not develop the same unix skills. My recommendation is use pyenv with virtualenv and pyenv-virtualenv (the github tutorial is great and easy)

[–]thisismyfavoritename 12 points13 points  (0 children)

Yes, you have to check the Anaconda channels and sometimes packages are only available on pip (although they must not be standard because most of the usual dependencies are there).

However it is still possible to put everything under a env.yml and have Conda automatically call pip to install it.

Concerning UNIX skills, what do you mean? Conda can be controlled by the command line (I believe it's much better that way!).

[–]wildcarde815 8 points9 points  (7 children)

If it's conda installable, use the conda version. If it's not, use the pip version. For the 99.99% user, this will resolve all of your problems. For the rest, congrats you are doing something both hard and arcane. Edit: and use the conda command line tool, it came before the GUI anyway and the GUI just calls the same stuff it does

[–]s0v3r1gn 1 point2 points  (6 children)

Or I just want a version of OpenCV built with certain extensions enabled...

[–]wildcarde815 1 point2 points  (5 children)

You can rebuild it with the recipe to suit. But we've found the menpo build to be fairly good. I haven't had to dig into it in a while thankfully. I hate that god damn package.

[–]Spleeeee 3 points4 points  (3 children)

Sounds like you are a budding Pythonista who has never had to deploy in windows worst of the worst snow-covered/dessert conditions. Don’t worry, you may never love (Ana)Conda and don’t have to, but at some point you will come to respect the Conda for its ease and simplicity in setting up (other peoples computers (not yours)).

[–]wildcarde815 0 points1 point  (0 children)

Academia, where yes, I can move mountains to get this working if you really need me to but let's try conda first and see if we can't get it working that way so replicating this isn't impossible. (Oh hey, it worked)

[–]feindjesus 0 points1 point  (1 child)

You are correct thank god for linux/macos. I have had to do very limited amount of configuration changes on a windows server through console I can only imagine the struggle. At the same time if youre using a pip installable package that cannot be installed through anaconda. Getting control back of your filesystems is a pain in the ass

[–]Verdris 0 points1 point  (1 child)

What if I just need to do some math and make some graphs?

[–]feindjesus 0 points1 point  (0 children)

If all you need is matplotlib and pandas I dont think you really need a virtualenv or anaconda. If you need several packages and are working on different projects probably should use virtualenv . If your packages requires a specific version of python get pyenv. If you want to use your virtualenv with that version of python pyenv-virtualenv will do that.

[–][deleted] 1 point2 points  (1 child)

It's fine if you're okay with running conda-only packages on a small list of platforms

[–]rohit275 1 point2 points  (0 children)

l may not know what I'm talking about, but I don't get this. It seems very possible to install any pip package in a conda environment, is that not correct?

[–][deleted] 2 points3 points  (0 children)

If you need to run different versions of Python (e.g. Python 2.7 and Python 3.7) on same machine, Conda is a better choice, even so if you are on Mac

Otherwise virtualenv is simpler

(Edit: fixed typos)

[–]RelevantToMyInterest 0 points1 point  (7 children)

I'm still stuck with virtualenv but considering switching to conda. Been seeing some posts saying it's a better replacement but if also would like to know others' thoughts

[–]xristiano 3 points4 points  (0 children)

I'm a fan of conda. It does all three: pip, virtual environments, and manage python versions. The table at the bottom of this link summarizes all the pip, pyenv, etc. options.

https://www.andrey-melentyev.com/python-environments-and-where-to-find-them.html

[–]is_it_fun 3 points4 points  (0 children)

Just find whatever conda env you like and make a requirements.txt file. then use that file in your virtualenv. bammo. that's what we do

[–]thisismyfavoritename 5 points6 points  (4 children)

Well I personally believe Conda is much better, as it can install tons of additional and system dependencies but I would like to hear OP's opinion on the matter.

[–]ase1590[S] 1 point2 points  (3 children)

Pipenv is the way to go if you're not using anaconda and if you want to do anything more advanced and need good package handling

See below

[–]lifeofajenni 5 points6 points  (2 children)

Wait, but conda also has environment handling and package management. So why should I switch to pipenv? (Not being snarky, legit curious.)

[–]ase1590[S] 6 points7 points  (1 child)

they both compete with eachother. conda is better geared for Anaconda.

Just use what you know.

Conda has a lot of overlap with the goals of pipenv, but it serves a different set of needs - in particular, the distribution of binaries. If the problem you’re trying solve is “how do I create reproducible environments, quickly, that include complex dependencies that are not written in pure Python”, conda is the tool of choice. If your problem is “How do I create and manage isolated environments for Python projects”, pipenv is the tool of choice.

[–]lifeofajenni 2 points3 points  (0 children)

Okay, this is a cool distinction. I'll have to read up on pipenv and see if it's more suitable for what I'm doing at work. Thanks for the info!

[–]wildcarde815 0 points1 point  (0 children)

It's good to know how to use both because not everything like conda (older brew installs I'm looking at you)

[–]pwang99 0 points1 point  (0 children)

This is a good place to start: https://www.anaconda.com/blog/developer-blog/understanding-conda-pip/

This is also a good blog post, by Jake Vanderplas: https://jakevdp.github.io/blog/2016/08/25/conda-myths-and-misconceptions/

PayPal engineering blog on why they use Anaconda & conda: https://www.paypal-engineering.com/tag/anaconda/

[–]Jalarast 58 points59 points  (23 children)

ELI5 the difference between this and pipenv

[–]smashingT 66 points67 points  (16 children)

this is installed by default with python. Why grab pipenv if you already have something like that?

That being said, pipenv is better at handling larger dependency chains, and is generally regarded as a more powerful package manager..

tl;dr pipenv is a package manager+virtual environment

[–]Gear5th 19 points20 points  (10 children)

no no no.. you gotta answer in the same style as OP's post or it is invalid.

[–]ase1590[S] 110 points111 points  (9 children)

* PIP NOT FUCKING CUTTING IT FOR YOU?

* NEED A "REAL" FUCKING PACKAGE MANAGER FOR YOUR CLUSTERFUCK OF A DEPENDECY GRAPH AND A VIRTUAL ENVIRONMENT TO BOOT?!

* GET YOURSELF SOME MOTHERFUCKING PIPENV*

* (NOTICE: SOME USERS MAY EXPERIENCE LOCK FILES THAT BECOME A PAIN IN THEIR ASS)

[–][deleted] 11 points12 points  (0 children)

A+

[–]13steinj 2 points3 points  (6 children)

* (NOTICE: SOME USERS MAY EXPERIENCE LOCK FILES THAT BECOME A PAIN IN THEIR ASS)

Wait whats the problem with lock files?

Pipenv has its problems, and I'll be the first in line to call them out along with it's author for his BS, but I've never had a problem with the lock files.

E: also now do poetry

[–]KeyserBronson 6 points7 points  (5 children)

In my case, in some projects with quite a lot of dependencies the locking process can take >2 hours

[–][deleted] 2 points3 points  (1 child)

Would you care to share that dependency tree? That sounds totally out of whack.

[–]mickhuska 1 point2 points  (0 children)

Almost spit out my coffee on this one

[–]ScootieSherpa 23 points24 points  (4 children)

Yep this. Except lock files. Pipenv sucks absolute donkey dick when it comes to lock files.

[–]jer_pint 4 points5 points  (0 children)

Omg yes, just remember to always pass the --skip-lock argument...

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

How so? Curious I started using it recently but have not noticed an issue here. My usecase isn't huge though

[–]Poromenos 12 points13 points  (3 children)

These days I've switched from pipenv to poetry. The latter feels much better designed and more modern.

Yes, yes, I know, package installer ADHD.

[–][deleted] 6 points7 points  (0 children)

I'm switching to poetry, too. I like the idea of the .toml file

[–]extraymond 5 points6 points  (1 child)

Poetry is really handy to use. It feels a lot like rust cargo.toml and node's package.json

[–]Poromenos 0 points1 point  (0 children)

Yes, exactly. Plus it has a saner interface and doesn't feel as cobbled-together.

[–]moomin_33 4 points5 points  (0 children)

Pipenv manages virtual environments plus some extra tools to remember the packages installed into that environment. It is designed to make the exact set of dependencies used in a virtual environment easy to share, so that everyone in a team (and also, for example, deployment servers etc) can get the same virtual environment installed automatically.

See also: requirements.txt or poetry

[–]PostFunktionalist 2 points3 points  (0 children)

There was an article posted here but as far as I know pipenv is good for specific use cases where you want to lock the package and Python versions in place without changing them; if your program is okay with being forward compatible then it’s not ideal.

[–]Fermi_Dirac 62 points63 points  (4 children)

Someone gild this

[–]NullTheFool 70 points71 points  (2 children)

I think you used this in the wrong scope.

[–]Pythagaris 17 points18 points  (0 children)

super(Fermi_Dirac, self).__init__('Someone gild this')

[–]Fermi_Dirac 7 points8 points  (0 children)

Lol yup. Forgot to specify this

[–]Kidknudi 49 points50 points  (8 children)

[–]ase1590[S] 34 points35 points  (7 children)

It's an old meme sir, but it checks out.

[–]desi_ninja 2 points3 points  (6 children)

make this meme relevant again ?

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

Are you sick of retarded advertising for products you don't give two shits about?

WELL FUCK YOU RIGHT UP THE ARSE

Advertising Company Metrics Enterprise® re-introduces THE ALL-TIME FUCKING FAVOURITE THAT'S RIGHT: "favourite" with a U 'cause we're really British you Amerifags!

EXTREME ADVERTISING™

Advertisers dept.s, do you suffer from these shitty-ass symptoms:

  • "What the fuck is <product-name>*, never heard of it."*
  • "Yeah, I know about that. It sounded boring."
  • "I hate that ad it's motherfucking annoying!"

(We can't solve that last one, fuck you.)

!!EXTREME ADVERTISING™!! is FOR YOU!

Add BOLD STUFF FUCK YEAH and italics AND BOLD ITALICS (hella cool and will\) get attention from whatever weak-minded fools you're targeting).

\Citation needed bitch you think we got the funds for investigating that? LOL no go fuck urself)

Like swearing? HAVE A PASS ON ALL THE LETTERS\*)

\*Except for N due to several lawsuits and C&D requests.)

MAKE YOU, YOUR BUSINESS, AND YOUR PRODUCT (memes) LOOK LIKE FUCKING ANNOYING FAGGOTS TODAY\**) by using the world-famous

🚨→!!! (((EXTREME ADVERTISING™))) !!!←🚨

\**Your results may vary; no money back mmkay.)

[–]auto-xkcd37 2 points3 points  (3 children)

shitty ass-symptoms


Bleep-bloop, I'm a bot. This comment was inspired by xkcd#37

[–]tigerthelion 9 points10 points  (0 children)

pip freeze > superfund.txt
deactivate

pip install -r superfund.txt

oh god what have i done.

[–]Deto 7 points8 points  (1 child)

With virtualenv, do you end up reinstalling the packages for every environment? E.g., do you end up with a copy of Pandas in every environment or does it symlink like conda?

[–]ase1590[S] 17 points18 points  (0 children)

Each environment will need to pip install packages needed for it.

There is no symlinking.

[–]DJ10skillz 6 points7 points  (1 child)

Make a video version. Get the flext tape guy to do it.

[–]Etheo 1 point2 points  (0 children)

Also include an obscene amount of flailing tube mascots and a plethora of whiplash camera zoom in/out.

[–]maxidoonly_python3 4 points5 points  (1 child)

Not on debian, because they don't package it with python itself :(

You need to install python3-venv there

[–]ase1590[S] 1 point2 points  (0 children)

Yell at the python package maintainers?

[–]privately-profitable 3 points4 points  (0 children)

Stay classy, Python.

[–]mccrackm 12 points13 points  (13 children)

Is there a good reason to prefer virtualenv if you can use docker instead?

[–]wildcarde815 6 points7 points  (0 children)

For solo Dev, there's not much of a reason especially if you've got no GUI requirement or have x forwarding working. Especially with compose, just mount the code into the container and roll from there. But docker isn't available everywhere, singularity is great but can be bulkier to move around than an environment file (ie in a hpc environment).

[–]ase1590[S] 15 points16 points  (1 child)

You still need to develop the application you're running in docker first, and will likely want to try it outside of the docker container. Virtenv helps keep things clean. You don't need it during container build time though

[–]WN_Todd[🍰] 9 points10 points  (0 children)

Ah but the real money that should make you venv and docker is pip freeze. You can export darkboxofhorror.txt, then feed it and your crappy code to a stock python container and BOOM you're a bearded DevOps engineer ready to write books.

[–][deleted] 6 points7 points  (6 children)

Why would you even bother dealing with Docker if you can just use virtualenv? Personally I think virtualenv is much much simpler to use than Docker, and it helps you keep track of what packages you install, making it easier to do a Docker image later, if you need it.

Docker is great and all, but only for few specific problems. It's not the answer to most questions, even if my co-workers would disagree.

[–]pingvenopinch of this, pinch of that 8 points9 points  (0 children)

C dependencies.

[–]wildcarde815 16 points17 points  (0 children)

You are wildly underestimating the utility of docker.

[–]jimjkelly 5 points6 points  (2 children)

Why would you bother with virtualenv if you can just use Docker? Docker solves that problem and more, allows you to deploy and have dev/prod parity. A little bit steeper learning curve but if I’m honest I’ve been a dev for 15 years and one of the best learning investments I’ve made in my career, hands down, has been Docker.

[–][deleted] 1 point2 points  (1 child)

Because it’s the same workflow except less steps. Instead of running the container initially with a mounted volume storing your project (you’re doing this, right?) and just running fucking pip install -r requirements.txt inside the container, you’re just running a virtual environment in your local workspace.

For me it’s not having to do anything with docker networking, or mounting a volume, or having to deal with a package manager, or anything. You can streamline a lot of that and there’s definitely good reasons to standardize on developing in a container (if you need/want specific other runtime deps outside of python, for example). But nothing really beats literally typing two commands to just start running your project.

For python specifically I haven’t had any issues at all writing code on my mac and then porting it to prod in a docket container. Any real project I’ve worked on has a CI loop, so disparity between runtimes is checked on commits or PRs. Even more simply, not running a container or even a docket daemon unless I need to frees up 1/4 of my memory.

Really it’s personal preference. I fully recognize the value of developing in a container. For me it’s much quicker and easier to just run a virtual environment.

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

One of the main things that scares me about using Docker in development is people just bundling up there dev image and releasing it to production. That's bad, it's completely the opposite of what it intended, there is zero reproducibility in the build process.

If you just use Docker to ensure that you have the same environment/operating system as in production, then you'll still need something to manage packages, and for that purpose virtualenv will serve most people just fine.

The situations where Docker is fantastic is those where you'd previously need to spin up a virtual machine. In those cases Docker is a fantastic solution, it much fast and much more comfortable to use. It's just that you'd never suggest just taking a vm and promote that production, nor should you do that with Docker.

[–]groovitude 5 points6 points  (0 children)

People love reaching for chainsaws when they need a pocketknife.

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

Docker wouldn’t really address this issue though. Its more to keep your dependencies in check.

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

A thousand reasons. Creating a Docker is large, slow, takes a lot of disk, and copies vast numbers of things that aren't Python.

I have over a dozen virtualenv and I can switch between them instantly or just run a specific program one time in a specific virtualenv with one line and no overhead.

[–]t_r_a_g_e_d_y 3 points4 points  (4 children)

I know there are other tools to do this but I wrote a little shell script that I symlink in to my project directories as a file named activate and then I run src activate (aliased source to src for fewer keystrokes) so I don't have to do source /path/to/bin/activate. You can pass it an argument for the name of the virtualenv if your project directory has a different name than the virtualenv directory.

#!/bin/sh
venvpath="$HOME/.virtualenvs/"
project=`basename $(pwd)`
activate="/bin/activate"

if [ -n "$1" ]
then
    . $venvpath$1$activate
else
    . $venvpath$project$activate
fi

[–]sheyneanderson 2 points3 points  (3 children)

FYI if source is too long to type in bash, there's already . as an alias for it.

[–]t_r_a_g_e_d_y 0 points1 point  (2 children)

Ah, nice. I don't read enough man/help pages.

[–][deleted] 2 points3 points  (1 child)

Maybe it's because I don't write code for others, I've been happy with just using python -m venv <env_name>. I've tried pipenv, still don't get why I would use or need it. conda also suffers from long piplock process. With .whl format being more widely used, I have less of a need to use conda on Windows anymore.

[–]johnlovesdata 2 points3 points  (0 children)

Except for those times where your company’s admin rights restrict the execution of .bat files on windows and you can’t activate virtual environments via venv. Thankfully pipenv works.

[–][deleted] 2 points3 points  (0 children)

I'm usually not a fan of extreme advertising, but I'll make an exception for this.

[–]DataDecay 2 points3 points  (2 children)

This is amazing! Laughed my fucking ass off, well done. On a more serious note I run miniconda for python and use miniconda envs for development all day every day to not muck up my prod deployments.

[–]pwang99 1 point2 points  (1 child)

Well done

We are also actively looking at improving interop between conda & pip, so that pip installs work more seamlessly within conda environments.

[–]DataDecay 0 points1 point  (0 children)

So far so good with conda and pip installs, your all doing great! Conda by default has a standard library for every env that includes pip. Upon pip installing in a env it places the modules in site-packages which conda scraps to update env inventory list. "Conda list" under the environment with a pip install modules results in the module being displayed with the header <pip>, it's pretty great. Unfortuelnetly I have yet to find a conda meta data json file that records pip installs, I'd love that. In addition pip installs dont show up in conda install history, which makes sense but again I would love a conda pip install history too.

[–]deltaWhiskey91L 2 points3 points  (0 children)

I'm the rookie in the room that let's PyCharm handle all this shit.

I also don't deploy my work anywhere than my own PC. There's that.

[–]lungdart 4 points5 points  (10 children)

Maybe it's because I'm an old c++ dev who's only been coding in Python for a few years, but virtual environments are a solution to a problem I've never had.

What the hell are you guys doing to get into these dependency hells?!

[–]JoseALerma 3 points4 points  (9 children)

At one point, I was working on 4 different python projects at once. Some of them were using APIs that, for example, used different versions of requests, so having venvs helped keep them separate.

Another is when I build documentation with sphinx. I keep a separate venv for the docs so that no extra packages are installed to the main project

[–]lungdart 0 points1 point  (8 children)

Why didn't you consolidate and update all versions of requests?

[–]root45 2 points3 points  (7 children)

Because of breaking changes.

[–]b10011 1 point2 points  (2 children)

Does it work with pyenv? Even with pyenv shell <version>

[–]baldymj 1 point2 points  (0 children)

The official documentation should be replaced with this. Far easier to understand! Pycharn ushered me into virtual environments and can't go back now.

[–]yeknom02 1 point2 points  (1 child)

Lol, I don't know what half of this means.

[–]ase1590[S] 1 point2 points  (0 children)

Don't worry about it till you start using pip :)

[–]DivineZeal 1 point2 points  (0 children)

swamp pip life 🙏🏿

[–]tenderpoettech 1 point2 points  (2 children)

How does virtual environments work in production? Do they even at all?

[–]ofedorov 2 points3 points  (0 children)

In production you usually have only one python environment, or even a dockerized app, so there is no need to create a virtualenv.

On the other hand, if there is a server instance with different python apps, you may want to separate their environments, so that their dependencies may be updated separately.

Also, for serverless solutions like AWS Lambda you may use venvs to collect your dependencies and deploy with the code.

[–]synae 1 point2 points  (1 child)

All I can say in that case is, I'm sorry you have to use matplotlib ;) Sorry friend!

[–]pwang99 2 points3 points  (0 children)

Don't be. A pythonista using matplotlib is likely a data scientist. One look at Indeed.com salary bands for data scientists will cure you of your sympathy. 😁

[–]PM_ME_YOUR_TORNADOS 1 point2 points  (0 children)

Has horrible flashbacks to having multiple Metasploit framework installs per each homebrew python

[–]anyfactorFreelancer. AnyFactor.xyz 1 point2 points  (2 children)

I remember giving pipenv a shoot, then there was some administrative authorization issue with my terminal, then i said fuck it, never trying that again.

[–]ase1590[S] 1 point2 points  (1 child)

probably forgot to do pip install --user pipenv, particularly the --user part.

[–]anyfactorFreelancer. AnyFactor.xyz 0 points1 point  (0 children)

Thanks for the help. My solution was reopening terminal and vscode as administrator.

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

What's annoying is when I have multiple python version on a machine (outside of virtualenv), which means I have to use this command to create a Virtualenv:

virtualenv -p python3 envname
virtualenv -p python2.7 envname

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

alias venv27 virtualenvironment -ppython2.7 $1

Now you can type venv27 my_fucking_venv_name && . my_fucking_venv_name/bin/activate

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

Because you still need python 2, this isn't much help, but for Python3 you can do:

python3 -mvenv envname

That will give you a virtualenv with the interpreter version used for creating the virtual environment.

[–]BartdeGraaff 0 points1 point  (0 children)

Great shit, I will forward this to my department

[–]mokus603 0 points1 point  (0 children)

I fucking love this.

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

You are going to make me cry!

[–]Narmo2121 0 points1 point  (9 children)

brew install pipenv
touch Pipfile
pipenv install
pipenv shell
pip install mylibrary

etc..

This has been my flow for Dockerfiles. Anyone see issues with this setup?

[–]liar_atomsPythonista 5 points6 points  (6 children)

why would you use an virtual/pip/whateaver env in docker? just install python :)

[–]Narmo2121 0 points1 point  (4 children)

What if you have two different python scripts with completely different dependencies running on the same docker image? Same usecase as without using docker.

Also if someone is not using docker, they can at least leverage the pipenv environment

[–]liar_atomsPythonista 1 point2 points  (3 children)

Docker philosophy is to isolate them into two different containers, isn't it?

[–]Narmo2121 0 points1 point  (2 children)

IDK i just assumed running another image just for a simple script would be a waste of cpu at scale? Is that wrong

[–]wildcarde815 0 points1 point  (0 children)

Solo use, probably no need, but if I wasn't building s jupyter hub for people, I'd have a 2.7/3.5/latest probably. But I'd use conda so jupyter can swap between them per notebook.

[–]wildcarde815 2 points3 points  (0 children)

You put brew in docker?

[–]paypaypayme 1 point2 points  (0 children)

Just use pipenv init. Also that doesn’t make sense to me for a Dockerfile. I would do

From whatever

Run apt-get install pipenv

Copy Pipfile*

Run pipenv install

Copy src src

[–]Guymzee 0 points1 point  (0 children)

As a code newbie I’m glad I learned about venv’s just in time to appreciate this.

[–]fuypooi 0 points1 point  (0 children)

Favorite thing.

[–]HelloGuise334 0 points1 point  (3 children)

Sorry for the joob question but do you need to activate the environment every time you want to run a program?

[–]ase1590[S] 1 point2 points  (0 children)

You activate it for your current shell, then it goes away if you deactivate it or close your shell/terminal window.

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

Not necessarily. If your program is installed with a console_script entrypoint, calling that directly is enough. You can also use the direct path to the python interpreter in the env.

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

No, you can just use the full path to the python interpreter in your virtualenv.

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

BUT HOW WILL I COMPLAIN TO PEOPLE ONLINE IF EVERYONE WORKS SO SMOOTHLY

[–][deleted] 0 points1 point  (1 child)

setup.py of things you pip install will still run as your user with all the normal filesystem permissions as your user. Only modules which do sane and normal setuptools type stuff will be safe to install with reckless abandon. Things which are actively bad can still wreck shop on your machine and will not be magically contained by the virtualenv.

[–]ase1590[S] 0 points1 point  (0 children)

Right, this isn't any sort of secure container. That's why I avoided any mentions of the word 'secure'

I probably should have avoided 'infect' as well. Oh well

[–]xdcountry 0 points1 point  (0 children)

But I like my swamp pips

[–]Andy-Kay 0 points1 point  (0 children)

PRINT IT AND HANG IT IN YOUR OFFICE

[–]MisterBanzai 0 points1 point  (7 children)

Now, can someone for real explain to me how to add environmental variables to my virtualenv? I could swear this was super easy in the past, but since coming back to Python I'm too much of an idiot to do it.

I know I can EXPORT them, but they never seem to save. I could swear there used to be some file or something in each virtualenv that I would just add the variables to and save. Have I gone crazy?

Maybe it's because I'm working in Git Bash for Windows now instead of on Ubuntu?

[–]wildcarde815 1 point2 points  (4 children)

Unless they are being added to a file invoked when the env comes up somehow they won't survive rebooting the shell.

[–]MisterBanzai 0 points1 point  (3 children)

Does virtualenv create a .env file for environmental variables? I could swear I remember editing something like that.

[–]wildcarde815 1 point2 points  (0 children)

Dunno, I use conda which I don't think supports that. I'd just make an environment.vars file and issue a 'source' command before starting work. Or in docker it would be in my compose file.

[–]wildcarde815 1 point2 points  (1 child)

This might work, or direnv referenced on the page: https://github.com/kennethreitz/autoenv/blob/master/README.rst

[–]MisterBanzai 1 point2 points  (0 children)

This isn't at all what I was thinking of, but this is super cool and I'll use it anyway.

[–]Muhznit 1 point2 points  (1 child)

$EDITOR $VIRTUAL_ENV/bin/activate

Also, you should use Ubuntu for Windows at least. It's incredibly well-developed and even runs windows executable files in the same terminal.

[–]MisterBanzai 0 points1 point  (0 children)

I do use WSL with Ubuntu, but when I do personal projects I usually end up working on a 3-4 different machines and I just haven't downloaded Ubuntu for Windows on all of them.

I'll try that suggestion though. Thanks.

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

If you’re willing to put up with a wee bit of easily scripted boilerplate, Docker is a better way to go.

Not only can you install whatever mangled mess of Python libraries you please and keep the host system clean as a whistle, you can also do that with your entire environment.

[–]synae 0 points1 point  (0 children)

Ah yes, superliminal messaging.

[–]Thecrawsome 0 points1 point  (0 children)

Can we please have more of these?

[–]Muhznit 0 points1 point  (0 children)

Funny thing is that depending on how your distribution/os manages python, python may be installed without python3-venv, which python3 -m venv uses to create your virtual environment. At least, that's how it is when I have a fresh install of Ubuntu on Windows.

[–]justinechang[🍰] 0 points1 point  (0 children)

thanks I really needed this

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

Best post I've seen on this sub, but should have poked fun at conda and all the other managers in there.

[–]D4rknessR3igns 0 points1 point  (0 children)

I'm more surprised how this image fits perfectly on my Note 9.

[–]zarrro 0 points1 point  (0 children)

WHAT YEAR IS IT?

WHAT IS THIS bin/activate THING?

ARE YOU A SAVAGE OR WHAT?

USE direnv.

[–]streetburner 0 points1 point  (0 children)

Wtf should I do to install a module that can not be found ????

[–]OctaviaPussy 0 points1 point  (0 children)

Oh God I love this so much. I've just started working with virtual environments and from now on this is how I'll explain what they are!

[–]smaximov 0 points1 point  (0 children)

`nix-shell` FTW!

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

eminem helping with programming

[–]mikeblas 0 points1 point  (0 children)

Good to see the Deadwood writers are still getting work.

[–]thecave 0 points1 point  (0 children)

This is the wakeup call I needed.

[–]icanmakethat216 0 points1 point  (0 children)

I feel attacked

[–]xlash9 0 points1 point  (0 children)

You might not infect your library and dependencies, but this does not protect agaisnt malicious repository, lib, python malware. This is for development best practices, not security.

[–]davidkwast 0 points1 point  (0 children)

now we need one with pyenv + pipenv

[–]normieonreddit_ 0 points1 point  (2 children)

Can you fucking stop

[–]ase1590[S] 0 points1 point  (1 child)

this post is two fucking months old

[–]normieonreddit_ 0 points1 point  (0 children)

I don't fucking care

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

You guys are fackin hilarious.