# Installing the Python Dynet module

(for instructions on installing on a computer with GPU, see below)

Python bindings to DyNet are supported for both Python 2.x and 3.x.

## TL;DR

(see below for the details)

## Detailed Instructions

First, get DyNet:

Then get Eigen:

(-r NUM specifies a known working revision of Eigen. You can remove this in order to get the bleeding edge Eigen, with the risk of some compile breaks, and the possible benefit of added optimizations.)

We also need to make sure the cython module is installed. (you can replace pip with your favorite package manager, such as conda, or install within a virtual environment)

To simplify the following steps, we can set a bash variable to hold where we have saved the main directories of DyNet and Eigen. In case you have gotten DyNet and Eigen differently from the instructions above and saved them in different location(s), these variables will be helpful:

Compile DyNet.

This is pretty much the same process as compiling DyNet, with the addition of the -DPYTHON= flag, pointing to the location of your Python interpreter.

If Boost is installed in a non-standard location, you should add the corresponding flags to the cmake commandline, see the DyNet installation instructions page.

Assuming that the cmake command found all the needed libraries and didn’t fail, the make command will take a while, and compile DyNet as well as the Python bindings. You can change make -j 2 to a higher number, depending on the available cores you want to use while compiling.

You now have a working Python binding inside of build/dynet. To verify this is working:

then, within Python:

In order to install the module so that it is accessible from everywhere in the system, run the following:

The --user switch will install the module in your local site-packages, and works without root privileges. To install the module to the system site-packages (for all users), or to the current virtualenv (if you are on one), run python setup.py installwithout this switch.

You should now have a working python binding (the dynet module).

Note however that the installation relies on the compiled DyNet library being in $PATH_TO_DYNET/build/dynet, so make sure not to move it from there. Now, check that everything works: Alternatively, if the following script works for you, then your installation is likely to be working: If it doesn’t work and you get an error similar to the following: then you may need to run the following (and add it to your shell init files): export DYLD_LIBRARY_PATH=/path/to/dynet/build/dynet/:$DYLD_LIBRARY_PATH

## Usage

There are two ways to import the dynet module :

imports dynet and automatically initializes the global dynet parameters with the command line arguments (see the documentation). The amount of memory allocated, GPU/CPU usage is fixed from there on.

Imports dynet for CPU (resp. GPU) and doesn’t initialize the global parameters. These must be initialized manually before using dynet, using one of the following :

## Anaconda Support

Anaconda is a popular package management system for Python. DyNet can be used from within an Anaconda environment, but be sure to activate the environment

source activate my_environment_name

then install some necessary packages as follows:

conda install gcc cmake boost cython

After this, the build process should be the same as normal.

Note that on some conda environments, people have reported build errors related to the interaction between the icu and boost packages. If you encounter this, try the solution in this comment.

## Windows Support

You can also use Python on Windows by following similar steps to the above. For simplicity, we recommend using a Python distribution that already has Cython installed. The following has been tested to work:

1. Install WinPython 2.7.10 (comes with Cython already installed).
2. Run CMake as above with -DPYTHON=/path/to/your/python.exe.
3. Open a command prompt and set VS90COMNTOOLS to the path to your Visual Studio “Common7/Tools” directory. One easy way to do this is a command such as:
1. Open dynet.sln from this command prompt and build the “Release” version of the solution.
2. Follow the rest of the instructions above for testing the build and installing it for other users

Note, currently only the Release version works.

## GPU/MKL Support

### Installing/running on GPU

For installing on a computer with GPU, first install CUDA. The following instructions assume CUDA is installed.

The installation process is pretty much the same, while adding the -DBACKEND=cuda flag to the cmake stage:

(if CUDA is installed in a non-standard location and cmake cannot find it, you can specify also -DCUDA_TOOLKIT_ROOT_DIR=/path/to/cuda.)

Now, build the Python modules (as above, we assume Cython is installed):

After running make -j 2, you should have the files _dynet.so and _gdynet.so in the build/python folder.

As before, cd build/python followed by python setup.py install --user will install the module.

In order to use the GPU support, you can either:

• Use import _gdynet as dy instead of import dynet as dy
• Or, (preferred), import dynet as usual, but use the commandline switch --dynet-gpu or the GPU switches detailed herewhen invoking the program. This option lets the same code work with either the GPU or the CPU version depending on how it is invoked.

### Running with MKL

If you’ve built DyNet to use MKL (using -DMKL or -DMKL_ROOT), Python sometimes has difficulty finding the MKL shared libraries. You can try setting LD_LIBRARY_PATH to point to your MKL library directory. If that doesn’t work, try setting the following environment variable (supposing, for example, your MKL libraries are located at /opt/intel/mkl/lib/intel64):