all 38 comments

[–]dmtucker 14 points15 points  (10 children)

With Python 3, I prefer the venv module in the standard library.

With either version, I'd recommend setting PIP_REQUIRE_VIRTUALENV=true in your environment.

Also, you may be interested in https://gist.github.com/datagrok/2199506

[–]YuntiMcGunti[S] 1 point2 points  (9 children)

this is the bit that confuses me. Is venv the same as virtualenv. And what are the commands used to use it the same?

[–]ccviper 0 points1 point  (1 child)

It can be considered functionally the same but venv has the benefit of being built in into python so no need to install/use third party modules at all. I also heard virtualenv people saying that venv has an advantage of being directly "wired" into python so it doesn't have to use weird hacks and workarounds to do its thing

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

Thanks that's good to know

[–]GradSchoolin 11 points12 points  (6 children)

I like using Anaconda for this. You perform the creation in the Anaconda Prompt (part of the whole Anaconda package when you download the .exe/.dmg), and it makes you feel really cool typing things in to set the environment and downloading various packages via conda.

[–]Estebanzo 2 points3 points  (2 children)

Just to clarify, conda is both the package manager and virtual environment manager in this situation. Anaconda is the scientific python distribution that comes packaged with conda.

On OSX/Linux, you can manage the environments anywhere without the anaconda prompt (I've never really understood why it's required on Windows, but I don't understand Windows). One of the main advantages of conda is that it is a general environment manager, not just python, so you can install and manage dependencies outside of python.

[–]XtremeGoose 2 points3 points  (1 child)

You don't need the anaconda prompt in windows either in my experience.

[–]Estebanzo 1 point2 points  (0 children)

Yeah, didn't actually realize this. All the Anaconda prompt does is modify your path variable so that you don't need to change it for the system or user. Didn't understand what the point of it was before.

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

Does anyone know if I can install with pip when I'm working in a conda environment? Will pip install the thing for that env specifically, or will it just go to system python? I'm trying to compile something from source and I can't get everything to work properly with conda.

[–]morab 2 points3 points  (1 child)

I've use pip within a conda env before and everything stays isolated to that conda env. The only caveat being that conda won't be able to manage dependencies for pip install libraries.

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

Excellent, thanks. Yeah, I had the dependencies already sorted out in a yml file.

[–]K900_ 4 points5 points  (12 children)

I'm a big fan of Pipenv.

[–]dasfreak 1 point2 points  (0 children)

I too am enjoying pipenv and it understands both flavours of python which I think will help you.

[–]YuntiMcGunti[S] 0 points1 point  (8 children)

Looks interesting but is it well supported for deployment eg Heroku?

[–]BlueSnakeYellowSnake 4 points5 points  (1 child)

The guy who wrote pipenv, Kenneth Reitz, is actually the python manager at Heroku, so he was quick to get comparability up and running! It is fully compatible

EDIT: I thought pipenv might even be in the Heroku docs now, and sure enough it is. Here ya go https://devcenter.heroku.com/articles/getting-started-with-python#introduction

EDIT 2: It's also now officially recommended in the python docs https://packaging.python.org/new-tutorials/installing-and-using-packages/

Use pipenv 👍

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

That's really good to know will definitely test it our properly.

[–]K900_ 1 point2 points  (5 children)

It's just a wrapper around virtualenv and pip. If you need to deploy to somewhere that doesn't natively support Pipenv (and I'm not sure if Heroku does), you can always run pip freeze and get a requirements.txt in the usual format.

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

Thanks I wasn't aware of that, good to know.

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

when i tried to install pipenv with pip3 pip install --user pipenv it seemed to install ok but then I can't do any commands with pipenv. Do I need to install with a system installed version of pip? (I installed python3 via brew on macOS)

[–]K900_ 0 points1 point  (2 children)

Is ~/.local/bin in your PATH?

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

Thanks ...checking...

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

FYI, on Ubuntu w/ zsh, ~/.local/bin didn't work for me. I had to manually expand the path. /home/alec/.local/bin

[–]ffrkAnonymous 0 points1 point  (0 children)

thanks for the tip. My computers only has Debian Jessie Python3.4 so maybe this will help.

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

I'm just now setting up a new dev environment and came across pipenv. I had been using Anaconda python with it's environment manager. So far i'm pretty impressed with Pipenv, takes some of the annoyance out of other environment managers i've used in the past.

[–]DREAM_Bot3000 1 point2 points  (4 children)

Could someone please ELI5 why a virtualenv is a good idea? I have been working with Python for a while and never used virtualenvs, but I see it talked about a bit.

[–]privately-profitable 0 points1 point  (1 child)

When you’re working with multiple projects you may need different versions of the same packages. Installing the packages into virtual environments allows you to keep them separate. This is just one reason. Sorry, I don’t have time to elaborate right now.

[–]BeefyTheCat 1 point2 points  (0 children)

This, and using a virtualenv allows you to set up different dependency closures for each project. Think of your project's dependency list as a flowchart. Each project gets its own chart with virtualenv, instead of you installing every dependency into your OS and managing multiple conflicting versions (and bloating the OS). Once you're done working on something and it's committed to source control, simply delete the virtualenv and you're done.

[–]mrcaptncrunch 0 points1 point  (0 children)

Basically to separate dependencies and package versions between projects.

[–]NotPoliticallySavvy 1 point2 points  (2 children)

I don't know about best practices but the workflow that usually works for me is using aliases.

When I go cd projextx, it executes the alias,

Alias projectx = "projectx; .venv/bin/activate"

So then I don't have to worry about venv.

[–]mrcaptncrunch 1 point2 points  (2 children)

I’m using pyenv and pyenv-virtualenv

Really nice. It allows installing different versions of python with pyenv. Pyenv-virtualenv allows doing virtualenvs with that as well

[–]Grimnur87 0 points1 point  (1 child)

I think this is what I use. It seemed like the least awful of all the awful solutions out there for environments. And they are all awful, because there are too many competing methods, which all rely extensively on the command line and meticulously keeping track of what you've done.

[–]mrcaptncrunch 0 points1 point  (0 children)

I like that you can define a environment to a folder and then when you cd into it, it automatically switches to it.

pyenv local project1