Stranice

Sunday 25 January 2015

Using Python libraries in Windows


Using Python in Windows in an adventure.

I decided to record all the steps you need to run Python normally in Windows 7 x64 system. Most of the things will be (I hope) applicable to other variants of Windows system.
Using basic Python for programming is not a problem, but when you want to install some tools and packages that it's getting pretty messy. But there is a light on the end of the tunnel (I hope).

I hope you wonder why do I grunt so much. Well, I mostly experience with Java where by using Maven and Gradle I had little problem adding extra components.

So here is required steps for using extra packages in Python.

I will describe installing libraries for up to 3.4 and 3.4. The reason is that in Python 3.4 setuptools are automatically installed with the installation. By setuptools I mean pip and easy_install programs.

When you installing Python also choose to Pyton on path, so you can easily run python executable from the command line.

This step is only required for the versions up to 3.4:

Here is the official documentation for installing setup tools:
https://pypi.python.org/pypi/setuptools

After you install the setup tools you will be able to run pip and easy_install programs. A crucial program to install with pip is wheel and another very important is virtualenv. With virtualenv we can run so called virtual environment (VE). So you can have multiple versions of various python packages on the same system.

You can read more about wheel on the official documentation page:
https://wheel.readthedocs.org/en/latest/

I will install virtualenv and wheel for the start with these commands:
  • pip install virtualenv
  • pip install wheel
Now that we have these programs installed I will create a VE
  1. c:\mkdir virtualenvs
  2. c:\cd virtualenvs
  3. c:\virtualenvs\mkdir test
  4. c:\virtaulenvs\test\virtualenv --clear venvI used option --clear so that I have clean virtualenv
    (without previously installed packages)
With this four steps we created a directory where will we store all virtualenvs and a directory for our test project. In the and we created a new environment.

With following command we will activate VE.
  • c:\virtualenvs\test\  venv\scripts\activate
  • (venv) c:\virtualenvs\test
With activate virtualenv we can install packages. If you have any package that you want to have in all VEs you should install it without VE enabled and when you you are creating VE omit --clear parameter.

Now we have three variations of installing packages. 
  1. Simple pip install
  2. Installing whl file with pip
  3. installing exe file with easy install
ad 1) To install programs just type pip install as we did when installing virtualenv and wheel

One of good place to get windows precompiled library is this web page:
http://www.lfd.uci.edu/~gohlke/pythonlibs/

ad 2) When installing whl file you need to use some extra options
         (venv) c:\virtualenvs\test\ pip install --use-wheel filename.whl
         And the file will be install

ad 3) If we have some installation file only as exe e.g. psychopg then we need to use
       easy_install  command
        (venv) c:\virtualenvs\test\ easy_install psychopg-version.exe

If make a command pip list we will have a list of installed packages in our VE. And when we choose interpreter in our IDE we choose a python.exe in the following directory:
    c:\virtualenvs\test\venv\scripts\python.exe

When we want to finish our VE session we need to run deactivate command:
     c:\virtualenvs\test\  venv\scripts\deactivate

So every time we want to install some packages in our VE we need to activate first installed programs and deactivate them at the end.