How to Use Mingw's Gcc Compiler When Installing Python Package Using Pip

Imitate Visual C++ with MinGW or other C++ compilers (for Python packages based on Visual C++)?

There is no chance for this idea.

  1. MSVC

I have sent feedback to them, yet I did not get any reply. A Python developer assured that they know about this size issue anyway and do not like it either. The only chance is a change from MSVC developers themselves. It is unlikely, but not impossible, that the size will be reduced in future releases by the MSVC team.


  1. Python distutils workaround

The Python community will not provide a distutils workaround, see https://discuss.python.org/t/imitate-visual-c-with-mingw-or-other-c-compilers-for-python-packages-based-on-visual-c/4609/11.

Quote from the Python forum:

There was a workaround until Python 3.4 which might also be an
approach now: Use MinGW compiler till Python 3.4 by adding a
“distutils.cfg” to the folder “\Lib\distutils” in Python install
directory. It would be nice to have that MinGW “distutils.cfg”
workaround for the recent Python versions as well.

Now it turns out that distutils will not be a realistic workaround.

  • There is no one who will work on it. A Python developer who was involved in the project before: Maybe there is ...

... someone else who might offer to help. But I wouldn’t be too optimistic.

  • And a deprecation issue:

As an aside, now that setuptools has fully taken on distutils, we’ll
be deprecating it in the standard library (soon). So this request in
future would have to be made to each project implementing a build
tool.

Using pip in windows

Installing the Microsoft Visual C++ 2008 Redistributable Package is not sufficient to compile packages. You need to install a compiler, not just the support files.

There are three ways to do this:

  1. Install Visual C++.
  2. Use mingw's port of gcc instead of Visual C++.
  3. Use cygwin's port of gcc instead of either, and a cygwin build on Python instead of the native one.

If you want to go with option 1, you need to install Visual C++ itself. The free version should work just as well as the paid version, as long as you're not going to build binary packages to redistribute to others. Unfortunately, I'm not sure where to find the 2008 version anymore. As of May 2013, the download page only has 2010 and 2012.

When you install this, it will create a batch file called vcvarsall.bat (not vcvarshall.bat!), and give you the option of putting that batch file in your PATH. Running that batch file sets up a DOS prompt for building with that version of Visual C++. (This is handy if you have multiple versions of Visual C++, or other compilers, around.) If you skip that option, you will have to do it manually.

This question shows how to use a newer Visual Studio with older Python, and also shows how to point distutils at a vcvarsall.bat that's not on your PATH, and has links to a whole lot of other relevant questions and blog posts.


Many people find option 2 simpler. Install mingw, modify your PATH in the environment to include C:\MinGW\bin (or wherever you choose to install it), and pass -c mingw32 whenever you run a setup.py script.

The problem is that it's not as clearly documented how to tell easy_install and pip to use mingw instead of VC++. To do that, you need to find or create a distutils.cfg file, find or create a [build] section within it, and add compiler=mingw32. Not too hard. This blog post looks like it explains things pretty well, or see this answer.


Option 3 is by far the simplest. Install cygwin, tell it to install the Python and gcc packages, and you're done.

The problem is that you don't have native Windows Python, you have a Unix Python running in a fake Unix environment on top of Windows. If you like Cygwin, you'll love this; otherwise, you won't.

Using MinGW C compiler instead of Microsoft Visual Studio Express for Python

  1. Just start %APPDATA%\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat amd64 and you will get environment for 64-bit Python

  2. MinGW compiler needs lot of tweaking in order to link with correct msvcr90.dll library but compiling with MinGW possible too - http://www.mingw.org/wiki/HOWTO_Use_the_GCC_specs_file

pip install cocotb ask me for visual C++ compiler installation

I finally managed to install cocotb with following procedure on msys2.

  • Download and install msys2.
  • update msys2 :
$ pacman -Syu
[close then re-launch terminal]
$ pacman -Su
  • Install Gcc and git
$ pacman -S –needed base-devel mingw-w64-x86_64-toolchain
$ pacman -S git
  • Install GHDL
$ pacman -S mingw-w64-x86_64-ghdl-llvm
  • Install gtkwave
$ pacman -S mingw-w64-x86_64-gtkwave
  • Install python3
$ pacman -S mingw-w64-x86_64-python-scipy mingw-w64-x86_64-python-matplotlib mingw-w64-x86_64-python-numpy
$ pacman -S --needed make mingw-w64-x86_64-gcc mingw-w64-x86_64-python3-pip mingw-w64-x86_64-python3-setuptools mingw-w64-x86_64-python3-wheel
  • Close terminal then open «MSYS2 MinGW 64-bit» from windows start menu.
  • And finally install cocotb with pip install :
$ pip install --no-build-isolation cocotb
$ pip install --no-build-isolation pytest
$ pip install --no-build-isolation cocotbext-axi
  • then enjoy classical cocotb makefile to simulate VHDL with GHDL. And visualize vcd traces with gtkwave.

I tested this procedure on other windows computer with succes.



Related Topics



Leave a reply



Submit