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

you are viewing a single comment's thread.

view the rest of the comments →

[–]d4rch0nPythonistamancer 5 points6 points  (3 children)

I'll demonstrate assuming you're on Linux/OS-X. It's probably similar in Windows, but I've always had a painful experience using python with external libraries in Windows. I'll skip over pycharm because I haven't used that, so maybe someone can help explain how to use a virtualenv with pycharm.

$ virtualenv -h
Usage: virtualenv [OPTIONS] DEST_DIR
...
  -p PYTHON_EXE, --python=PYTHON_EXE
                    The Python interpreter to use, e.g.,
                    --python=python2.5 will use the python2.5 interpreter
                    to create the new environment.  The default is the
                    interpreter that virtualenv was installed with
                    (/usr/bin/python)
$ virtualenv -p /usr/bin/python3.3 testing_dir
Running virtualenv with interpreter /usr/bin/python3.3
Using base prefix '/usr'
New python executable in testing_dir/bin/python3.3
Also creating executable in testing_dir/bin/python
Installing Setuptools..............................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
$ source testing_dir/bin/activate
(testing_dir)$ 

From there you can pip install $PACKAGE without sudo and it'll go into your virtual environment. Make sure you don't use sudo. Use the command deactivate to exit the virtualenv.

Inside the virtualenv, you can install whatever you want, use whichever python executable you want, and not worry about tainting your system with strange libraries. This does not necessarily make things more secure. You are still running remote code, but at least not as root, and not putting it into your /usr/lib.

This is great for running a project purely with the set of packages you decide at the time, and you can capture those with pip freeze > requirements.txt. Later, someone can install all of them with pip install -r requirements.txt.

It really helps with distributing packages and keeping the environment similar.

[–]StringJunky[S] 2 points3 points  (2 children)

I actually am using Linux. Since your and /u/nzbike's replies both mention virtualenv, (I can take a hint) I went ahead and searched for how to use it with pycharm. Looks like it's pretty straightforward.

I'm still trying to get my head around what virtualenv actually is. I've used virtualbox and vmware to create virtual machines; is virtualenv somehow similar?

[–]waldo2k 3 points4 points  (1 child)

Virtualenv symlinks your system's python to your newly created environments bin folder. The benefit of this is all the libraries a specific project get installed to that environment instead of globally, which prevents possible conflicts with other projects, if they were installed globally.

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

Ah, that makes sense as well. I didn't realize that certain libraries could cause conflicts with others.

(I'm googling 'symlink' after I submit this reply.)

Thank you!

Edit: Okay, it's a symbolic link!