# Building NumPy for Cinema 4D on Windows

I’ve recently needed to compile NumPy for C4D – again. As receipt for myself in the future, here’s a list of the steps I did to compile NumPy:

## Step-by-step

1. Copy the Python.win64.framework directory and place it somewhere convenient. You can find this folder in C4DAPPDIR/resource/modules/python/res. Open a terminal (WIN+R, type “cmd” and Enter), then change directory into the folder that you just copied, for example:

> cd Desktop\Python.win64.framework

2. Install Pip by downloading the get-pip.py script to the Python.win64.framework and run it. We don’t actually need Pip, but it will install setuptools for us.

> .\python.exe get-pip.py

3. Install Visual Studio. First, check the MSVC version that Python was compiled with. You need the same version. Compare the output with the table below to figure what you need to be looking for.

> .\python -c "import sys; print(sys.version)"
2.7.9 (default, Apr 20 2015, 08:46:13) [MSC v.1700 64 bit (AMD64)]

Name _MSC_VER Shortcode Version
Visual Studio 2017 1910 v141 MSVC++ 14.1
Visual Studio 2015 1900 v140 MSVC++ 14.0
Visual Studio 2013 1800 v120 MSVC++ 12.0
Visual Studio 2012 1700 v110 MSVC++ 11.0
Visual Studio 2010 1600 v100 MSVC++ 10.0
Visual Studio 2008 1500 v90 MSVC++ 9.0
Visual Studio 2005 1400 v80 MSVC++ 8.0

source: https://en.wikipedia.org/wiki/Visual_C%2B%2B#Common_MSVC_version

Unfortunately, Microsoft makes it a pain to find older Visual Studio installers. Happy hunting! – https://www.visualstudio.com/

4. Activate the build environment by running the vcvarsall.bat of the Visual Studio version that you just installed. You also need to specify the target architecture x86_amd64. For the running example:

> "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" x86_amd64

5. Compile NumPy – you can download it from GitHub. Extract it, change directory to the extracted directory and run the build. Make sure that you use the python.exe from the previously copied Python.win64.framework directory:

> cd numpy-1.13.3
> ..\Python.win64.framework\python.exe setup.py build
> ..\Python.win64.framework\python.exe setup.py bdist_egg


NOTE: Many Python C-extensions require other libraries to compile (NumPy is one that compiles out of the box). You will need to download precompiled versions of these libraries matching the same Visual Studio verson or compile them yourself before you will be able to compile the C-extension.

6. Done. You can find the final Python egg in build/numpy-1.13.3-py2.7-win-amd64.egg. Python eggs are just ZIP files that you can import from when they are in the sys.path. You can also just unpack the contents of the egg into the Cinema 4D Python packages directory under C4DPREFSDIR/library/python/packages/win64.

## Troubleshooting

• Cannot open include file: ‘Python.h’: No such file or directory

The Python.win64.framework folder in Cinema 4D – sometimes – seems to be structured slightly different from what setuptools expects. I couldn’t make out a definite pattern yet, that’s why I did not include this infromation in the main steps.

If you get this error, check the Python.win64.framework\include\ directory. If it contains a python27\ subdirectory, move all the files from that directory to the parent directory (thus directly to the include\ directory). If there is no python27\ directory, try it the other way around.

• LINK : fatal error LNK1104: cannot open file ‘python27.lib’

Same as above, but for the Python.win64.framework\libs\ directory.

• Importing the multiarray numpy extension module failed. […] Original error was: DLL load failed: The specified module could not be found.

If you see this error, you may have an existing installation of Python 2.7 (either for your user only or system-wide) that has NumPy installed. Check your sys.path with this script:

import c4d, os, sys
prefs = os.path.dirname(c4d.storage.GeGetC4DPath(c4d.C4D_PATH_PREFS))
appdir = os.path.dirname(sys.executable)
print '-'*60
print 'Prefs Dir:', prefs
print 'Appdir Dir:', appdir
print 'Unexpected elements in sys.path:'
for p in sys.path:
if not p.startswith(prefs) and not p.startswith(appdir):
print '  *', p


If a directory like C:\Users\niklas\AppData\Roaming\Python\Python27\site-packages appears in the console, you do have another Python 2.7 installation and it is very likely that NumPy is first loaded from that directory. Since NumPy in that Python installation was compiled with a different version of MSVC or even for a different architecture (ie. x86), that is why you see the error.

## Final Word

You can find a few pre-compiled Python C-extensions here:

https://public.niklasrosenstein.com/cinema4d/python/modules/

Similar steps apply to macOS, althogh it is even easier because you don’t have to first activate the build environment and not care too much about the correct compiler version.