kronosapiens.github.io - Setting up Virtual Environments in Python









Search Preview

Setting up Virtual Environments in Python

kronosapiens.github.io
There comes a time in every young developer’s life when they realize that they need to start programming like a grown-up. This is when they learn how to use ...
.io > kronosapiens.github.io

SEO audit: Content analysis

Language Error! No language localisation is found.
Title Setting up Virtual Environments in Python
Text / HTML ratio 50 %
Frame Excellent! The website does not use iFrame solutions.
Flash Excellent! The website does not have any flash contents.
Keywords cloud environment virtual environments pip >>> ƒ packages numpy install env_one python directory import virtualenv Python run create default inside called
Keywords consistency
Keyword Content Title Description Headings
environment 35
virtual 21
environments 19
pip 16
>>> 13
ƒ 12
Headings
H1 H2 H3 H4 H5 H6
3 5 0 0 0 0
Images We found 0 images on this web page.

SEO Keywords (Single)

Keyword Occurrence Density
environment 35 1.75 %
virtual 21 1.05 %
environments 19 0.95 %
pip 16 0.80 %
>>> 13 0.65 %
ƒ 12 0.60 %
packages 12 0.60 %
numpy 11 0.55 %
install 9 0.45 %
env_one 8 0.40 %
python 8 0.40 %
directory 8 0.40 %
import 8 0.40 %
virtualenv 8 0.40 %
Python 8 0.40 %
run 7 0.35 %
create 6 0.30 %
default 6 0.30 %
inside 6 0.30 %
called 6 0.30 %

SEO Keywords (Two Word)

Keyword Occurrence Density
virtual environment 16 0.80 %
environments ƒ 9 0.45 %
>>> import 8 0.40 %
the virtual 7 0.35 %
of the 6 0.30 %
env_one environments 6 0.30 %
in the 6 0.30 %
ƒ pip 6 0.30 %
look in 5 0.25 %
for more 4 0.20 %
copyright credits 4 0.20 %
credits or 4 0.20 %
or license 4 0.20 %
in that 4 0.20 %
You can 4 0.20 %
more information 4 0.20 %
information >>> 4 0.20 %
inside of 4 0.20 %
help copyright 4 0.20 %
that environment 4 0.20 %

SEO Keywords (Three Word)

Keyword Occurrence Density Possible Spam
env_one environments ƒ 6 0.30 % No
environments ƒ pip 5 0.25 % No
the virtual environment 5 0.25 % No
221505 on darwin 4 0.20 % No
on darwin Type 4 0.20 % No
information >>> import 4 0.20 % No
more information >>> 4 0.20 % No
for more information 4 0.20 % No
license for more 4 0.20 % No
or license for 4 0.20 % No
credits or license 4 0.20 % No
copyright credits or 4 0.20 % No
help copyright credits 4 0.20 % No
275 default Mar 4 0.20 % No
2014 221505 on 4 0.20 % No
9 2014 221505 4 0.20 % No
Mar 9 2014 4 0.20 % No
default Mar 9 4 0.20 % No
Python 275 default 4 0.20 % No
darwin Type help 4 0.20 % No

SEO Keywords (Four Word)

Keyword Occurrence Density Possible Spam
help copyright credits or 4 0.20 % No
darwin Type help copyright 4 0.20 % No
for more information >>> 4 0.20 % No
license for more information 4 0.20 % No
or license for more 4 0.20 % No
credits or license for 4 0.20 % No
copyright credits or license 4 0.20 % No
env_one environments ƒ pip 4 0.20 % No
Type help copyright credits 4 0.20 % No
on darwin Type help 4 0.20 % No
221505 on darwin Type 4 0.20 % No
2014 221505 on darwin 4 0.20 % No
9 2014 221505 on 4 0.20 % No
Mar 9 2014 221505 4 0.20 % No
default Mar 9 2014 4 0.20 % No
275 default Mar 9 4 0.20 % No
Python 275 default Mar 4 0.20 % No
more information >>> import 4 0.20 % No
1 in ImportError 3 0.15 % No
last File line 3 0.15 % No

Internal links in - kronosapiens.github.io

