Skip to content

Instantly share code, notes, and snippets.

@econchick
Last active December 21, 2015 06:28
Show Gist options
  • Save econchick/a0fbd9564434ec11a7c2 to your computer and use it in GitHub Desktop.
Save econchick/a0fbd9564434ec11a7c2 to your computer and use it in GitHub Desktop.
For Review: Installation of git, pip, virtualenv, and virtualenvwrapper for Windows.

Windows

python

  1. Go here and click “run” if given the option. Otherwise, save it to your Desktop, then minimize windows to see your desktop, and double click on it to start the installer. Follow the installer instructions to completion.
  2. Open a command prompt (we will be doing this multiple times, so make a note of how to do this!):
    • On Windows Vista or Windows 7: click on the Start menu (the Windows logo in the lower left of the screen), type cmd into the Search field directly above the Start menu button, and click on "cmd" in the search results above the Search field.
    • On Windows XP: click on the Start menu (the Windows logo in the lower left of the screen), click on "Run...", type cmd into the text box, and hit enter.
  3. At this C:\ prompt that appears, test your Python install by typing \Python27\python.exe and hitting enter. You should see something like
Python 2.7.3 (r271:86832,...) on win32
Type "help", "copyright", "credits" or "license" for more information
>>>
  1. You just started Python! The >>> indicates that you are at a new type of prompt – a Python prompt. The command prompt lets you navigate your computer and run programs, and the Python prompt lets you write and run Python code interactively.
  2. To exit the Python prompt, type exit() and press Enter. This will take you back to the Windows command prompt (the C:\ you saw earlier).
  3. Put Python on the PATH – You might have noticed that you typed a "full path" to the Python application above when launching Python (python.exe is the application, but we typed \Python27\python.exe). In this step, you will configure your computer so that you can run Python without typing the ''Python27'' directory name.
    • Get to System Properties
      1. Open up “My Computer” by clicking on the Start menu or the Windows logo in the lower-left hand corner, and navigate to "My Computer" (for Windows XP) or "Computer" (For Vista and Windows 7).
      2. Right-click on the empty space in the window, and choose “Properties”.
        • If you’re using XP: window labeled "System Properties" will pop up. Click the "Advanced" tab. A window with the title "System Properties" will appear.
        • If you’re not using XP: A window labeled “View basic information about your computer” will appear. In this window, click "Advanced system settings". A window with the title "System Properties" will appear.
    • Edit the Path
      1. Within System Properties, make sure you are in the tab labeled “Advanced’.
      2. Click the button labeled “Environment Variables”. A window labeled "Environment Variables" will appear.
      3. In this window, the screen is split between “User variables” and “System variables”. Within “System variables’, scroll down and find the one labeled “Path’. Click the “Edit...” button A window with the "Variable name" and the "Variable value" should appear. The “Variable value” will already have some text in it; click in the box to unhighlight it (we don't want to accidentally delete that text).
    • In the "Variable value" box, scroll to the end. Add the following text, and hit OK. Make sure to include the semicolon at the start! ;c:\python27\;c:\python27\scripts;c:\python27\tools\scripts
    • Hit "OK" to close out the system properties window.
    • Test your change:
      1. Open up a new command prompt: you do this the same way you did above when installing python. This needs to be a new command prompt because the changes you just made didn't take affect in prompts that were already open.
      2. Type python into the command prompt to start Python
      3. Notice that you now get a Python interpreter, indicated by the change to a >>> prompt.
      4. Exit the Python prompt by typing exit() and hitting enter. Now you're back at the Windows command prompt (C:\).

Success! You have installed Python!

git

Download git here. This installs git for Windows, as well as Msys, a Unix-like shell, that also includes a GCC compiler, MinGW.

pip, virtualenv + virtualenvwrapper

  1. You’ll first need to install setuptools, and use ez_setup.py to run it. Download ez_setup.py and run it.

  2. Once installation is complete, you will find an easy_install.exe program in your Python Scripts subdirectory. For simple invocation and best results, add this directory to your PATH environment variable, if it is not already present.

  3. Next, run easy_install pip to install pip.

  4. Open/run the Git Bash program. NOTE: Windows users: We will use this Git Bash program for whenever the “terminal”, “shell”, or “command line” is referred to.

  5. Run the following command to install virtualenv:

    $ pip install virtualenv

    or, if you get a permission error:

    $ sudo pip install virtualenv
  6. Next, run the command to install virtualenvwrapper:

    $ pip install virtualenvwrapper

    or, if you get a permission error:

    $ sudo pip install virtualenvwrapper
  7. And now setup virtualenvwrapper:

    $ export WORKON_HOME=$HOME/.virtualenvs
    $ export MSYS_HOME=/c/msys/1.0
    $ source /usr/local/bin/virtualenvwrapper.sh

    or,

    $ export WORKON_HOME=$HOME/.virtualenvs
    $ export MSYS_HOME=C:\msys\1.0
    $ source /usr/local/bin/virtualenvwrapper.sh

Test your setup

Now let’s test our installation and get familiar with creating & using virtual environments, let’s return to our terminal:

$ mkvirtualenv TestEnv
Installing
distribute..........................................
....................................................
....................................................
...............................done.
virtualenvwrapper.user_scripts Creating /Users/lynnroot/Envs/TestEnv/bin/predeactivate
virtualenvwrapper.user_scripts Creating /Users/lynnroot/Envs/TestEnv/bin/postdeactivate
virtualenvwrapper.user_scripts Creating /Users/lynnroot/Envs/TestEnv/bin/preactivate
virtualenvwrapper.user_scripts Creating /Users/lynnroot/Envs/TestEnv/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/lynnroot/Envs/TestEnv/bin/get_env_details

Now that you made a virtual environment called TestEnv, you should see (TestEnv) before your prompt:

(TestEnv) $

Let’s play around with commands for virtualenv:

# deactivate the TestEnv
(TestEnv) $ deactivate
$
# reactivate the TestEnv
$ workon TestEnv
(TestEnv) $

Next, we’ll practice installing a package into the virtualenv:

# install the Django package in your TestEnv environment
(TestEnv) $ pip install django
Downloading/unpacking django
  Downloading Django-1.1.1.tar.gz (5.6Mb): 5.6Mb downloaded
  Running setup.py egg_info for package django
Installing collected packages: django
  Running setup.py install for django
    changing mode of build/scripts-2.6/django-admin.py from 644 to 755
    changing mode of /Users/lynnroot/Envs/TestEnv/bin/django-admin.py to 755
Successfully installed django
(TestEnv) $
# test the installation of Django
(TestEnv) $ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> exit()
# deactivate the TestEnv virtual environment
(TestEnv) $ deactivate
$
# try to import Django again
# we should get an error because we deactivated the virtualenv
$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named django
>>> exit()
$
# reactivate the TestEnv virtual environment
$ workon TestEnv
(TestEnv) $
# try again to import Django
(TestEnv) $ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> exit()
(TestEnv) $
# see what libraries are installed in the TestEnv virtual environment:
(TestEnv) $ pip freeze
django==1.5
(TestEnv) $

Here’s a run-down of useful commands for pip, virtualenv & virtualenvwrapper.

  • mkvirtualenv [ENV_NAME] – creates and activates a fresh virtual environment
  • workon [ENV_NAME] – activates an already-created virtual environment
  • deactivate – deactivates the virtual environment that is currently active
  • within an activated virtualenv, pip install [PACKAGE_NAME] installs a package into the virtualenv
  • within an activated virtualenv, pip freeze lists the packages that is installed & accessible within the virtualenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment