r/Python • • Apr 30 '18

xkcd: Python Environment

Post image
2.4k Upvotes

388 comments sorted by

View all comments

80

u/solostman Apr 30 '18

As somebody who struggled with Python installations when trying to learn Python (as a primary R user) and having to use both 2.7, 3.6, virtual environments, and an IDE... I'm so glad to see that it's not just me.

I still don't fully grasp where my python packages are when I install them by command line or PyCharm.

30

u/2freevl2frank Apr 30 '18

Why not install a virtualenv for every one of your projects however small it is?You don't even have to do it through command line. Pycharm does it for you.

13

u/solostman Apr 30 '18

Sounds nice. Do you have a resource that can walk me through that in Pycharm?

I was using scrapy which required a virtualenv in terminal and (it worked but) it always felt like a black box of what was happening to me.

21

u/leom4862 Apr 30 '18

This is my workflow for every project:

mkdir myproj                       # create new project dir.
cd myproj                          # change into your project dir.
pipenv --python 3.6                # create new clean virtual env.
pipenv install foo                 # install third party packages into your venv.

# write your code          

pipenv run python myfile.py        # Run your code inside the venv.

I don't think it can get much simpler than this.

7

u/exoendo May 01 '18

isn't it annoying to always have to install 3rd party packages every time you start a project? why not just use your system install?

12

u/jcampbelly May 01 '18

No, it's actually very reassuring knowing nothing is going to mess with my install and I can rely on a consistent environment.

When you use the distribution (system) python, you're always stuck on some dreadfully old version and may not be able to start using new things when they come out. In a virtualenv, I'm not even phased by installing and compiling a stable branch or a release candidate. If you tried that with a system install, you can end up breaking your system, as the system install serves the SYSTEM not your projects. Breaking yum or apt is NOT fun.

3

u/leom4862 May 01 '18

To prevent dependency conflicts, for example if project A relies on django version X and project B relies on django version Y. Or if proejct A relies on Python3.4 and project B relies on Python3.6.

→ More replies (1)
→ More replies (2)

2

u/internet_badass_here May 01 '18

Dude... thank you! I'm going to start doing this.

→ More replies (16)

10

u/2freevl2frank Apr 30 '18

You don't need any. It's as simple as checking a box when you creating a new project. https://imgur.com/lk7Qnli

For existing projects you can go to settings>Project Intepreter and add a new environment.

If you wanna do it through command line you can make a venv within the project folder by virtualenv/venv/pipenv as

python2 -m virtualenv ./venv

This makes an isolated env (a copy of python and default packages) in the folder ./venv. Now activate the venv as :

source ./venv/bin/activate

When activated all packages installed through pip are installed within the venv (doesnt affect global python environment) unless you use sudo.

20

u/leom4862 Apr 30 '18

python2 seriously?

3

u/robot_wrangler Apr 30 '18

Then what do you do when you try to use parts of two different projects of your own in a third?

4

u/leom4862 May 01 '18

You usually would make shared libraries from the "parts" and host them on pypi or your private package registry. You then install the libs in your "third" project via pipenv, pip or what ever tool you use to install packages in a virtualenv.

→ More replies (2)
→ More replies (2)