About
About
Strange Loops and Blockchains
Strange Loops and Blockchains
Trie, Merkle, Patricia: A Blockchain Story
Trie, Merkle, Patricia: A Blockchain Story
Reputation Systems: Promise and Peril
Reputation Systems: Promise and Peril
The Future of Housing, in Three Parts
The Future of Housing, in Three Parts
Proof of Work vs Proof of Stake: a Mirror of History
Proof of Work vs Proof of Stake: a Mirror of History
Introducing Talmud
Introducing Talmud
The Economics of Urban Farming
The Economics of Urban Farming
Time and Authority
Time and Authority
On Meaning in Games
On Meaning in Games
Objective Functions in Machine Learning
Objective Functions in Machine Learning
A Basic Computing Curriculum
A Basic Computing Curriculum
The Problem of Information II
The Problem of Information II
The Problem of Information
The Problem of Information
Elements of Modern Computing
Elements of Modern Computing
Blockchain as Talmud
Blockchain as Talmud
Understanding Variational Inference
Understanding Variational Inference
OpsWorks, Flask, and Chef
OpsWorks, Flask, and Chef
On Learning Some Math
On Learning Some Math
Understanding Unix Permissions
Understanding Unix Permissions
30 Feet from Michael Bloomberg
30 Feet from Michael Bloomberg
The Academy: A Machine Learning Framework
The Academy: A Machine Learning Framework
Setting up a queue service: Django, RabbitMQ, Celery on AWS
Setting up a queue service: Django, RabbitMQ, Celery on AWS
Versioning and Orthogonality in an API
Versioning and Orthogonality in an API
Designing to be Subclassed
Designing to be Subclassed
Understanding Contexts in Flask
Understanding Contexts in Flask
Setting up Unit Tests with Flask, SQLAlchemy, and Postgres
Setting up Unit Tests with Flask, SQLAlchemy, and Postgres
Understanding Package Imports in Python
Understanding Package Imports in Python
Setting up Virtual Environments in Python
Setting up Virtual Environments in Python
Creating superfunctions in Python
Creating superfunctions in Python
Some Recent Adventures
Some Recent Adventures
Sorting in pandas
Sorting in pandas
Mimicking DCI through Integration Tests
Mimicking DCI through Integration Tests
From Ruby to Python
From Ruby to Python
Self-Focus vs. Collaboration in a Programming School
Self-Focus vs. Collaboration in a Programming School
Designing Software to Influence Behavior
Designing Software to Influence Behavior
Maintaining Octopress themes as git submodules
Maintaining Octopress themes as git submodules
Setting up a test suite with FactoryGirl and Faker
Setting up a test suite with FactoryGirl and Faker
To Unit Test or not to Unit Test
To Unit Test or not to Unit Test
A Dynamic and Generally Efficient Front-End Filtering Algorithm
A Dynamic and Generally Efficient Front-End Filtering Algorithm
Trails & Ways: A Look at Rails Routing
Trails & Ways: A Look at Rails Routing
Getting Cozy with rspec_helper
Getting Cozy with rspec_helper
Exploring the ActiveRecord Metaphor
Exploring the ActiveRecord Metaphor
Civic Hacking as Inspiration
Civic Hacking as Inspiration
From Scheme to Ruby
From Scheme to Ruby
Setting up Auto-Indent in Sublime Text 2
Setting up Auto-Indent in Sublime Text 2
hello world
hello world
via RSS
Abacus

Kronosapiens.github.io Spined HTML


Setting up Virtual Environments in Python AbacusWell-nighSetting up Virtual Environments in Python Jul 22, 2014 There comes a time in every young developer’s life when they realize that they need to start programming like a grown-up. This is when they learn how to use virtual environments. A virtual environment is basically like a small clubhouse in your computer that’s a little bit isolated from everything else. Files in that clubhouse only really know well-nigh other files in that clubhouse; they couldn’t superintendency less well-nigh what’s outside, what’s outside doesn’t really know much well-nigh them. It’s a kind of isolation that is frowned upon when it occurs on University campuses, but is quite essential when it comes to writing production software. Why?Consideringthe world of lawmaking is a fickle, perpetually shifting place, with updates and changes happening constantly. You update things all the time; you’re never quite sure what’s going to change, and when. Version 1.2 might work a little bit differently from 1.2.2, and you can never be quite sure if something you’ve written for one will work for the other. Further, you’re using 1.2, but maybe your colleague is still stuck on 1.0 (sad, I know). If she pulls your lawmaking and tries to run it, it’ll throw approximately eight thousand bugs. Does that sound relaxing to you? Enter virtual environments. With a virtual environment, you pick exactly what version of what packages go inside, and it’s easy for someone else to re-create that environment on their own machine. I’m in the process of moving the ParagonMeasure backend into a virtual environment, in preparation for towers out the Django using which will bring it to life. The rest of this post will follow the process of setting up the environment. Installation & Creation First, install virtualenv, the virtual environment library: sudo pip install virtualenv Easy enough. Now, create a directory for all your environments to go. This can be separate from the files meant to be run in the environment. I created a top-level directory in my lawmaking directory tabbed environments, where I plan on keeping all of the virtual environments I create for any of my projects. code/ personal/ projects/ work/ environments/ Now, let’s unquestionably create an environment. virtualenv -p python --no-site-packages env_one You should see some output that looks like this: Running virtualenv with interpreter /usr/bin/python New python executable in env_one/bin/python Installing setuptools, pip...done. You may be wondering well-nigh those options I passed. The -p flag specifies which version of Python to use to create the environment. Odds are you’ll be fine without it – I did a somewhat wonky install of the Enthought IPython Distribution and made some sort of default, so virtualenv kept crashing until I explicitly said to use regular old python. By default, virtualenv will squint in the zippy virtual environment for a package, but will fall when on any global installs if it isn’t found. The --no-site-packages option tells virtualenv to only use the packages you install in that environment, and not to squint in the global folders for it if it isn’t found. I like this option considering it ways that I have to be explicit well-nigh every package that I’m using; I want to stave a deployment where I realize that I’ve unquestionably been relying on some obscure package I installed two years ago and completely forgot about. Alright! We now have our very own virtual environment! Let’s take a squint at what we’ve made: code/ environments/ env_one/ bin/ include/ lib/ .Python The /bin directory contains the binaries, like pip and python, which you’ll unquestionably be running when you use this environment. It moreover contains an executable python filed tabbed activate, which we’ll talk well-nigh in a second. The /include directory contains a directory tabbed python2.7/, which contains a tuft of header files (.h) which I don’t understand. lib/ contains /python2.7, which is where all the goodies live. When you install new packages into this virtual environment, they’ll end up in /lib/python2.7/site-packages. That’ll be where your applications running in this environment will squint first. Here’s what’s inside site-packages to start: site-packages/ _markerlib/ pip/ pip-1.5.6.dist-info/ setuptools/ setuptools-2.6.dist-info/ easy_install.py pkg_resources.py Pretty bare-bones, huh? Use Now that we have our environment, how do we use it? In general, we use our virtual environment by calling the binaries installed inside of it – these binaries know to squint in the virtual environment surpassing the global environment, and so anything tabbed with those binaries will be tabbed inside of the virtual environment. There are two ways of doing this. One is the wearisome way, the other is the tomfool way. The wearisome way is to explicitly state the path to the virtual env binary. Say you had somescript.py in the environments/ directory, and you wanted to run it inside of your new virtual environment. You could run it by typing the following: env_one/bin/python somescript.py This tells the shell to squint in env_one/bin for a python executable and use it to run somescript.py, instead of whatever executable would’ve been found if you had typed python somescript.py and the shell had gone romping virtually the $PATH looking for it. So that’s the wearisome way. The AWESOME way is to use the seated vivify method, as follows: source env_one/bin/activate This will do some magic and transpiration your $PATH variable to point to the virtual environment surpassing anything else. Further, you can cd virtually your hardrive and undeniability files from anywhere without “leaving” the virtual environment. You moreover get a tomfool prompt, which I think is the weightier part: (env_one)[16:25:01] environments ƒ: (the ƒ is my own flavor) You can “leave” the environment by inward the deactivate command, which will restore the $PATH variable and put you when in your global environment. Installing Packages Now, let’s icon out how to install new packages. Out of the box, a virtual environment is pretty bare-bones: [16:46:59] environments ƒ: source pm_app/bin/activate (pm_app)[16:47:07] environments ƒ: python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for increasingly information. >>> import pandas Traceback (most recent undeniability last): File "<stdin>", line 1, in <module> ImportError: No module named pandas >>> import numpy Traceback (most recent undeniability last): File "<stdin>", line 1, in <module> ImportError: No module named numpy >>> import matplotlib Traceback (most recent undeniability last): File "<stdin>", line 1, in <module> ImportError: No module named matplotlib >>> Heartbreaking. We’re going to talk well-nigh installing packages in a second, but first let’s zoom out for a sec and squint at how the $PATH is specifically waffly when we transpiration environments. Here’s python’s sys.path in my global environment: Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for increasingly information. >>> import sys >>> import pprint >>> pprint.pprint(sys.path) ['', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/Library/Python/2.7/site-packages/xlrd-0.9.3-py2.7.egg', '/Library/Python/2.7/site-packages/openpyxl-2.0.2-py2.7.egg', '/Library/Python/2.7/site-packages/jdcal-1.0-py2.7.egg', '/Library/Python/2.7/site-packages/nose-1.3.3-py2.7.egg', '/Library/Python/2.7/site-packages/pandas-0.14.0-py2.7-macosx-10.9-intel.egg', '/Library/Python/2.7/site-packages/ipdb-0.8-py2.7.egg', '/Library/Python/2.7/site-packages/ipython-2.1.0-py2.7.egg', '/Library/Python/2.7/site-packages/gnureadline-6.3.3-py2.7-macosx-10.9-intel.egg', '/Library/Python/2.7/site-packages/pyzmq-14.3.1-py2.7-macosx-10.6-intel.egg', '/Users/kronosapiens/Dropbox/Documents/Development/code/jobs/paragon/backend', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/$PYTHONPATH', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages'] >>> Here’s the same thing, inside of my virtual environment: Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for increasingly information. >>> import sys >>> import pprint >>> pprint.pprint(sys.path) ['', '/Users/kronosapiens/Dropbox/Documents/Development/code/jobs/paragon/backend', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/$PYTHONPATH', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/lib/python27.zip', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/lib/python2.7', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/lib/python2.7/plat-darwin', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/lib/python2.7/plat-mac', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/Extras/lib/python', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/lib/python2.7/lib-tk', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/lib/python2.7/lib-old', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/env_one/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/kronosapiens/Dropbox/Documents/Development/code/environments/pm_app/lib/python2.7/site-packages'] Notice how the all the entries pointing to /Library/ are totally gone, and the first entries are all pointing towards the directory containing my virtual env? That’s how the magic happens. Ok, when to installing packages. We’re using pandas and numpy pretty heavily at ParagonMeasure, so we need to get those bad boys installed ASAP. Let’s try the obvious: (env_one)[16:57:33] environments ƒ: pip install numpy Downloading/unpacking numpy Downloading numpy-1.8.1-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl (12.0MB): 12.0MB downloaded Installing placid packages: numpy Successfully installed numpy Cleaning up... (env_one)[16:58:14] environments ƒ: Well, gosh. Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for increasingly information. >>> import numpy >>> Let’s take a squint in our env_one/lib/site-packages folder and see if anything looks different. site-packages/ _markerlib/ numpy/ numpy-1.8.1.dist-info/ pip/ pip-1.5.6.dist-info/ setuptools/ setuptools-2.6.dist-info/ easy_install.py pkg_resources.py WELL HOW ABOUT THAT. Another way of looking at your packages is through the pip list command: (env_one)[17:22:45] environments ƒ: pip list numpy (1.8.1) pip (1.5.6) setuptools (3.6) wsgiref (0.1.2) Duplicating your environment So, you’ve gotten your virtual environment set up just the way you like it. All the versions are right, your tests are passing, the birds are chirping. How do you create this environment somewhere else? Via a requirements file. You can create one by inward the following: (env_one)[17:22:45] environments ƒ: pip freeze > requirements.txt` This will write a text file containing all of the packages and versions installed in that environment, in a special format that pip can re-interpret later. It’ll squint something like this: (env_one)[17:29:17] environments ƒ: pip freeze numpy==1.8.1 wsgiref==0.1.2 You can recreate that environment elsewhere by inward the pursuit (assuming you’ve zingy that other environment): (env_two)[17:22:45] environments ƒ: pip install -r requirements.txt You should see pip go superiority and start installing any missing packages. You can edit requirements.txt directly to add packages or transpiration version numbers. Just know that If you delete a package from requirements.txt, virtualenv won’t uninstall it when you run pip install -r requirements.txt. There you basically have it. Virtual environments are kind of like the magical womanhood of programming, where you can exert basically total tenancy over your world (and make other people’s lives easier to boot). See the full documentation over at the official site. Shout out to Jamie Matthews’ spanking-new post tent much of the same ground (which helped me quite a bit). Postscript The ultimate test. I’ve created an environment tabbed pm_app and have installed only the packages that the ParagonMeasure backend should require. I’m well-nigh to cd when into the main repository and run the test suite… (pm_app)[18:06:23] (master*) backend ƒ: pip list backports.ssl-match-hostname (3.4.0.2) certifi (14.05.14) matplotlib (1.3.1) nose (1.3.3) numpy (1.8.1) pandas (0.14.1) pip (1.5.6) pyparsing (2.0.2) python-dateutil (2.2) pytz (2014.4) setuptools (3.6) six (1.7.3) tornado (4.0) wsgiref (0.1.2) (pm_app)[18:07:27] (master*) backend ƒ: py.test ============test session starts ============ platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2 placid 77 items logs/test id_outliers.txt . logs/test_outliers.txt . logs/test_subject_outliers.txt . tests/test_analyzer.py ............................ tests/test_device.py .... tests/test_parser.py .... tests/test_session_parser.py ............. tests/test_subject.py ....................... tests/test_visualizer.py .. ============ 77 passed in 45.14 seconds ============ YES! Comments Please enable JavaScript to view the comments powered by Disqus. Abacus Abacus kronovet@gmail.com kronosapiens kronosapiens I'm Daniel Kronovet, a data scientist living in Tel Aviv.