Compiling Python modules on Windows x64 - python

I'm starting out some projects in words processing and I needed NumPy and NLTK.
That was the first time I got to know easy_install and how to compile new module of python into the system.
I have Python 2.7 x64 plus VS 11 and VS 12. Also Cygwin (the latest one I guess).
I could see in the file that compiles using VS that it looks for VS env with the same version as the one that compiled the python code, why?
When I hardcoded 11.0 which is my version, numpy failed to build on several strange errors regarding vcvarsall (it found vcvarsall, probably misused it).
Can't I build python binaries on Windows?
If not, can I cross compile on Linux for Windows? (using the same method as Google for the Android SDK)

Update: This answer is now over 5 years old! Python-2.7 is about to be deprecated, so what you really need is a Microsoft Visual C compiler for Python-3. Take a look at this Python wiki on MS Windows Compilers. MS Build Tools 2015 with VC-14.0 is what you need to build extensions for Python-3.5 and 3.6. You can either install the older MS build tools 2015 which includes VC-14.0 or the newer MS build tools for 2017 - click the link, then scroll down until you find Build Tools for Visual Studio 2017 - which also includes VC-14.0.
Also if your versions of pip and setuptools are up to date, then you can ignore all of that silly old school MSSDK nonsense. Especially if you are using the VC for Python 2.7 or MS build tools 2015. Since setuptools-24, it just knows where to look for these compilers, hopefully.
Update: As Zooba mentions below, free x86 and AMD64 (x86-64) VC90 c-compilers for Python-2.7 are now available from Microsoft.
Update: Patch vcvarsall.bat to use x64 compilers from SDK v7.0 directly with pip in any shell instead of using SDK shell and setting DISTUTILS_USE_SDK and MSSdk environmental variables as in directions below. See Fix vcvarsall.bat to install Python-2.7 x64 extensions with v90 instead of sdk7.
tl;dr: Use Windows SDK v7.0 compilers, open the SDK shell and call
C:\> setenv /release /x64
C:\> set DISTUTILS_USE_SDK=1
C:\> MSSdk=1
to build Python 2.7.x extensions using distutils, pip or easy_install. See Python x64 Package Extensions with pip, MSVC 2008 Express & SDK 7.
Note: You can install Numpy optimized using Intel MKL from Christoph Gohlke. For virtualenv's, try converting the binary distribution windows installer to a wheel file which can be installed with pip. Building NumPy from source is not trivial. It has become a possibility with the recent introduction and availability of OpenBLAS, a fork of GotoBLAS. There are optimized binaries available for Windows x86 and x86-64 as well as source which is relatively simpler to compile than GotoBLAS or ATLAS.
Note: In the past I hadn't recommended compiling NumPy from source because you had to optimize BLAS for your platform which was very time-intensive, and the reference BLAS (FORTRAN) or CBLAS (C/C++) implementations were relatively low-performing. Also NumPy depends on LAPACK which also depends on BLAS an additional hurdle to building NumPy. However OpenBLAS binaries are already compiled with LAPACK, so this obstacle has already been removed. See Carl Kleffner's static MinGW-w64 toolchains to build NumPy with OpenBLAS on Windows - he has resolved some of the ABI incompatibilities and issues linking to the correct Microsoft Visual C Common Runtime library (msvcr90.dll for Python-2.7).
So onward to the main question about installing Python extensions on Windows 7 x64.
The recommended way to compile extensions is to use the same compiler used to compile the Python shared library [1-3]. The official Python 2.7.x for Windows was compiled using Microsoft Visual C++ Compilers version 9.0 [4] (aka MSVC90 or simply VC90) from Microsoft Visual Studio 2008 Professional, which you might be able to get for free from Microsoft DreamSpark. Evidentally the x64 compilers are not installed by default, so make sure they are installed along with the x86 compilers. Note: MS Visual Studio 2008 may no longer be available, but MS Visual Studio 2010 will let you use the MSVC90 toolchain if installed, which can be installed from Windows SDK7.
You can tell what version your Python was built with by looking at the header when you execute the python interpreter from a command line. e.g.: Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 means that it was built with VS 2008 C++. You can build Python yourself from source, but this is also an arduous task. In general it is not recommended to mix compiler runtimes, although in practice you may find that they still work.
If you have your heart set on using GNU GCC, then you would have to use mingw-w64 because MinGW is only for native x86 applications, not AMD64/x86-64. You will need to
create a distutils config file to specify the mingw compiler,
remove the -mno-cygwin bug from distutils.cygwinccompiler.Mingw32ccompiler and
export a library definitions file using either pexports.exe from msys or gendef.exe from msys2 and make a static library libpython27.a using dlltool.exe (msys/msys2) (but the newest releases of Official Python for Windows already have this file in the Python27\lib folder, thanks!).
However in the end you will still need to link against the runtime that python was built with, msvcr90.dll, so you will need Visual C++ 2008 redistributable. Note: Windows GCC from mingw-w64 uses msvcrt.dll which is not the same as msvcr90.dll or later.
If you want to do it for free you can use Microsoft Visual C++ 2008 Express SP1 but you will need to add the Windows SDK 7 for .NET Frameworks 3.5 SP1 because the express version does not have the x64 compilers. The procedure for installing x64 C++ extensions with VS2008 Express & SDK 7 are very similar to the those on the cython site for windows x64 extensions. Note: MS Visual Studio 2008 Express is no longer available or supported.
FYI: You do not necessarily need Visual Studio to build using Microsoft compilers. They are available free with the appropriate SDK package. CL.EXE is the compiler executable, but you will have to set whatever platform options that are normally configured by Autotools or some other tool such as CMAKE. CMAKE plays well with MSVC, but Autotools AFAIK doesn't work with MSVC and would require some POSIX environment and utilities which on Windows are available in MSYS.
For many Python packages that use distutils or setuptools, they can compile extensions using Windows SDK 7 by following the directions that are posted in various places through the reference documentation and wikis:
From the Start Menu select All Programs then Microsoft Windows SDK v7.0 and start CMD Shell to open a command window optimized for Windows SDK.
Step #1 is the equivalent of typing the following in the Run box from the Start Menu or from a Command Prompt (aka C:\Windows\System32\cmd.exe):
%COMSPEC% /E:ON /V:ON /K "%PROGRAMFILES%\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.cmd"
NOTE: The flags /E:ON, which enables command extensions, and /V:ON, which enables delayed variable expansion, are both necessary to for SetEnv.cmd to function, or you will get an message that the x64 compilers are not installed, &c.
Then type setenv /Release /x64 which will set the SDK environment variables specifically for Windows 7 x64 release (vs debug or x86 which are the default).
Type set DISTUTILS_USE_SDK=1 hit return and then type set MSSdk=1 and return to tell distutils.msvccompiler that you are using the SDK, and that the SDK will determine all of the environment variables.
Now use your installation method of choice:
pip install pyyaml ntlk which is the recommended way, see ntlk, but you must have setuptools and pip installed.
python setup.py install for each downloaded, extracted tarball
easy_install pyyaml ntlk which is the old way and the only way to install eggs.
[1] Building C and C++ Extensions on Windows
[2] distutils.msvccompiler — Microsoft Compiler
[3] Python Dev Guide: Getting Started: Windows
[4] What version of visual studio is this python compiled with

Since the other answers, Microsoft has released a compiler package specifically for building extensions for Python 2.7 (and any version of Python that used VS 2008/VC9). Here's how to use it:
Go to http://aka.ms/vcpython27, download and install the package (it's fairly small and does not require administrator rights)
Update setuptools to 6.0 or later (pip install -U setuptools)
Install your package (pip install <whatever>)
The combination of the latest setuptools version and this compiler pack should make it really easy to do this.

there are several ways to do it apparently.
build using mingw
build python 2.7 using VS 2008 express.
i'm not sure regarding which version is good to build 3.2 but it could be VS 2010.
you can compile python x64 from source using your desired VS, but u'll have competability issues with other pre built packages.

This worked best for me:
Install Visual Studio Express 2008
Install Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1 from http://www.microsoft.com/en-us/download/details.aspx?id=3138
Go to C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin and open the Visual Studio 2008 x64 Win64 Command Prompt
In that shell, run pip install <package>
You should not have to install any redistributable packages on client systems when done this way.

For Windows 10, Python 2.7.11, Cygwin 2.5.2 and x86_64-w64-mingw32-gcc 4.9.2
or 5.4.0 (but see note under 1 below), pulling together comments from elsewhere, here's what I had to do for my Python extension to compile, link, and beat a race condition between msvcrt.dll and msvcr90.dll:
Edit distutils/cygwinccompiler, and in class Mingw32CCompiler
Replace the old -mno-cygwin fix with
if self.gcc_version < '4' or is_cygwingcc():
no_cygwin = ' -nostdlib -lgcc'
for gcc 4.9.2 -- note for 5.4.0, no 'if', just
no_cygwin = ''
Get the right compiler in place:
self.set_executables(compiler='x86_64-w64-mingw32-gcc%s -O -Wall' % no_cygwin,
compiler_so='x86_64-w64-mingw32-gcc%s -mdll -O -Wall' % no_cygwin,
compiler_cxx='x86_64-w64-mingw32-g++%s -O -Wall' % no_cygwin,
linker_exe='x86_64-w64-mingw32-gcc%s' % no_cygwin,
linker_so='x86_64-w64-mingw32-%s%s %s %s'
note for x86_64-w64-mingw32-gcc 5.4.0, it seems to work better without msvcr90, so make the call to get_msvcr conditional:
if self.gcc_version < '5':
self.dll_libraries = get_msvcr()
Use a setup.py with the following
libdirs=['C:\\Python27\\libs',...]
macros=[('MS_WIN64','1'),...]
platform="WIN32"
Patch one of my source file headers to include
#ifdef MS_WIN64
#define strdup _strdup
#endif
Finally,
/c/Python27/python [editted setup].py -v build --compiler=mingw32
/c/Python27/python [editted setup].py -v install
Some of the above may not be in quite the optimal place, but it got my extension working...

Related

pip install error: Microsoft Visual C++ 10.0 is required

I have Python 3.4, running on Windows 10 x64, trying to install pylint via pip: pip install pylint.
When doing so, I get the following error: Microsoft Visual C++ 10.0 is required. Get it with "Microsoft Windows SDK 7.1": www.microsoft.com/download/details.aspx?id=8279
However I already have:
Microsoft Visual C++ 2010 x64, Microsoft Visual C++ x86 both Redistributable
Microsoft Visual C++ 2013 x64, Microsoft Visual C++ x86 both Redistributable
Microsoft Visual C++ 2015 - 2019 x64, Redistributable
Microsoft Visual C++ 2017 x64, Microsoft Visual C++ 2017 x86 both Redistributable
I saw some other posts with possible solutions that I've tried and did not seem to help to me -
updating setuptools and virtualenv
Downloading the SDK given in the error
Add VS110COMNTOOLS, VS120COMNTOOLS, VS140COMNTOOLS env variables
Of course, after every attempt I've rebooted my PC.
What else could I try to fix this?
Is it possible to install pylint without pip?
You don't have any Visual C++, you only have Redistributable packages. They install run-time libraries that are used to run applications written with VC. But you need Visual C++ compiler! Install Visual Studio 2010 Express.
See also https://stackoverflow.com/search?q=%5Bpip%5D+%22Microsoft+Visual+C%2B%2B+10.0+is+required%22
Python 3.4
According to [Python]: PEP 429 -- Python 3.4 Release Schedule (emphasis is mine):
Python 3.4 has now reached its end-of-life and has been retired. No more releases will be made.
The last version (3.4.10) was released on 2019/03/18.
So, you should move away from it (I'd recommend the (current) LTS version: 3.7). That doesn't magically solve all possible problems, but being a newer version, it requires newer build tools (VStudio) which are easier to find, and have a higher probability of installing and running correctly.
Notes:
Even if you manage to solve the problem for this version, you'll be likely to run into it again when trying to install other such packages, as more and more of them will no longer support it
Python 3.5+ are built with VStudio 14.X. Starting with VStudio 2015 (14.0), MS did some changes to improve backward and forward compatibility between VStudio versions. More details on [SO]: How to circumvent Windows Universal CRT headers dependency on vcruntime.h - (#CristiFati's answer)
2. Pylint
[PyPI]: Pylint is written entirely in Python (I checked the source code), so it wouldn't require a C(++) compiler. This is also backed up by the existence of the pylint-2.3.1-py3-none-any.whl file (available for download).
Some of its dependencies (wrapt, typed-ast: which don't have prebuilt binaries for Python 3.4 on Win - makes sense considering the previous bullet) require it.
According to [PyPA]: pip install - Options (pip install -h):
--no-deps
    Don’t install package dependencies.
Now, I don't know your Pylint usecase: whether you are using the parts of it that require the above dependencies, but if you don't, simply pass the --no-deps argument.
[cfati#CFATI-5510-0:e:\Work\Dev\StackOverflow\q057581571]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.04.04_test0\Scripts\python.exe" -c "import sys;print(sys.version)"
3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)]
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.04.04_test0\Scripts\python.exe" -m pip freeze
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.04.04_test0\Scripts\python.exe" -m pip install --no-deps pylint
Collecting pylint
Using cached https://files.pythonhosted.org/packages/60/c2/b3f73f4ac008bef6e75bca4992f3963b3f85942e0277237721ef1c151f0d/pylint-2.3.1-py3-none-any.whl
Installing collected packages: pylint
Successfully installed pylint-2.3.1
You are using pip version 18.1, however version 19.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.04.04_test0\Scripts\python.exe" -m pip freeze
pylint==2.3.1
[prompt]>
[prompt]> :: Pylint dummy usage example - just import it
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.04.04_test0\Scripts\python.exe" -c "import pylint;print(pylint)"
<module 'pylint' from 'e:\\Work\\Dev\\VEnvs\\py_064_03.04.04_test0\\lib\\site-packages\\pylint\\__init__.py'>
As seen, the package was installed, and it's usable.
Notes:
There are (official) prebuilt Python 3.5 (and newer) binaries for typed-ast, but not for wrapt, so a Python upgrade will still require building it (assuming that it's needed)
You could always turn to unofficial prebuilt binaries. [UCI.LFD]: Unofficial Windows Binaries for Python Extension Packages is the most comprehensive repository (that I know of), and contains the wrapt build for Python 3.4 (but doesn't the typed-ast one :) )
3. Or else ...
Python 3.4 version is built (on Win) using VStudio 2010 (10.0). Since it's written in C, it requires a C compiler. More details on [Python.Wiki]: WindowsCompilers.
Every package that contains parts written in C(++) (and needs to be built from sources at pip install time - meaning that there are no prebuilt binaries on the public repository), also requires the compiler (the compiler version must match the one that Python was built with).
The above URL lists the 4 items that need to be installed in order to get things going:
Microsoft Visual C++ 2010 Service Pack 1 Compiler Update for the Windows SDK 7.1
Microsoft Windows SDK for Windows 7 and .NET Framework 4
The other 2 are dependencies for these 2 (and I tend to think that will be automatically installed)
I didn't (have to) attempt this step, as I already have VStudio 2010 installed. As a side note, I have 7 VStudio versions (with their corresponding SDKs) installed, and also I have 30+ Python instances (installed, VEnvs, custom built by me, installed by other 3rd-party software, ...) on my machine.
Once you get the (above) build tools installed (and the %VS*% env vars pointing to the desired VStudio version - you have to do this manually when having multiple versions), you will be able to install most of the Python packages containing C(++) code. But there will probably be a few exceptions. Here's an example that gave me some headaches: [SO]: Installing pygraphviz on Windows 10 64-bit, Python 3.6 - (#CristiFati's answer) (check it out - and the URLs it references, there's a bunch of useful information there).
I noticed that you're having some problems installing VStudio 2010. With the risk of repeating myself, switching to a newer Python version would make some of your current problems go away, since it would require newer build tools which are less likely to fail installing. Everything else still applies, though.
Update #0
Concerning the last inquiry in the question:
It's possible to install Pylint without pip, for example doing (almost) what pip does, but manually. However, from what am I concerned, this would be a (lame) workaround (gainarie). Anyway, here are the steps:
Download and unpack the .whl (from section #2.)
Copy the (inside) pylint dir, under Python's site-packages dir
But again, everything done here is also done properly in #2., so this is truly pointless.
Maybe you can try to install pylint from https://www.lfd.uci.edu/~gohlke/pythonlibs/
You could try installing Visual Studio Code and opening your project from there. From my experience, Visual Studio Code offers to install pylint (and other missing libraries) for you when it recognizes it's missing.

Python pip show me error: Unable to find vcvarsall.bat [duplicate]

I tried to install the Python package dulwich:
pip install dulwich
But I get a cryptic error message:
error: Unable to find vcvarsall.bat
The same happens if I try installing the package manually:
> python setup.py install
running build_ext
building 'dulwich._objects' extension
error: Unable to find vcvarsall.bat
Update: Comments point out that the instructions here may be dangerous. Consider using the Visual C++ 2008 Express edition or the purpose-built Microsoft Visual C++ Compiler for Python (details) and NOT using the original answer below. Original error message means the required version of Visual C++ is not installed.
For Windows installations:
While running setup.py for package installations, Python 2.7 searches for an installed Visual Studio 2008. You can trick Python to use a newer Visual Studio by setting the correct path in VS90COMNTOOLS environment variable before calling setup.py.
Execute the following command based on the version of Visual Studio installed:
Visual Studio 2010 (VS10): SET VS90COMNTOOLS=%VS100COMNTOOLS%
Visual Studio 2012 (VS11): SET VS90COMNTOOLS=%VS110COMNTOOLS%
Visual Studio 2013 (VS12): SET VS90COMNTOOLS=%VS120COMNTOOLS%
Visual Studio 2015 (VS14): SET VS90COMNTOOLS=%VS140COMNTOOLS%
WARNING: As noted below, this answer is unlikely to work if you are trying to compile python modules.
See Building lxml for Python 2.7 on Windows for details.
I found the solution.
I had the exact same problem, and error, installing 'amara'. I had mingw32 installed, but distutils needed to be configured.
I have Python 2.6 that was already installed.
I installed mingw32 to C:\programs\mingw\
Add mingw32's bin directory to your environment variable: append c:\programs\MinGW\bin; to the PATH
Edit (create if not existing) distutils.cfg file located at C:\Python26\Lib\distutils\distutils.cfg to be:
[build]
compiler=mingw32
Now run easy_install.exe amara.
Make sure environment is set by opening a new cmd.exe.
You can install compiled version from http://www.lfd.uci.edu/~gohlke/pythonlibs/
If you want to compile with Visual Studio C++ instead of mingw...
Run python.exe to display which version of VC++ it was compiled with (example shown below).
It is important to use the corresponding version of the Visual C++ compiler that Python was compiled with since distilutils's get_build_version prevents mixing versions (per Piotr's warning).
Yellow (top) is Python 2.7, compiled with MSC v.1500 (Visual Studio C++ 2008)
Red (bottom) is Python 3.4.1, compiled with MSC v.1600 (Visual Studio C++ 2010)
Use the table below[1] to match the internal VC++ version with the corresponding Visual Studio release:
MSC v.1000 -> Visual C++ 4.x
MSC v.1100 -> Visual C++ 5
MSC v.1200 -> Visual C++ 6
MSC v.1300 -> Visual C++ .NET
MSC v.1310 -> Visual C++ .NET 2003
MSC v.1400 -> Visual C++ 2005 (8.0)
MSC v.1500 -> Visual C++ 2008 (9.0)
MSC v.1600 -> Visual C++ 2010 (10.0)
MSC v.1700 -> Visual C++ 2012 (11.0)
MSC v.1800 -> Visual C++ 2013 (12.0)
MSC v.1900 -> Visual C++ 2015 (14.0)
MSC v.1910 -> Visual C++ 2017 (15.0)
MSC v.1911 -> Visual C++ 2017 (15.3)
MSC v.1912 -> Visual C++ 2017 (15.5)
MSC v.1913 -> Visual C++ 2017 (15.6)
MSC v.1914 -> Visual C++ 2017 (15.7)
MSC v.1915 -> Visual C++ 2017 (15.8)
MSC v.1916 -> Visual C++ 2017 (15.9)
Download and install the corresponding version of Visual Studio C++ from the previous step.
Additional notes for specific versions of VC++ are listed below.
Notes for Visual Studio C++ 2008
For only the 32-bit compilers, download Visual Studio C++ 2008 Express Edition.
For the 64-bit compilers[2][3], download Windows SDK for Windows 7 and .NET Framework 3.5 SP1.
Uncheck everything except Developer Tools >> Visual C++ Compilers to save time and disk space from installing SDK tools you otherwise don't need.
Notes for Visual Studio C++ 2010
According to Microsoft, if you installed Visual Studio 2010 SP1, it may have removed the compilers and libraries for VC++.
If that is the case, download Visual C++ 2010 SP1 Compiler Update.
Notes for Visual Studio C++ 2015
If you don't need the Visual Studio IDE, download Visual Studio C++ 2015 Build Tools.
Notes for Visual Studio C++ 2017
If you don't need the Visual Studio IDE, download Build Tools for Visual Studio 2017.
Suggestion: If you have both a 32- and 64-bit Python installation, you may also want to use virtualenv to create separate Python environments so you can use one or the other at a time without messing with your path to choose which Python version to use.
According to #srodriguex, you may be able to skip manually loading the batch file (Steps 4-6) by instead copying a few batch files to where Python is searching by following this answer. If that doesn't work, here are the following steps that originally worked for me.
Open up a cmd.exe
Before you try installing something which requires C extensions, run the following batch file to load the VC++ compiler's environment into the session (i.e. environment variables, the path to the compiler, etc).
Execute:
32-bit Compilers:
Note: 32-bit Windows installs will only have C:\Program Files\ as expected
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
64-bit Compilers:
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars64.bat"
Note: Yes, the native 64-bit compilers are in Program Files (x86). Don't ask me why.
Additionally, if you are wondering what the difference between vcvars64.bat and vcvarsx86_amd64.bat or more importantly the difference between amd64 and x86_amd64, the former are for the native 64-bit compiler tools and the latter are the 64-bit cross compilers that can run on a 32-bit Windows installation.
Update:
If for some reason you are getting error: ... was unexpected at this time. where the ... is some series of characters, then you need to check that you path variable does not have any extraneous characters like extra quotations or stray characters. The batch file is not going to be able to update your session path if it can't make sense of it in the first place.
If that went well, you should get one of the following messages depending on which version of VC++ and which command you ran:
For the 32-bit compiler tools:
Setting environment for using Microsoft Visual Studio 20xx x86 tools.
For the 64-bit compiler tools:
Setting environment for using Microsoft Visual Studio 20xx x64 tools.
Now, run the setup via python setup.py install or pip install pkg-name
Hope and cross your fingers that the planets are aligned correctly for VC++ to cooperate.
What's going on? Python modules can be part written in C or C++ (typically for speed). If you try to install such a package with Pip (or setup.py), it has to compile that C/C++ from source. Out the box, Pip will brazenly assume you the compiler Microsoft Visual C++ installed. If you don't have it, you'll see this cryptic error message "Error: Unable to find vcvarsall.bat".
The prescribed solution is to install a C/C++ compiler, either Microsoft Visual C++, or MinGW (an open-source project). However, installing and configuring either is prohibitively difficult. (Edit 2014: Microsoft have published a special C++ compiler for Python 2.7)
The easiest solution is to use Christoph Gohlke's Windows installers (.msi) for popular Python packages. He builds installers for Python 2.x and 3.x, 32 bit and 64 bit. You can download them from http://www.lfd.uci.edu/~gohlke/pythonlibs/
If you too think "Error: Unable to find vcvarsall.bat" is a ludicrously cryptic and unhelpful message, then please comment on the bug at http://bugs.python.org/issue2943 to replace it with a more helpful and user-friendly message.
For comparison, Ruby ships with a package manager Gem and offers a quasi-official C/C++ compiler, DevKit. If you try to install a package without it, you see this helpful friendly useful message:
Please update your PATH to include build tools or download the DevKit from http://rubyinstaller.org/downloads and follow the instructions at http://github.com/oneclick/rubyinstaller/wiki/Development-Kit
You can read a longer rant about Python packaging at https://stackoverflow.com/a/13445719/284795
You'll need to install a Microsoft compiler, compatible with the compiler used to build Python. This means you need Visual C++ 2008 (or newer, with some tweaking).
Microsoft now supplies a bundled compiler and headers just to be able to compile Python extensions, at the memorable URL:
Microsoft Visual C++ Compiler for Python 2.7
http://aka.ms/vcpython27
This is a relatively small package; 85MB to download, installable without admin privileges, no reboot required. The name is a little misleading, the compiler will work for any Python version originally compiled with Visual C++ 2008, not just Python 2.7.
If you start a Python interactive prompt or print sys.version, look for the MSC version string; if it is MSC v.1500 you can use this tool.
From the original announcement to the distutils list:
Microsoft has released a compiler package for Python 2.7 to make it easier for people to build and distribute their C extension modules on Windows. The Microsoft Visual C++ Compiler for Python 2.7 (a.k.a. VC9) is available from: http://aka.ms/vcpython27
This package contains all the tools and headers required to build C extension modules for Python 2.7 32-bit and 64-bit (note that some extension modules require 3rd party dependencies such as OpenSSL or libxml2 that are not included). Other versions of Python built with Visual C++ 2008 are also supported, so "Python 2.7" is just advertising - it'll work fine with 2.6 and 3.2.
Note that you need to have setuptools 6.0 or newer installed (listed in the system requirements on the download page). The project you are installing must use setuptools.setup(), not distutils or the auto-detection won't work.
Microsoft has stated that they want to keep the URL stable, so that automated scripts can reference it easily.
I just had this same problem, so I'll tell my story here hoping it helps someone else with the same issues and save them the couple of hours I just spent:
I have mingw (g++ (GCC) 4.6.1) and python 2.7.3 in a windows 7 box and I'm trying to install PyCrypto.
It all started with this error when running setup.py install:
error: Unable to find vcvarsall.bat
Easily solved after googling the error by specifying mingw as the compiler of choice:
setup.py install build --compiler=mingw32
The problem is that then I got a different error:
configure: error: cannot run C compiled programs.
It turns out that my anti-virus was blocking the execution of a freshly compiled .exe. I just disabled the anti-virus "resident shield" and went to the next error:
cc1.exe: error: unrecognized command line option '-mno-cygwin'
error: command 'gcc' failed with exit status 1
This solved it: "Either install a slightly older version of MinGW, or edit distutils\cygwinccompiler.py in your Python directory to remove all instances of -mno-cygwin." (from here)
Now, I can finally start working.
Looks like its looking for VC compilers, so you could try to mention compiler type with -c mingw32, since you have msys
python setup.py install -c mingw32
I have python 2.73 and windows 7 .The solution that worked for me was:
Added mingw32's bin directory to environment variable: append PATH with C:\programs\mingw\bin;
Created distutils.cfg located at C:\Python27\Lib\distutils\distutils.cfg containing:
[build]
compiler=mingw32
To deal with MinGW not recognizing the -mno-cygwin flag anymore, remove the flag in C:\Python27\Lib\distutils\cygwincompiler.py line 322 to 326, so it looks like this:
self.set_executables(compiler='gcc -O -Wall',
compiler_so='gcc -mdll -O -Wall',
compiler_cxx='g++ -O -Wall',
linker_exe='gcc',
linker_so='%s %s %s'
% (self.linker_dll, shared_option,
entry_point))
Look in the setup.py file of the package you are trying to install. If it is an older package it may be importing distutils.core.setup() rather than setuptools.setup().
I ran in to this (in 2015) with a combination of these factors:
The Microsoft Visual C++ Compiler for Python 2.7 from http://aka.ms/vcpython27
An older package that uses distutils.core.setup()
Trying to do python setup.py build rather than using pip.
If you use a recent version of pip, it will force (monkeypatch) the package to use setuptools, even if its setup.py calls for distutils. However, if you are not using pip, and instead are just doing python setup.py build, the build process will use distutils.core.setup(), which does not know about the compiler install location.
Solution
Step 1: Open the appropriate Visual C++ 2008 Command Prompt
Open the Start menu or Start screen, and search for "Visual C++ 2008 32-bit Command Prompt" (if your python is 32-bit) or "Visual C++ 2008 64-bit Command Prompt" (if your python is 64-bit). Run it. The command prompt should say Visual C++ 2008 ... in the title bar.
Step 2: Set environment variables
Set these environment variables in the command prompt you just opened.
SET DISTUTILS_USE_SDK=1
SET MSSdk=1
Reference http://bugs.python.org/issue23246
Step 3: Build and install
cd to the package you want to build, and run python setup.py build, then python setup.py install. If you want to install in to a virtualenv, activate it before you build.
Maybe somebody can be interested, the following worked for me for the py2exe package.
(I have windows 7 64 bit and portable python 2.7, Visual Studio 2005 Express with Windows SDK for Windows 7 and .NET Framework 4)
set VS90COMNTOOLS=%VS80COMNTOOLS%
then:
python.exe setup.py install
I spent almost 2 days figuring out how to fix this problem in my python 3.4 64 bit version: Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Solution 1, hard: (before reading this, read first Solution 2 below)
Finally, this is what helped me:
install Visual C++ 2010 Express
install Microsoft Windows SDK v7.1 for Windows 7
create manually file vcvars64.bat in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64 which contains CALL "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64 or other path depending on where you have yours installed
(this seems to be optional) install Microsoft Visual Studio 2010 Service Pack 1 together with Microsoft Visual C++ 2010 Service Pack 1 Compiler Update for the Windows SDK 7.1
after that I tried to pip install numpy but received the following error:
File "numpy\core\setup.py", line 686, in get_mathlib_info
raise RuntimeError("Broken toolchain: cannot link a simple C program")
RuntimeError: Broken toolchain: cannot link a simple C program
I changed mfinfo to None in C:\Python34\Lib\distutils\msvc9compiler.py per this https://stackoverflow.com/a/23099820/4383472
finally after pip install numpy command my avast antivirus tried to interfere into the installation process, but i quickly disabled it
It took very long - several minutes for numpy to compile, I even thought that there was an error, but finally everything was ok.
Solution 2, easy:
(I know this approach has already been mentioned in a highly voted answer, but let me repeat since it really is easier)
After going through all of this work I understood that the best way for me is just to use already precompiled binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/ in future. There is very small chance that I will ever need some package (or a version of a package) which this site doesn't contain. The installation process is also much quicker this way. For example, to install numpy:
donwload numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl (if you have Python 3.4 64-bit) from that site
in command prompt or powershell install it with pip pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl (or full path to the file depending how command prompt is opened)
I wanted to run pysph on Windows 10 under Python 2.7 and got vcvarsall.bat was not found (from distutils)
My solution was the following:
Install Microsoft Visual C++ for Python 2.7 (like #Michael suggested)
On Windows 10 it was installed into (my username is Andreas):
C:\Users\Andreas\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0
Set environment variable VS90COMNTOOLS to the installation path of Visual C++ for Python 2.7 (see above path).
If it still doesn't work, then modifiy in the module
C:/Python27/lib/distutils
the file msvc9compiler.py. Find in it the function find_vcvarsall and do following modification.
Replace the line:
productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
with
productdir = os.path.join(toolsdir)
This is where vcvarsall.bat resides in my case (check, where vcvarsall.bat is in your installation).
I encountered this issue when I tried to install numpy library on my python 3.5. The solution is to install VS2015. I had VS2008, 2012, 2013, none of which is compatible with python 3.5. Apparently newer version of python has dependency on newer versions of VS.
Also make sure C++ Common Tools are installed with Visual Studio.
Use this link to download and install Visual C++ 2015 Build Tools. It will automatically download visualcppbuildtools_full.exe and install Visual C++ 14.0 without actually installing Visual Studio. After the installation completes, retry pip install and you won't get the error again.
I have tested it on following platform and versions:
Python 3.6 on Windows 7 64-bit
Python 3.7 on Windows Server 2016 (64-bit system)
Python 3.8 on Windows 10 64-bit
I tried all the above answers, and found all of them not to work, this was perhaps I was using Windows 8 and had installed Visual Studio 2012. In this case, this is what you do.
The vcvarsall.bat file is located here:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC
Simply select the file, and copy it.
Then go to this directory:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools
and paste the file. And then, all should be well.
You can download the free Visual C++ 2008 Express Edition from http://go.microsoft.com/?linkid=7729279, which will set the VS90COMNTOOLS environment variable during installation and therefore build with a compatible compiler.
As #PiotrDobrogost mentioned in a comment, his answer to this other question goes into details about why Visual C++ 2008 is the right thing to build with, but this can change as the Windows build of Python moves to newer versions of Visual Studio: Building lxml for Python 2.7 on Windows
I had this problem using Python 3.4.1 on Windows 7 x64, and unfortunately the packages I needed didn't have suitable exe or wheels that I could use. This system requires a few 'workarounds', which are detailed below (and TLDR at bottom).
Using the info in Jaxrtech's answer above, I determined I needed Visual Studio C++ 2010 (sys.version return MSC v.1600), so I installed Visual C++ 2010 Express from the link in his answer, which is http://go.microsoft.com/?linkid=9709949. I installed everything with updates, but as you can read below, this was a mistake. Only the original version of Express should be installed at this time (no updated anything).
vcvarsall.bat was now present, but there was a new error when installing the package, query_vcvarsall raise ValueError(str(list(result.keys())))ValueError: [u'path']. There are other stackoverflow questions with this error, such as Errors while building/installing C module for Python 2.7
I determined from that answer that 2010 Express only installs 32-bit compilers. To get 64-bit (and other) compilers, you need to install Windows 7.1 SDK. See http://msdn.microsoft.com/en-us/windowsserver/bb980924.aspx
This would not install for me though, and the installer returned the error installation failed with return code 5100. I found the solution at the following link: http://support.microsoft.com/kb/2717426. In short, if newer versions of x86 and x64 Microsoft Visual C++ 2010 Redistributable's are installed, they conflict with the ones in SDK installer, and need uninstalling first.
The SDK then installed, but I noticed vcvars64.bat still did not exist in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin, nor its subfolders. vcvarsall.bat runs the vcvars64 batch file, so without it, the python package still wouldn't install (I forgot the error that was shown at this time).
I then found some instructions here: http://www.cryptohaze.com/wiki/index.php/Windows_7_Build_Setup#Download_VS_2010_and_Windows_SDK_7.1
Following the instructions, I had already installed Express and 7.1 SDK, so installed SDK 7.1 SP1, and did the missing header file fix. I then manually created vcvars64.bat with the content CALL setenv /x64. I will paste all those instructions here, so they don't get lost.
Step 1 is to download Visual Studio Express 2010.
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express
is a good place to start. Download the installer, and run it
(vc_web.exe). You don't need the SQL 2008 additional download.
You'll also need the Windows SDK (currently 7.1) for the 64-bit
compilers - unless you want to do 32-bit only builds, which are not
fully supported...
http://www.microsoft.com/en-us/download/details.aspx?id=8279 is a good
starting point to download this - you'll want to run winsdk_web.exe
when downloaded!
The default install here is just fine.
Finally, download and install the Windows SDK 7.1 SP1 update:
http://www.microsoft.com/en-us/download/details.aspx?id=4422
And, to fix missing header file, VS2010 SP1.
http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=75568aa6-8107-475d-948a-ef22627e57a5
And, bloody hell, fix the missing batch file for VS2010 Express. This
is getting downright absurd.
In C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64,
create "vcvars64.bat" with the following (you will need to be running
as administrator):
CALL setenv /x64
My python package still did not install (can't recall error). I then found some instructions (copied below) to use the special SDK 7.1 Command Prompt, see: https://mail.python.org/pipermail/distutils-sig/2012-February/018300.html
Never mind this question. Somebody here noticed this item on the menu: Start->All Programs->Microsoft Windows SDK v7.1 ->Windows SDK 7.1 Command Prompt
This runs a batch job that appears to set up a working environment for the compiler. From that prompt, you can type "setup.py build" or "setup.py install".
I opened the Windows SDK 7.1 Command Prompt as instructed, and used it to run easy_install on the python package. And at last, success!
TLDR;
Install Visual Studio Express 2010 (preferably without updated redistributables or SQL server).
Install Windows 7.1 SDK
Instal SDK 7.1 SP1 update, and VS2010 SP1 header file fix (this step may not be required).
Manually create C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\vcvars64.bat with content CALL setenv /x64
Start->All Programs->Microsoft Windows SDK v7.1 ->Windows SDK 7.1 Command Prompt to open special x64 command prompt, which can then be used with python/easy_install/pip/etc (including those in virtual_envs).
Below steps fixed this issue for me, I was trying to create setup with cython extension.
Install Microsoft Visual C++ Compiler for Python 2.7
The default install location would be #
C:\Users\PC-user\AppData\Local\Programs\Common\Microsoft\Visual C++
for Python This might actually fix the issue, test once before proceeding.
If it fails, Check where in VC++
for python vcvarsall.bat file is located
Open the msvc9compiler.py
file of distutils package in notepad.
In my box this was #
C:\Anaconda2\Lib\distutils\msvc9compiler.py find_vcvarsall function
in this file, determine the version of VC by printing out version
argument. For Python 2.7 it's likely to be 9.0
Now create an
environment variable VS90COMNTOOLS, Pointing to
C:\Users\PC-user\AppData\Local\Programs\Common\Microsoft\Visual C++
for Python\9.0\VC\bin
For some reason distutils expects the
vcvarsall.bat file to be within VC dir, but VC++ for python tools
has it in the root of 9.0 To fix this, remove "VC" from the
path.join (roughly around line 247)
#productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
productdir = os.path.join(toolsdir, os.pardir, os.pardir)
The above steps fixed the issue for me.
calling import setuptools will monkey patch distutils to force compatibility with Visual Studio. Calling vcvars32.bat manually will setup the virtual environment and prevent other common errors the compiler will throw. For VS 2017 the file is located at
"C:\Program Files (x86)\Microsoft Visual
Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"
Here is the setup script I use to quickly compile .pyx files to .pyd:
(Note: it uses the 3rd party module
send2trash
# cython_setup.py
import sys, os, time, platform, subprocess
from setuptools import setup, find_packages
from Cython.Build import cythonize
from traceback import format_exc
# USAGE:
#
# from cython_setup import run
# run(pyx_path)
# vcvars = r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"
# NOTE: to use visual studio 2017 you must have setuptools version 34+
vcvars = r"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars32.bat"
def _build_ext():
try:
pyx_path = sys.argv.pop(-1)
pyx_path = os.path.abspath(pyx_path)
if not os.path.exists(pyx_path):
raise FileNotFoundError(f"{pyx_path} does not exist")
project_name = sys.argv.pop(-1)
os.chdir(os.path.abspath(os.path.dirname(pyx_path)))
print("cwd: %s" % os.getcwd())
print(os.path.abspath("build"))
setup(
name=project_name,
# cmdclass = {'build_ext': build_ext},
packages=find_packages(),
# ext_modules=cythonize(extensions)
ext_modules=cythonize(pyx_path,
compiler_directives={'language_level': 3, 'infer_types': True, 'binding': False},
annotate=True),
# include_dirs = [numpy.get_include()]
build_dir=os.path.abspath("build")
)
except:
input(format_exc())
def retry(func):
def wrapper(*args, **kw):
tries = 0
while True:
try:
return func(*args, **kw)
except Exception:
tries += 1
if tries > 4:
raise
time.sleep(0.4)
return wrapper
#retry
def cleanup(pyx_path):
from send2trash import send2trash
c_file = os.path.splitext(pyx_path)[0] + ".c"
if os.path.exists(c_file):
os.remove(c_file)
if os.path.exists("build"):
send2trash("build")
def move_pyd_files(pyx_path):
pyx_dir = os.path.dirname(pyx_path)
build_dir = os.path.join(pyx_dir, "build")
if not os.path.exists(build_dir):
raise RuntimeError(f"build_dir {build_dir} did not exist....")
found_pyd = False
for top, dirs, nondirs in os.walk(build_dir):
for name in nondirs:
if name.lower().endswith(".pyd") or name.lower().endswith(".so"):
found_pyd = True
old_path = os.path.join(top, name)
new_path = os.path.join(pyx_dir, name)
if os.path.exists(new_path):
print(f"removing {new_path}")
os.remove(new_path)
print(f"file created at {new_path}")
os.rename(old_path, new_path)
if not found_pyd:
raise RuntimeError("Never found .pyd file to move")
def run(pyx_path):
"""
:param pyx_path:
:type pyx_path:
:return: this function creates the batch file, which in turn calls this module, which calls cythonize, once done
the batch script deletes itself... I'm sure theres a less convoluted way of doing this, but it works
:rtype:
"""
try:
project_name = os.path.splitext(os.path.basename(pyx_path))[0]
run_script(project_name, os.path.abspath(pyx_path))
except:
input(format_exc())
def run_script(project_name, pyx_path):
dirname = os.path.dirname(pyx_path)
# ------------------------------
os.chdir(dirname)
if os.path.exists(vcvars):
# raise RuntimeError(
# f"Could not find vcvars32.bat at {vcvars}\nis Visual Studio Installed?\nIs setuptools version > 34?")
subprocess.check_call(f'call "{vcvars}"', shell=True)
cmd = "python" if platform.system() == "Windows" else "python3"
subprocess.check_call(f'{cmd} "{__file__}" build_ext "{project_name}" "{pyx_path}"', shell=True)
move_pyd_files(pyx_path)
cleanup(pyx_path)
if len(sys.argv) > 2:
_build_ext()
I tried many solutions but only one worked for me, the install of Microsoft Visual Studio 2008 Express C++.
I got this issue with a Python 2.7 module written in C (yEnc, which has other issues with MS VS). Note that Python 2.7 is built with MS VS 2008 version, not 2010!
Despite the fact it's free, it is quite hard to find since MS is promoting VS 2010.
Still, the MSDN official very direct links are still working: check https://stackoverflow.com/a/15319069/2227298 for download links.
If you have mingw installed
pip install --global-option build_ext --global-option --compiler=mingw32 packagename
works, forcing pip to build using the mingw compiler instead of Microsoft's. See here https://github.com/pypa/pip/issues/18 for details (last post).
Is Microsoft Visual C++ Compiler for Python 2.7 at http://www.microsoft.com/en-us/download/details.aspx?id=44266 not a solution?
The easiest way to solve this in 2016 is to install Chocolatey and then the vcpython27 package. Open Powershell:
> iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
> choco install python2 -y
> choco install vcpython27 -y
I don't know if it is too late, but I found Microsoft Visual C++ Compiler for Python 2.7 which reads
The typical error message you will receive if you need this compiler package is Unable to find vcvarsall.bat
Hope this helps!
An exhaustive list of MS VС++ versions and installation variations officially supported by distutils (and some by setuptools) and how to use them can be found at
https://wiki.python.org/moin/WindowsCompilers
It also specifies which VC++ version is required for which official Win32 Python release. Note that MinGW is not officially supported (see below for details).
In brief:
For each release, there's a specific Visual Studio release that works out of the box.
Note that Express editions before 2012 do not support x64. Trying to use one for x64 results in an obscure error.
For standalone SDKs, it's generally required to run the installation from the SDK prompt (=set environment variables).
For some, this requirement can be lifted by a one-time tweaking which doesn't have adversal side effects
Some unusual VC++ releases are supported by setuptools and not distutils.
MinGW is said to "work", but only for x86. https://bugs.python.org/issue4709 says that problems with its support lie in MinGW itself and even x86 is bound to have problems since Python 3.5.
I got the same problem and have solved it at the moment.
"Google" told me that I need to install "Microsoft Visual C++ Compiler for Python 2.7". I install not only the tool, but also Visual C++ 2008 Reditributable, but it didn't help. I then tried to install Visual C++ 2008 Express Edition. And the problem has gone!
Just try to install Visual C++ 2008 Express Edition!
If you're looking to install pyodbc on a Windows box that doesn't have Visual Studio installed another option is to manually install pyodbc using the binary distribution.
This is particularly useful if you do not have administrator privileges on the machine you're working with and are trying to set up a virtualenv.
Steps:
Download the latest Windows installer from here (pyodbc-X.X.X.win-Y-py2.7.exe)
Open the installer executable using 7-Zip (or WinRAR or whatever)
Extract pyodbc.pyd and pyodbc-X.X.X-py2.7.egg-info and place them in [python installation directory or virtualenv]\Lib\site-packages
There is no step 4 :)
With Python 3.4, the dependency is on Visual Studio 2010. Installing Visual C++ 2010 Express fixed the problem for me.
Tricking it into using the VS 2008 or 2013 installs that I happened to have didn't work.
You can use easy_install instead of pip it works for me.

Python : Unable to find vcvarsall.bat [duplicate]

I tried to install the Python package dulwich:
pip install dulwich
But I get a cryptic error message:
error: Unable to find vcvarsall.bat
The same happens if I try installing the package manually:
> python setup.py install
running build_ext
building 'dulwich._objects' extension
error: Unable to find vcvarsall.bat
Update: Comments point out that the instructions here may be dangerous. Consider using the Visual C++ 2008 Express edition or the purpose-built Microsoft Visual C++ Compiler for Python (details) and NOT using the original answer below. Original error message means the required version of Visual C++ is not installed.
For Windows installations:
While running setup.py for package installations, Python 2.7 searches for an installed Visual Studio 2008. You can trick Python to use a newer Visual Studio by setting the correct path in VS90COMNTOOLS environment variable before calling setup.py.
Execute the following command based on the version of Visual Studio installed:
Visual Studio 2010 (VS10): SET VS90COMNTOOLS=%VS100COMNTOOLS%
Visual Studio 2012 (VS11): SET VS90COMNTOOLS=%VS110COMNTOOLS%
Visual Studio 2013 (VS12): SET VS90COMNTOOLS=%VS120COMNTOOLS%
Visual Studio 2015 (VS14): SET VS90COMNTOOLS=%VS140COMNTOOLS%
WARNING: As noted below, this answer is unlikely to work if you are trying to compile python modules.
See Building lxml for Python 2.7 on Windows for details.
I found the solution.
I had the exact same problem, and error, installing 'amara'. I had mingw32 installed, but distutils needed to be configured.
I have Python 2.6 that was already installed.
I installed mingw32 to C:\programs\mingw\
Add mingw32's bin directory to your environment variable: append c:\programs\MinGW\bin; to the PATH
Edit (create if not existing) distutils.cfg file located at C:\Python26\Lib\distutils\distutils.cfg to be:
[build]
compiler=mingw32
Now run easy_install.exe amara.
Make sure environment is set by opening a new cmd.exe.
You can install compiled version from http://www.lfd.uci.edu/~gohlke/pythonlibs/
If you want to compile with Visual Studio C++ instead of mingw...
Run python.exe to display which version of VC++ it was compiled with (example shown below).
It is important to use the corresponding version of the Visual C++ compiler that Python was compiled with since distilutils's get_build_version prevents mixing versions (per Piotr's warning).
Yellow (top) is Python 2.7, compiled with MSC v.1500 (Visual Studio C++ 2008)
Red (bottom) is Python 3.4.1, compiled with MSC v.1600 (Visual Studio C++ 2010)
Use the table below[1] to match the internal VC++ version with the corresponding Visual Studio release:
MSC v.1000 -> Visual C++ 4.x
MSC v.1100 -> Visual C++ 5
MSC v.1200 -> Visual C++ 6
MSC v.1300 -> Visual C++ .NET
MSC v.1310 -> Visual C++ .NET 2003
MSC v.1400 -> Visual C++ 2005 (8.0)
MSC v.1500 -> Visual C++ 2008 (9.0)
MSC v.1600 -> Visual C++ 2010 (10.0)
MSC v.1700 -> Visual C++ 2012 (11.0)
MSC v.1800 -> Visual C++ 2013 (12.0)
MSC v.1900 -> Visual C++ 2015 (14.0)
MSC v.1910 -> Visual C++ 2017 (15.0)
MSC v.1911 -> Visual C++ 2017 (15.3)
MSC v.1912 -> Visual C++ 2017 (15.5)
MSC v.1913 -> Visual C++ 2017 (15.6)
MSC v.1914 -> Visual C++ 2017 (15.7)
MSC v.1915 -> Visual C++ 2017 (15.8)
MSC v.1916 -> Visual C++ 2017 (15.9)
Download and install the corresponding version of Visual Studio C++ from the previous step.
Additional notes for specific versions of VC++ are listed below.
Notes for Visual Studio C++ 2008
For only the 32-bit compilers, download Visual Studio C++ 2008 Express Edition.
For the 64-bit compilers[2][3], download Windows SDK for Windows 7 and .NET Framework 3.5 SP1.
Uncheck everything except Developer Tools >> Visual C++ Compilers to save time and disk space from installing SDK tools you otherwise don't need.
Notes for Visual Studio C++ 2010
According to Microsoft, if you installed Visual Studio 2010 SP1, it may have removed the compilers and libraries for VC++.
If that is the case, download Visual C++ 2010 SP1 Compiler Update.
Notes for Visual Studio C++ 2015
If you don't need the Visual Studio IDE, download Visual Studio C++ 2015 Build Tools.
Notes for Visual Studio C++ 2017
If you don't need the Visual Studio IDE, download Build Tools for Visual Studio 2017.
Suggestion: If you have both a 32- and 64-bit Python installation, you may also want to use virtualenv to create separate Python environments so you can use one or the other at a time without messing with your path to choose which Python version to use.
According to #srodriguex, you may be able to skip manually loading the batch file (Steps 4-6) by instead copying a few batch files to where Python is searching by following this answer. If that doesn't work, here are the following steps that originally worked for me.
Open up a cmd.exe
Before you try installing something which requires C extensions, run the following batch file to load the VC++ compiler's environment into the session (i.e. environment variables, the path to the compiler, etc).
Execute:
32-bit Compilers:
Note: 32-bit Windows installs will only have C:\Program Files\ as expected
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
64-bit Compilers:
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars64.bat"
Note: Yes, the native 64-bit compilers are in Program Files (x86). Don't ask me why.
Additionally, if you are wondering what the difference between vcvars64.bat and vcvarsx86_amd64.bat or more importantly the difference between amd64 and x86_amd64, the former are for the native 64-bit compiler tools and the latter are the 64-bit cross compilers that can run on a 32-bit Windows installation.
Update:
If for some reason you are getting error: ... was unexpected at this time. where the ... is some series of characters, then you need to check that you path variable does not have any extraneous characters like extra quotations or stray characters. The batch file is not going to be able to update your session path if it can't make sense of it in the first place.
If that went well, you should get one of the following messages depending on which version of VC++ and which command you ran:
For the 32-bit compiler tools:
Setting environment for using Microsoft Visual Studio 20xx x86 tools.
For the 64-bit compiler tools:
Setting environment for using Microsoft Visual Studio 20xx x64 tools.
Now, run the setup via python setup.py install or pip install pkg-name
Hope and cross your fingers that the planets are aligned correctly for VC++ to cooperate.
What's going on? Python modules can be part written in C or C++ (typically for speed). If you try to install such a package with Pip (or setup.py), it has to compile that C/C++ from source. Out the box, Pip will brazenly assume you the compiler Microsoft Visual C++ installed. If you don't have it, you'll see this cryptic error message "Error: Unable to find vcvarsall.bat".
The prescribed solution is to install a C/C++ compiler, either Microsoft Visual C++, or MinGW (an open-source project). However, installing and configuring either is prohibitively difficult. (Edit 2014: Microsoft have published a special C++ compiler for Python 2.7)
The easiest solution is to use Christoph Gohlke's Windows installers (.msi) for popular Python packages. He builds installers for Python 2.x and 3.x, 32 bit and 64 bit. You can download them from http://www.lfd.uci.edu/~gohlke/pythonlibs/
If you too think "Error: Unable to find vcvarsall.bat" is a ludicrously cryptic and unhelpful message, then please comment on the bug at http://bugs.python.org/issue2943 to replace it with a more helpful and user-friendly message.
For comparison, Ruby ships with a package manager Gem and offers a quasi-official C/C++ compiler, DevKit. If you try to install a package without it, you see this helpful friendly useful message:
Please update your PATH to include build tools or download the DevKit from http://rubyinstaller.org/downloads and follow the instructions at http://github.com/oneclick/rubyinstaller/wiki/Development-Kit
You can read a longer rant about Python packaging at https://stackoverflow.com/a/13445719/284795
You'll need to install a Microsoft compiler, compatible with the compiler used to build Python. This means you need Visual C++ 2008 (or newer, with some tweaking).
Microsoft now supplies a bundled compiler and headers just to be able to compile Python extensions, at the memorable URL:
Microsoft Visual C++ Compiler for Python 2.7
http://aka.ms/vcpython27
This is a relatively small package; 85MB to download, installable without admin privileges, no reboot required. The name is a little misleading, the compiler will work for any Python version originally compiled with Visual C++ 2008, not just Python 2.7.
If you start a Python interactive prompt or print sys.version, look for the MSC version string; if it is MSC v.1500 you can use this tool.
From the original announcement to the distutils list:
Microsoft has released a compiler package for Python 2.7 to make it easier for people to build and distribute their C extension modules on Windows. The Microsoft Visual C++ Compiler for Python 2.7 (a.k.a. VC9) is available from: http://aka.ms/vcpython27
This package contains all the tools and headers required to build C extension modules for Python 2.7 32-bit and 64-bit (note that some extension modules require 3rd party dependencies such as OpenSSL or libxml2 that are not included). Other versions of Python built with Visual C++ 2008 are also supported, so "Python 2.7" is just advertising - it'll work fine with 2.6 and 3.2.
Note that you need to have setuptools 6.0 or newer installed (listed in the system requirements on the download page). The project you are installing must use setuptools.setup(), not distutils or the auto-detection won't work.
Microsoft has stated that they want to keep the URL stable, so that automated scripts can reference it easily.
I just had this same problem, so I'll tell my story here hoping it helps someone else with the same issues and save them the couple of hours I just spent:
I have mingw (g++ (GCC) 4.6.1) and python 2.7.3 in a windows 7 box and I'm trying to install PyCrypto.
It all started with this error when running setup.py install:
error: Unable to find vcvarsall.bat
Easily solved after googling the error by specifying mingw as the compiler of choice:
setup.py install build --compiler=mingw32
The problem is that then I got a different error:
configure: error: cannot run C compiled programs.
It turns out that my anti-virus was blocking the execution of a freshly compiled .exe. I just disabled the anti-virus "resident shield" and went to the next error:
cc1.exe: error: unrecognized command line option '-mno-cygwin'
error: command 'gcc' failed with exit status 1
This solved it: "Either install a slightly older version of MinGW, or edit distutils\cygwinccompiler.py in your Python directory to remove all instances of -mno-cygwin." (from here)
Now, I can finally start working.
Looks like its looking for VC compilers, so you could try to mention compiler type with -c mingw32, since you have msys
python setup.py install -c mingw32
I have python 2.73 and windows 7 .The solution that worked for me was:
Added mingw32's bin directory to environment variable: append PATH with C:\programs\mingw\bin;
Created distutils.cfg located at C:\Python27\Lib\distutils\distutils.cfg containing:
[build]
compiler=mingw32
To deal with MinGW not recognizing the -mno-cygwin flag anymore, remove the flag in C:\Python27\Lib\distutils\cygwincompiler.py line 322 to 326, so it looks like this:
self.set_executables(compiler='gcc -O -Wall',
compiler_so='gcc -mdll -O -Wall',
compiler_cxx='g++ -O -Wall',
linker_exe='gcc',
linker_so='%s %s %s'
% (self.linker_dll, shared_option,
entry_point))
Look in the setup.py file of the package you are trying to install. If it is an older package it may be importing distutils.core.setup() rather than setuptools.setup().
I ran in to this (in 2015) with a combination of these factors:
The Microsoft Visual C++ Compiler for Python 2.7 from http://aka.ms/vcpython27
An older package that uses distutils.core.setup()
Trying to do python setup.py build rather than using pip.
If you use a recent version of pip, it will force (monkeypatch) the package to use setuptools, even if its setup.py calls for distutils. However, if you are not using pip, and instead are just doing python setup.py build, the build process will use distutils.core.setup(), which does not know about the compiler install location.
Solution
Step 1: Open the appropriate Visual C++ 2008 Command Prompt
Open the Start menu or Start screen, and search for "Visual C++ 2008 32-bit Command Prompt" (if your python is 32-bit) or "Visual C++ 2008 64-bit Command Prompt" (if your python is 64-bit). Run it. The command prompt should say Visual C++ 2008 ... in the title bar.
Step 2: Set environment variables
Set these environment variables in the command prompt you just opened.
SET DISTUTILS_USE_SDK=1
SET MSSdk=1
Reference http://bugs.python.org/issue23246
Step 3: Build and install
cd to the package you want to build, and run python setup.py build, then python setup.py install. If you want to install in to a virtualenv, activate it before you build.
Maybe somebody can be interested, the following worked for me for the py2exe package.
(I have windows 7 64 bit and portable python 2.7, Visual Studio 2005 Express with Windows SDK for Windows 7 and .NET Framework 4)
set VS90COMNTOOLS=%VS80COMNTOOLS%
then:
python.exe setup.py install
I spent almost 2 days figuring out how to fix this problem in my python 3.4 64 bit version: Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Solution 1, hard: (before reading this, read first Solution 2 below)
Finally, this is what helped me:
install Visual C++ 2010 Express
install Microsoft Windows SDK v7.1 for Windows 7
create manually file vcvars64.bat in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64 which contains CALL "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64 or other path depending on where you have yours installed
(this seems to be optional) install Microsoft Visual Studio 2010 Service Pack 1 together with Microsoft Visual C++ 2010 Service Pack 1 Compiler Update for the Windows SDK 7.1
after that I tried to pip install numpy but received the following error:
File "numpy\core\setup.py", line 686, in get_mathlib_info
raise RuntimeError("Broken toolchain: cannot link a simple C program")
RuntimeError: Broken toolchain: cannot link a simple C program
I changed mfinfo to None in C:\Python34\Lib\distutils\msvc9compiler.py per this https://stackoverflow.com/a/23099820/4383472
finally after pip install numpy command my avast antivirus tried to interfere into the installation process, but i quickly disabled it
It took very long - several minutes for numpy to compile, I even thought that there was an error, but finally everything was ok.
Solution 2, easy:
(I know this approach has already been mentioned in a highly voted answer, but let me repeat since it really is easier)
After going through all of this work I understood that the best way for me is just to use already precompiled binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/ in future. There is very small chance that I will ever need some package (or a version of a package) which this site doesn't contain. The installation process is also much quicker this way. For example, to install numpy:
donwload numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl (if you have Python 3.4 64-bit) from that site
in command prompt or powershell install it with pip pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl (or full path to the file depending how command prompt is opened)
I wanted to run pysph on Windows 10 under Python 2.7 and got vcvarsall.bat was not found (from distutils)
My solution was the following:
Install Microsoft Visual C++ for Python 2.7 (like #Michael suggested)
On Windows 10 it was installed into (my username is Andreas):
C:\Users\Andreas\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0
Set environment variable VS90COMNTOOLS to the installation path of Visual C++ for Python 2.7 (see above path).
If it still doesn't work, then modifiy in the module
C:/Python27/lib/distutils
the file msvc9compiler.py. Find in it the function find_vcvarsall and do following modification.
Replace the line:
productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
with
productdir = os.path.join(toolsdir)
This is where vcvarsall.bat resides in my case (check, where vcvarsall.bat is in your installation).
I encountered this issue when I tried to install numpy library on my python 3.5. The solution is to install VS2015. I had VS2008, 2012, 2013, none of which is compatible with python 3.5. Apparently newer version of python has dependency on newer versions of VS.
Also make sure C++ Common Tools are installed with Visual Studio.
Use this link to download and install Visual C++ 2015 Build Tools. It will automatically download visualcppbuildtools_full.exe and install Visual C++ 14.0 without actually installing Visual Studio. After the installation completes, retry pip install and you won't get the error again.
I have tested it on following platform and versions:
Python 3.6 on Windows 7 64-bit
Python 3.7 on Windows Server 2016 (64-bit system)
Python 3.8 on Windows 10 64-bit
I tried all the above answers, and found all of them not to work, this was perhaps I was using Windows 8 and had installed Visual Studio 2012. In this case, this is what you do.
The vcvarsall.bat file is located here:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC
Simply select the file, and copy it.
Then go to this directory:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools
and paste the file. And then, all should be well.
You can download the free Visual C++ 2008 Express Edition from http://go.microsoft.com/?linkid=7729279, which will set the VS90COMNTOOLS environment variable during installation and therefore build with a compatible compiler.
As #PiotrDobrogost mentioned in a comment, his answer to this other question goes into details about why Visual C++ 2008 is the right thing to build with, but this can change as the Windows build of Python moves to newer versions of Visual Studio: Building lxml for Python 2.7 on Windows
I had this problem using Python 3.4.1 on Windows 7 x64, and unfortunately the packages I needed didn't have suitable exe or wheels that I could use. This system requires a few 'workarounds', which are detailed below (and TLDR at bottom).
Using the info in Jaxrtech's answer above, I determined I needed Visual Studio C++ 2010 (sys.version return MSC v.1600), so I installed Visual C++ 2010 Express from the link in his answer, which is http://go.microsoft.com/?linkid=9709949. I installed everything with updates, but as you can read below, this was a mistake. Only the original version of Express should be installed at this time (no updated anything).
vcvarsall.bat was now present, but there was a new error when installing the package, query_vcvarsall raise ValueError(str(list(result.keys())))ValueError: [u'path']. There are other stackoverflow questions with this error, such as Errors while building/installing C module for Python 2.7
I determined from that answer that 2010 Express only installs 32-bit compilers. To get 64-bit (and other) compilers, you need to install Windows 7.1 SDK. See http://msdn.microsoft.com/en-us/windowsserver/bb980924.aspx
This would not install for me though, and the installer returned the error installation failed with return code 5100. I found the solution at the following link: http://support.microsoft.com/kb/2717426. In short, if newer versions of x86 and x64 Microsoft Visual C++ 2010 Redistributable's are installed, they conflict with the ones in SDK installer, and need uninstalling first.
The SDK then installed, but I noticed vcvars64.bat still did not exist in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin, nor its subfolders. vcvarsall.bat runs the vcvars64 batch file, so without it, the python package still wouldn't install (I forgot the error that was shown at this time).
I then found some instructions here: http://www.cryptohaze.com/wiki/index.php/Windows_7_Build_Setup#Download_VS_2010_and_Windows_SDK_7.1
Following the instructions, I had already installed Express and 7.1 SDK, so installed SDK 7.1 SP1, and did the missing header file fix. I then manually created vcvars64.bat with the content CALL setenv /x64. I will paste all those instructions here, so they don't get lost.
Step 1 is to download Visual Studio Express 2010.
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express
is a good place to start. Download the installer, and run it
(vc_web.exe). You don't need the SQL 2008 additional download.
You'll also need the Windows SDK (currently 7.1) for the 64-bit
compilers - unless you want to do 32-bit only builds, which are not
fully supported...
http://www.microsoft.com/en-us/download/details.aspx?id=8279 is a good
starting point to download this - you'll want to run winsdk_web.exe
when downloaded!
The default install here is just fine.
Finally, download and install the Windows SDK 7.1 SP1 update:
http://www.microsoft.com/en-us/download/details.aspx?id=4422
And, to fix missing header file, VS2010 SP1.
http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=75568aa6-8107-475d-948a-ef22627e57a5
And, bloody hell, fix the missing batch file for VS2010 Express. This
is getting downright absurd.
In C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64,
create "vcvars64.bat" with the following (you will need to be running
as administrator):
CALL setenv /x64
My python package still did not install (can't recall error). I then found some instructions (copied below) to use the special SDK 7.1 Command Prompt, see: https://mail.python.org/pipermail/distutils-sig/2012-February/018300.html
Never mind this question. Somebody here noticed this item on the menu: Start->All Programs->Microsoft Windows SDK v7.1 ->Windows SDK 7.1 Command Prompt
This runs a batch job that appears to set up a working environment for the compiler. From that prompt, you can type "setup.py build" or "setup.py install".
I opened the Windows SDK 7.1 Command Prompt as instructed, and used it to run easy_install on the python package. And at last, success!
TLDR;
Install Visual Studio Express 2010 (preferably without updated redistributables or SQL server).
Install Windows 7.1 SDK
Instal SDK 7.1 SP1 update, and VS2010 SP1 header file fix (this step may not be required).
Manually create C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\vcvars64.bat with content CALL setenv /x64
Start->All Programs->Microsoft Windows SDK v7.1 ->Windows SDK 7.1 Command Prompt to open special x64 command prompt, which can then be used with python/easy_install/pip/etc (including those in virtual_envs).
Below steps fixed this issue for me, I was trying to create setup with cython extension.
Install Microsoft Visual C++ Compiler for Python 2.7
The default install location would be #
C:\Users\PC-user\AppData\Local\Programs\Common\Microsoft\Visual C++
for Python This might actually fix the issue, test once before proceeding.
If it fails, Check where in VC++
for python vcvarsall.bat file is located
Open the msvc9compiler.py
file of distutils package in notepad.
In my box this was #
C:\Anaconda2\Lib\distutils\msvc9compiler.py find_vcvarsall function
in this file, determine the version of VC by printing out version
argument. For Python 2.7 it's likely to be 9.0
Now create an
environment variable VS90COMNTOOLS, Pointing to
C:\Users\PC-user\AppData\Local\Programs\Common\Microsoft\Visual C++
for Python\9.0\VC\bin
For some reason distutils expects the
vcvarsall.bat file to be within VC dir, but VC++ for python tools
has it in the root of 9.0 To fix this, remove "VC" from the
path.join (roughly around line 247)
#productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
productdir = os.path.join(toolsdir, os.pardir, os.pardir)
The above steps fixed the issue for me.
calling import setuptools will monkey patch distutils to force compatibility with Visual Studio. Calling vcvars32.bat manually will setup the virtual environment and prevent other common errors the compiler will throw. For VS 2017 the file is located at
"C:\Program Files (x86)\Microsoft Visual
Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"
Here is the setup script I use to quickly compile .pyx files to .pyd:
(Note: it uses the 3rd party module
send2trash
# cython_setup.py
import sys, os, time, platform, subprocess
from setuptools import setup, find_packages
from Cython.Build import cythonize
from traceback import format_exc
# USAGE:
#
# from cython_setup import run
# run(pyx_path)
# vcvars = r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"
# NOTE: to use visual studio 2017 you must have setuptools version 34+
vcvars = r"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars32.bat"
def _build_ext():
try:
pyx_path = sys.argv.pop(-1)
pyx_path = os.path.abspath(pyx_path)
if not os.path.exists(pyx_path):
raise FileNotFoundError(f"{pyx_path} does not exist")
project_name = sys.argv.pop(-1)
os.chdir(os.path.abspath(os.path.dirname(pyx_path)))
print("cwd: %s" % os.getcwd())
print(os.path.abspath("build"))
setup(
name=project_name,
# cmdclass = {'build_ext': build_ext},
packages=find_packages(),
# ext_modules=cythonize(extensions)
ext_modules=cythonize(pyx_path,
compiler_directives={'language_level': 3, 'infer_types': True, 'binding': False},
annotate=True),
# include_dirs = [numpy.get_include()]
build_dir=os.path.abspath("build")
)
except:
input(format_exc())
def retry(func):
def wrapper(*args, **kw):
tries = 0
while True:
try:
return func(*args, **kw)
except Exception:
tries += 1
if tries > 4:
raise
time.sleep(0.4)
return wrapper
#retry
def cleanup(pyx_path):
from send2trash import send2trash
c_file = os.path.splitext(pyx_path)[0] + ".c"
if os.path.exists(c_file):
os.remove(c_file)
if os.path.exists("build"):
send2trash("build")
def move_pyd_files(pyx_path):
pyx_dir = os.path.dirname(pyx_path)
build_dir = os.path.join(pyx_dir, "build")
if not os.path.exists(build_dir):
raise RuntimeError(f"build_dir {build_dir} did not exist....")
found_pyd = False
for top, dirs, nondirs in os.walk(build_dir):
for name in nondirs:
if name.lower().endswith(".pyd") or name.lower().endswith(".so"):
found_pyd = True
old_path = os.path.join(top, name)
new_path = os.path.join(pyx_dir, name)
if os.path.exists(new_path):
print(f"removing {new_path}")
os.remove(new_path)
print(f"file created at {new_path}")
os.rename(old_path, new_path)
if not found_pyd:
raise RuntimeError("Never found .pyd file to move")
def run(pyx_path):
"""
:param pyx_path:
:type pyx_path:
:return: this function creates the batch file, which in turn calls this module, which calls cythonize, once done
the batch script deletes itself... I'm sure theres a less convoluted way of doing this, but it works
:rtype:
"""
try:
project_name = os.path.splitext(os.path.basename(pyx_path))[0]
run_script(project_name, os.path.abspath(pyx_path))
except:
input(format_exc())
def run_script(project_name, pyx_path):
dirname = os.path.dirname(pyx_path)
# ------------------------------
os.chdir(dirname)
if os.path.exists(vcvars):
# raise RuntimeError(
# f"Could not find vcvars32.bat at {vcvars}\nis Visual Studio Installed?\nIs setuptools version > 34?")
subprocess.check_call(f'call "{vcvars}"', shell=True)
cmd = "python" if platform.system() == "Windows" else "python3"
subprocess.check_call(f'{cmd} "{__file__}" build_ext "{project_name}" "{pyx_path}"', shell=True)
move_pyd_files(pyx_path)
cleanup(pyx_path)
if len(sys.argv) > 2:
_build_ext()
I tried many solutions but only one worked for me, the install of Microsoft Visual Studio 2008 Express C++.
I got this issue with a Python 2.7 module written in C (yEnc, which has other issues with MS VS). Note that Python 2.7 is built with MS VS 2008 version, not 2010!
Despite the fact it's free, it is quite hard to find since MS is promoting VS 2010.
Still, the MSDN official very direct links are still working: check https://stackoverflow.com/a/15319069/2227298 for download links.
If you have mingw installed
pip install --global-option build_ext --global-option --compiler=mingw32 packagename
works, forcing pip to build using the mingw compiler instead of Microsoft's. See here https://github.com/pypa/pip/issues/18 for details (last post).
Is Microsoft Visual C++ Compiler for Python 2.7 at http://www.microsoft.com/en-us/download/details.aspx?id=44266 not a solution?
The easiest way to solve this in 2016 is to install Chocolatey and then the vcpython27 package. Open Powershell:
> iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
> choco install python2 -y
> choco install vcpython27 -y
I don't know if it is too late, but I found Microsoft Visual C++ Compiler for Python 2.7 which reads
The typical error message you will receive if you need this compiler package is Unable to find vcvarsall.bat
Hope this helps!
An exhaustive list of MS VС++ versions and installation variations officially supported by distutils (and some by setuptools) and how to use them can be found at
https://wiki.python.org/moin/WindowsCompilers
It also specifies which VC++ version is required for which official Win32 Python release. Note that MinGW is not officially supported (see below for details).
In brief:
For each release, there's a specific Visual Studio release that works out of the box.
Note that Express editions before 2012 do not support x64. Trying to use one for x64 results in an obscure error.
For standalone SDKs, it's generally required to run the installation from the SDK prompt (=set environment variables).
For some, this requirement can be lifted by a one-time tweaking which doesn't have adversal side effects
Some unusual VC++ releases are supported by setuptools and not distutils.
MinGW is said to "work", but only for x86. https://bugs.python.org/issue4709 says that problems with its support lie in MinGW itself and even x86 is bound to have problems since Python 3.5.
I got the same problem and have solved it at the moment.
"Google" told me that I need to install "Microsoft Visual C++ Compiler for Python 2.7". I install not only the tool, but also Visual C++ 2008 Reditributable, but it didn't help. I then tried to install Visual C++ 2008 Express Edition. And the problem has gone!
Just try to install Visual C++ 2008 Express Edition!
If you're looking to install pyodbc on a Windows box that doesn't have Visual Studio installed another option is to manually install pyodbc using the binary distribution.
This is particularly useful if you do not have administrator privileges on the machine you're working with and are trying to set up a virtualenv.
Steps:
Download the latest Windows installer from here (pyodbc-X.X.X.win-Y-py2.7.exe)
Open the installer executable using 7-Zip (or WinRAR or whatever)
Extract pyodbc.pyd and pyodbc-X.X.X-py2.7.egg-info and place them in [python installation directory or virtualenv]\Lib\site-packages
There is no step 4 :)
With Python 3.4, the dependency is on Visual Studio 2010. Installing Visual C++ 2010 Express fixed the problem for me.
Tricking it into using the VS 2008 or 2013 installs that I happened to have didn't work.
You can use easy_install instead of pip it works for me.

python error:unable to find vcvarsall.bat while installing sklearn windows 8 (installed python2.7 32bit installer and packages(numpy,scipy,etc)) [duplicate]

I tried to install the Python package dulwich:
pip install dulwich
But I get a cryptic error message:
error: Unable to find vcvarsall.bat
The same happens if I try installing the package manually:
> python setup.py install
running build_ext
building 'dulwich._objects' extension
error: Unable to find vcvarsall.bat
Update: Comments point out that the instructions here may be dangerous. Consider using the Visual C++ 2008 Express edition or the purpose-built Microsoft Visual C++ Compiler for Python (details) and NOT using the original answer below. Original error message means the required version of Visual C++ is not installed.
For Windows installations:
While running setup.py for package installations, Python 2.7 searches for an installed Visual Studio 2008. You can trick Python to use a newer Visual Studio by setting the correct path in VS90COMNTOOLS environment variable before calling setup.py.
Execute the following command based on the version of Visual Studio installed:
Visual Studio 2010 (VS10): SET VS90COMNTOOLS=%VS100COMNTOOLS%
Visual Studio 2012 (VS11): SET VS90COMNTOOLS=%VS110COMNTOOLS%
Visual Studio 2013 (VS12): SET VS90COMNTOOLS=%VS120COMNTOOLS%
Visual Studio 2015 (VS14): SET VS90COMNTOOLS=%VS140COMNTOOLS%
WARNING: As noted below, this answer is unlikely to work if you are trying to compile python modules.
See Building lxml for Python 2.7 on Windows for details.
I found the solution.
I had the exact same problem, and error, installing 'amara'. I had mingw32 installed, but distutils needed to be configured.
I have Python 2.6 that was already installed.
I installed mingw32 to C:\programs\mingw\
Add mingw32's bin directory to your environment variable: append c:\programs\MinGW\bin; to the PATH
Edit (create if not existing) distutils.cfg file located at C:\Python26\Lib\distutils\distutils.cfg to be:
[build]
compiler=mingw32
Now run easy_install.exe amara.
Make sure environment is set by opening a new cmd.exe.
You can install compiled version from http://www.lfd.uci.edu/~gohlke/pythonlibs/
If you want to compile with Visual Studio C++ instead of mingw...
Run python.exe to display which version of VC++ it was compiled with (example shown below).
It is important to use the corresponding version of the Visual C++ compiler that Python was compiled with since distilutils's get_build_version prevents mixing versions (per Piotr's warning).
Yellow (top) is Python 2.7, compiled with MSC v.1500 (Visual Studio C++ 2008)
Red (bottom) is Python 3.4.1, compiled with MSC v.1600 (Visual Studio C++ 2010)
Use the table below[1] to match the internal VC++ version with the corresponding Visual Studio release:
MSC v.1000 -> Visual C++ 4.x
MSC v.1100 -> Visual C++ 5
MSC v.1200 -> Visual C++ 6
MSC v.1300 -> Visual C++ .NET
MSC v.1310 -> Visual C++ .NET 2003
MSC v.1400 -> Visual C++ 2005 (8.0)
MSC v.1500 -> Visual C++ 2008 (9.0)
MSC v.1600 -> Visual C++ 2010 (10.0)
MSC v.1700 -> Visual C++ 2012 (11.0)
MSC v.1800 -> Visual C++ 2013 (12.0)
MSC v.1900 -> Visual C++ 2015 (14.0)
MSC v.1910 -> Visual C++ 2017 (15.0)
MSC v.1911 -> Visual C++ 2017 (15.3)
MSC v.1912 -> Visual C++ 2017 (15.5)
MSC v.1913 -> Visual C++ 2017 (15.6)
MSC v.1914 -> Visual C++ 2017 (15.7)
MSC v.1915 -> Visual C++ 2017 (15.8)
MSC v.1916 -> Visual C++ 2017 (15.9)
Download and install the corresponding version of Visual Studio C++ from the previous step.
Additional notes for specific versions of VC++ are listed below.
Notes for Visual Studio C++ 2008
For only the 32-bit compilers, download Visual Studio C++ 2008 Express Edition.
For the 64-bit compilers[2][3], download Windows SDK for Windows 7 and .NET Framework 3.5 SP1.
Uncheck everything except Developer Tools >> Visual C++ Compilers to save time and disk space from installing SDK tools you otherwise don't need.
Notes for Visual Studio C++ 2010
According to Microsoft, if you installed Visual Studio 2010 SP1, it may have removed the compilers and libraries for VC++.
If that is the case, download Visual C++ 2010 SP1 Compiler Update.
Notes for Visual Studio C++ 2015
If you don't need the Visual Studio IDE, download Visual Studio C++ 2015 Build Tools.
Notes for Visual Studio C++ 2017
If you don't need the Visual Studio IDE, download Build Tools for Visual Studio 2017.
Suggestion: If you have both a 32- and 64-bit Python installation, you may also want to use virtualenv to create separate Python environments so you can use one or the other at a time without messing with your path to choose which Python version to use.
According to #srodriguex, you may be able to skip manually loading the batch file (Steps 4-6) by instead copying a few batch files to where Python is searching by following this answer. If that doesn't work, here are the following steps that originally worked for me.
Open up a cmd.exe
Before you try installing something which requires C extensions, run the following batch file to load the VC++ compiler's environment into the session (i.e. environment variables, the path to the compiler, etc).
Execute:
32-bit Compilers:
Note: 32-bit Windows installs will only have C:\Program Files\ as expected
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
64-bit Compilers:
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars64.bat"
Note: Yes, the native 64-bit compilers are in Program Files (x86). Don't ask me why.
Additionally, if you are wondering what the difference between vcvars64.bat and vcvarsx86_amd64.bat or more importantly the difference between amd64 and x86_amd64, the former are for the native 64-bit compiler tools and the latter are the 64-bit cross compilers that can run on a 32-bit Windows installation.
Update:
If for some reason you are getting error: ... was unexpected at this time. where the ... is some series of characters, then you need to check that you path variable does not have any extraneous characters like extra quotations or stray characters. The batch file is not going to be able to update your session path if it can't make sense of it in the first place.
If that went well, you should get one of the following messages depending on which version of VC++ and which command you ran:
For the 32-bit compiler tools:
Setting environment for using Microsoft Visual Studio 20xx x86 tools.
For the 64-bit compiler tools:
Setting environment for using Microsoft Visual Studio 20xx x64 tools.
Now, run the setup via python setup.py install or pip install pkg-name
Hope and cross your fingers that the planets are aligned correctly for VC++ to cooperate.
What's going on? Python modules can be part written in C or C++ (typically for speed). If you try to install such a package with Pip (or setup.py), it has to compile that C/C++ from source. Out the box, Pip will brazenly assume you the compiler Microsoft Visual C++ installed. If you don't have it, you'll see this cryptic error message "Error: Unable to find vcvarsall.bat".
The prescribed solution is to install a C/C++ compiler, either Microsoft Visual C++, or MinGW (an open-source project). However, installing and configuring either is prohibitively difficult. (Edit 2014: Microsoft have published a special C++ compiler for Python 2.7)
The easiest solution is to use Christoph Gohlke's Windows installers (.msi) for popular Python packages. He builds installers for Python 2.x and 3.x, 32 bit and 64 bit. You can download them from http://www.lfd.uci.edu/~gohlke/pythonlibs/
If you too think "Error: Unable to find vcvarsall.bat" is a ludicrously cryptic and unhelpful message, then please comment on the bug at http://bugs.python.org/issue2943 to replace it with a more helpful and user-friendly message.
For comparison, Ruby ships with a package manager Gem and offers a quasi-official C/C++ compiler, DevKit. If you try to install a package without it, you see this helpful friendly useful message:
Please update your PATH to include build tools or download the DevKit from http://rubyinstaller.org/downloads and follow the instructions at http://github.com/oneclick/rubyinstaller/wiki/Development-Kit
You can read a longer rant about Python packaging at https://stackoverflow.com/a/13445719/284795
You'll need to install a Microsoft compiler, compatible with the compiler used to build Python. This means you need Visual C++ 2008 (or newer, with some tweaking).
Microsoft now supplies a bundled compiler and headers just to be able to compile Python extensions, at the memorable URL:
Microsoft Visual C++ Compiler for Python 2.7
http://aka.ms/vcpython27
This is a relatively small package; 85MB to download, installable without admin privileges, no reboot required. The name is a little misleading, the compiler will work for any Python version originally compiled with Visual C++ 2008, not just Python 2.7.
If you start a Python interactive prompt or print sys.version, look for the MSC version string; if it is MSC v.1500 you can use this tool.
From the original announcement to the distutils list:
Microsoft has released a compiler package for Python 2.7 to make it easier for people to build and distribute their C extension modules on Windows. The Microsoft Visual C++ Compiler for Python 2.7 (a.k.a. VC9) is available from: http://aka.ms/vcpython27
This package contains all the tools and headers required to build C extension modules for Python 2.7 32-bit and 64-bit (note that some extension modules require 3rd party dependencies such as OpenSSL or libxml2 that are not included). Other versions of Python built with Visual C++ 2008 are also supported, so "Python 2.7" is just advertising - it'll work fine with 2.6 and 3.2.
Note that you need to have setuptools 6.0 or newer installed (listed in the system requirements on the download page). The project you are installing must use setuptools.setup(), not distutils or the auto-detection won't work.
Microsoft has stated that they want to keep the URL stable, so that automated scripts can reference it easily.
I just had this same problem, so I'll tell my story here hoping it helps someone else with the same issues and save them the couple of hours I just spent:
I have mingw (g++ (GCC) 4.6.1) and python 2.7.3 in a windows 7 box and I'm trying to install PyCrypto.
It all started with this error when running setup.py install:
error: Unable to find vcvarsall.bat
Easily solved after googling the error by specifying mingw as the compiler of choice:
setup.py install build --compiler=mingw32
The problem is that then I got a different error:
configure: error: cannot run C compiled programs.
It turns out that my anti-virus was blocking the execution of a freshly compiled .exe. I just disabled the anti-virus "resident shield" and went to the next error:
cc1.exe: error: unrecognized command line option '-mno-cygwin'
error: command 'gcc' failed with exit status 1
This solved it: "Either install a slightly older version of MinGW, or edit distutils\cygwinccompiler.py in your Python directory to remove all instances of -mno-cygwin." (from here)
Now, I can finally start working.
Looks like its looking for VC compilers, so you could try to mention compiler type with -c mingw32, since you have msys
python setup.py install -c mingw32
I have python 2.73 and windows 7 .The solution that worked for me was:
Added mingw32's bin directory to environment variable: append PATH with C:\programs\mingw\bin;
Created distutils.cfg located at C:\Python27\Lib\distutils\distutils.cfg containing:
[build]
compiler=mingw32
To deal with MinGW not recognizing the -mno-cygwin flag anymore, remove the flag in C:\Python27\Lib\distutils\cygwincompiler.py line 322 to 326, so it looks like this:
self.set_executables(compiler='gcc -O -Wall',
compiler_so='gcc -mdll -O -Wall',
compiler_cxx='g++ -O -Wall',
linker_exe='gcc',
linker_so='%s %s %s'
% (self.linker_dll, shared_option,
entry_point))
Look in the setup.py file of the package you are trying to install. If it is an older package it may be importing distutils.core.setup() rather than setuptools.setup().
I ran in to this (in 2015) with a combination of these factors:
The Microsoft Visual C++ Compiler for Python 2.7 from http://aka.ms/vcpython27
An older package that uses distutils.core.setup()
Trying to do python setup.py build rather than using pip.
If you use a recent version of pip, it will force (monkeypatch) the package to use setuptools, even if its setup.py calls for distutils. However, if you are not using pip, and instead are just doing python setup.py build, the build process will use distutils.core.setup(), which does not know about the compiler install location.
Solution
Step 1: Open the appropriate Visual C++ 2008 Command Prompt
Open the Start menu or Start screen, and search for "Visual C++ 2008 32-bit Command Prompt" (if your python is 32-bit) or "Visual C++ 2008 64-bit Command Prompt" (if your python is 64-bit). Run it. The command prompt should say Visual C++ 2008 ... in the title bar.
Step 2: Set environment variables
Set these environment variables in the command prompt you just opened.
SET DISTUTILS_USE_SDK=1
SET MSSdk=1
Reference http://bugs.python.org/issue23246
Step 3: Build and install
cd to the package you want to build, and run python setup.py build, then python setup.py install. If you want to install in to a virtualenv, activate it before you build.
Maybe somebody can be interested, the following worked for me for the py2exe package.
(I have windows 7 64 bit and portable python 2.7, Visual Studio 2005 Express with Windows SDK for Windows 7 and .NET Framework 4)
set VS90COMNTOOLS=%VS80COMNTOOLS%
then:
python.exe setup.py install
I spent almost 2 days figuring out how to fix this problem in my python 3.4 64 bit version: Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Solution 1, hard: (before reading this, read first Solution 2 below)
Finally, this is what helped me:
install Visual C++ 2010 Express
install Microsoft Windows SDK v7.1 for Windows 7
create manually file vcvars64.bat in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64 which contains CALL "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64 or other path depending on where you have yours installed
(this seems to be optional) install Microsoft Visual Studio 2010 Service Pack 1 together with Microsoft Visual C++ 2010 Service Pack 1 Compiler Update for the Windows SDK 7.1
after that I tried to pip install numpy but received the following error:
File "numpy\core\setup.py", line 686, in get_mathlib_info
raise RuntimeError("Broken toolchain: cannot link a simple C program")
RuntimeError: Broken toolchain: cannot link a simple C program
I changed mfinfo to None in C:\Python34\Lib\distutils\msvc9compiler.py per this https://stackoverflow.com/a/23099820/4383472
finally after pip install numpy command my avast antivirus tried to interfere into the installation process, but i quickly disabled it
It took very long - several minutes for numpy to compile, I even thought that there was an error, but finally everything was ok.
Solution 2, easy:
(I know this approach has already been mentioned in a highly voted answer, but let me repeat since it really is easier)
After going through all of this work I understood that the best way for me is just to use already precompiled binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/ in future. There is very small chance that I will ever need some package (or a version of a package) which this site doesn't contain. The installation process is also much quicker this way. For example, to install numpy:
donwload numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl (if you have Python 3.4 64-bit) from that site
in command prompt or powershell install it with pip pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl (or full path to the file depending how command prompt is opened)
I wanted to run pysph on Windows 10 under Python 2.7 and got vcvarsall.bat was not found (from distutils)
My solution was the following:
Install Microsoft Visual C++ for Python 2.7 (like #Michael suggested)
On Windows 10 it was installed into (my username is Andreas):
C:\Users\Andreas\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0
Set environment variable VS90COMNTOOLS to the installation path of Visual C++ for Python 2.7 (see above path).
If it still doesn't work, then modifiy in the module
C:/Python27/lib/distutils
the file msvc9compiler.py. Find in it the function find_vcvarsall and do following modification.
Replace the line:
productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
with
productdir = os.path.join(toolsdir)
This is where vcvarsall.bat resides in my case (check, where vcvarsall.bat is in your installation).
I encountered this issue when I tried to install numpy library on my python 3.5. The solution is to install VS2015. I had VS2008, 2012, 2013, none of which is compatible with python 3.5. Apparently newer version of python has dependency on newer versions of VS.
Also make sure C++ Common Tools are installed with Visual Studio.
Use this link to download and install Visual C++ 2015 Build Tools. It will automatically download visualcppbuildtools_full.exe and install Visual C++ 14.0 without actually installing Visual Studio. After the installation completes, retry pip install and you won't get the error again.
I have tested it on following platform and versions:
Python 3.6 on Windows 7 64-bit
Python 3.7 on Windows Server 2016 (64-bit system)
Python 3.8 on Windows 10 64-bit
I tried all the above answers, and found all of them not to work, this was perhaps I was using Windows 8 and had installed Visual Studio 2012. In this case, this is what you do.
The vcvarsall.bat file is located here:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC
Simply select the file, and copy it.
Then go to this directory:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools
and paste the file. And then, all should be well.
You can download the free Visual C++ 2008 Express Edition from http://go.microsoft.com/?linkid=7729279, which will set the VS90COMNTOOLS environment variable during installation and therefore build with a compatible compiler.
As #PiotrDobrogost mentioned in a comment, his answer to this other question goes into details about why Visual C++ 2008 is the right thing to build with, but this can change as the Windows build of Python moves to newer versions of Visual Studio: Building lxml for Python 2.7 on Windows
I had this problem using Python 3.4.1 on Windows 7 x64, and unfortunately the packages I needed didn't have suitable exe or wheels that I could use. This system requires a few 'workarounds', which are detailed below (and TLDR at bottom).
Using the info in Jaxrtech's answer above, I determined I needed Visual Studio C++ 2010 (sys.version return MSC v.1600), so I installed Visual C++ 2010 Express from the link in his answer, which is http://go.microsoft.com/?linkid=9709949. I installed everything with updates, but as you can read below, this was a mistake. Only the original version of Express should be installed at this time (no updated anything).
vcvarsall.bat was now present, but there was a new error when installing the package, query_vcvarsall raise ValueError(str(list(result.keys())))ValueError: [u'path']. There are other stackoverflow questions with this error, such as Errors while building/installing C module for Python 2.7
I determined from that answer that 2010 Express only installs 32-bit compilers. To get 64-bit (and other) compilers, you need to install Windows 7.1 SDK. See http://msdn.microsoft.com/en-us/windowsserver/bb980924.aspx
This would not install for me though, and the installer returned the error installation failed with return code 5100. I found the solution at the following link: http://support.microsoft.com/kb/2717426. In short, if newer versions of x86 and x64 Microsoft Visual C++ 2010 Redistributable's are installed, they conflict with the ones in SDK installer, and need uninstalling first.
The SDK then installed, but I noticed vcvars64.bat still did not exist in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin, nor its subfolders. vcvarsall.bat runs the vcvars64 batch file, so without it, the python package still wouldn't install (I forgot the error that was shown at this time).
I then found some instructions here: http://www.cryptohaze.com/wiki/index.php/Windows_7_Build_Setup#Download_VS_2010_and_Windows_SDK_7.1
Following the instructions, I had already installed Express and 7.1 SDK, so installed SDK 7.1 SP1, and did the missing header file fix. I then manually created vcvars64.bat with the content CALL setenv /x64. I will paste all those instructions here, so they don't get lost.
Step 1 is to download Visual Studio Express 2010.
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express
is a good place to start. Download the installer, and run it
(vc_web.exe). You don't need the SQL 2008 additional download.
You'll also need the Windows SDK (currently 7.1) for the 64-bit
compilers - unless you want to do 32-bit only builds, which are not
fully supported...
http://www.microsoft.com/en-us/download/details.aspx?id=8279 is a good
starting point to download this - you'll want to run winsdk_web.exe
when downloaded!
The default install here is just fine.
Finally, download and install the Windows SDK 7.1 SP1 update:
http://www.microsoft.com/en-us/download/details.aspx?id=4422
And, to fix missing header file, VS2010 SP1.
http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=75568aa6-8107-475d-948a-ef22627e57a5
And, bloody hell, fix the missing batch file for VS2010 Express. This
is getting downright absurd.
In C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64,
create "vcvars64.bat" with the following (you will need to be running
as administrator):
CALL setenv /x64
My python package still did not install (can't recall error). I then found some instructions (copied below) to use the special SDK 7.1 Command Prompt, see: https://mail.python.org/pipermail/distutils-sig/2012-February/018300.html
Never mind this question. Somebody here noticed this item on the menu: Start->All Programs->Microsoft Windows SDK v7.1 ->Windows SDK 7.1 Command Prompt
This runs a batch job that appears to set up a working environment for the compiler. From that prompt, you can type "setup.py build" or "setup.py install".
I opened the Windows SDK 7.1 Command Prompt as instructed, and used it to run easy_install on the python package. And at last, success!
TLDR;
Install Visual Studio Express 2010 (preferably without updated redistributables or SQL server).
Install Windows 7.1 SDK
Instal SDK 7.1 SP1 update, and VS2010 SP1 header file fix (this step may not be required).
Manually create C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\vcvars64.bat with content CALL setenv /x64
Start->All Programs->Microsoft Windows SDK v7.1 ->Windows SDK 7.1 Command Prompt to open special x64 command prompt, which can then be used with python/easy_install/pip/etc (including those in virtual_envs).
Below steps fixed this issue for me, I was trying to create setup with cython extension.
Install Microsoft Visual C++ Compiler for Python 2.7
The default install location would be #
C:\Users\PC-user\AppData\Local\Programs\Common\Microsoft\Visual C++
for Python This might actually fix the issue, test once before proceeding.
If it fails, Check where in VC++
for python vcvarsall.bat file is located
Open the msvc9compiler.py
file of distutils package in notepad.
In my box this was #
C:\Anaconda2\Lib\distutils\msvc9compiler.py find_vcvarsall function
in this file, determine the version of VC by printing out version
argument. For Python 2.7 it's likely to be 9.0
Now create an
environment variable VS90COMNTOOLS, Pointing to
C:\Users\PC-user\AppData\Local\Programs\Common\Microsoft\Visual C++
for Python\9.0\VC\bin
For some reason distutils expects the
vcvarsall.bat file to be within VC dir, but VC++ for python tools
has it in the root of 9.0 To fix this, remove "VC" from the
path.join (roughly around line 247)
#productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
productdir = os.path.join(toolsdir, os.pardir, os.pardir)
The above steps fixed the issue for me.
calling import setuptools will monkey patch distutils to force compatibility with Visual Studio. Calling vcvars32.bat manually will setup the virtual environment and prevent other common errors the compiler will throw. For VS 2017 the file is located at
"C:\Program Files (x86)\Microsoft Visual
Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"
Here is the setup script I use to quickly compile .pyx files to .pyd:
(Note: it uses the 3rd party module
send2trash
# cython_setup.py
import sys, os, time, platform, subprocess
from setuptools import setup, find_packages
from Cython.Build import cythonize
from traceback import format_exc
# USAGE:
#
# from cython_setup import run
# run(pyx_path)
# vcvars = r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"
# NOTE: to use visual studio 2017 you must have setuptools version 34+
vcvars = r"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars32.bat"
def _build_ext():
try:
pyx_path = sys.argv.pop(-1)
pyx_path = os.path.abspath(pyx_path)
if not os.path.exists(pyx_path):
raise FileNotFoundError(f"{pyx_path} does not exist")
project_name = sys.argv.pop(-1)
os.chdir(os.path.abspath(os.path.dirname(pyx_path)))
print("cwd: %s" % os.getcwd())
print(os.path.abspath("build"))
setup(
name=project_name,
# cmdclass = {'build_ext': build_ext},
packages=find_packages(),
# ext_modules=cythonize(extensions)
ext_modules=cythonize(pyx_path,
compiler_directives={'language_level': 3, 'infer_types': True, 'binding': False},
annotate=True),
# include_dirs = [numpy.get_include()]
build_dir=os.path.abspath("build")
)
except:
input(format_exc())
def retry(func):
def wrapper(*args, **kw):
tries = 0
while True:
try:
return func(*args, **kw)
except Exception:
tries += 1
if tries > 4:
raise
time.sleep(0.4)
return wrapper
#retry
def cleanup(pyx_path):
from send2trash import send2trash
c_file = os.path.splitext(pyx_path)[0] + ".c"
if os.path.exists(c_file):
os.remove(c_file)
if os.path.exists("build"):
send2trash("build")
def move_pyd_files(pyx_path):
pyx_dir = os.path.dirname(pyx_path)
build_dir = os.path.join(pyx_dir, "build")
if not os.path.exists(build_dir):
raise RuntimeError(f"build_dir {build_dir} did not exist....")
found_pyd = False
for top, dirs, nondirs in os.walk(build_dir):
for name in nondirs:
if name.lower().endswith(".pyd") or name.lower().endswith(".so"):
found_pyd = True
old_path = os.path.join(top, name)
new_path = os.path.join(pyx_dir, name)
if os.path.exists(new_path):
print(f"removing {new_path}")
os.remove(new_path)
print(f"file created at {new_path}")
os.rename(old_path, new_path)
if not found_pyd:
raise RuntimeError("Never found .pyd file to move")
def run(pyx_path):
"""
:param pyx_path:
:type pyx_path:
:return: this function creates the batch file, which in turn calls this module, which calls cythonize, once done
the batch script deletes itself... I'm sure theres a less convoluted way of doing this, but it works
:rtype:
"""
try:
project_name = os.path.splitext(os.path.basename(pyx_path))[0]
run_script(project_name, os.path.abspath(pyx_path))
except:
input(format_exc())
def run_script(project_name, pyx_path):
dirname = os.path.dirname(pyx_path)
# ------------------------------
os.chdir(dirname)
if os.path.exists(vcvars):
# raise RuntimeError(
# f"Could not find vcvars32.bat at {vcvars}\nis Visual Studio Installed?\nIs setuptools version > 34?")
subprocess.check_call(f'call "{vcvars}"', shell=True)
cmd = "python" if platform.system() == "Windows" else "python3"
subprocess.check_call(f'{cmd} "{__file__}" build_ext "{project_name}" "{pyx_path}"', shell=True)
move_pyd_files(pyx_path)
cleanup(pyx_path)
if len(sys.argv) > 2:
_build_ext()
I tried many solutions but only one worked for me, the install of Microsoft Visual Studio 2008 Express C++.
I got this issue with a Python 2.7 module written in C (yEnc, which has other issues with MS VS). Note that Python 2.7 is built with MS VS 2008 version, not 2010!
Despite the fact it's free, it is quite hard to find since MS is promoting VS 2010.
Still, the MSDN official very direct links are still working: check https://stackoverflow.com/a/15319069/2227298 for download links.
If you have mingw installed
pip install --global-option build_ext --global-option --compiler=mingw32 packagename
works, forcing pip to build using the mingw compiler instead of Microsoft's. See here https://github.com/pypa/pip/issues/18 for details (last post).
Is Microsoft Visual C++ Compiler for Python 2.7 at http://www.microsoft.com/en-us/download/details.aspx?id=44266 not a solution?
The easiest way to solve this in 2016 is to install Chocolatey and then the vcpython27 package. Open Powershell:
> iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
> choco install python2 -y
> choco install vcpython27 -y
I don't know if it is too late, but I found Microsoft Visual C++ Compiler for Python 2.7 which reads
The typical error message you will receive if you need this compiler package is Unable to find vcvarsall.bat
Hope this helps!
An exhaustive list of MS VС++ versions and installation variations officially supported by distutils (and some by setuptools) and how to use them can be found at
https://wiki.python.org/moin/WindowsCompilers
It also specifies which VC++ version is required for which official Win32 Python release. Note that MinGW is not officially supported (see below for details).
In brief:
For each release, there's a specific Visual Studio release that works out of the box.
Note that Express editions before 2012 do not support x64. Trying to use one for x64 results in an obscure error.
For standalone SDKs, it's generally required to run the installation from the SDK prompt (=set environment variables).
For some, this requirement can be lifted by a one-time tweaking which doesn't have adversal side effects
Some unusual VC++ releases are supported by setuptools and not distutils.
MinGW is said to "work", but only for x86. https://bugs.python.org/issue4709 says that problems with its support lie in MinGW itself and even x86 is bound to have problems since Python 3.5.
I got the same problem and have solved it at the moment.
"Google" told me that I need to install "Microsoft Visual C++ Compiler for Python 2.7". I install not only the tool, but also Visual C++ 2008 Reditributable, but it didn't help. I then tried to install Visual C++ 2008 Express Edition. And the problem has gone!
Just try to install Visual C++ 2008 Express Edition!
If you're looking to install pyodbc on a Windows box that doesn't have Visual Studio installed another option is to manually install pyodbc using the binary distribution.
This is particularly useful if you do not have administrator privileges on the machine you're working with and are trying to set up a virtualenv.
Steps:
Download the latest Windows installer from here (pyodbc-X.X.X.win-Y-py2.7.exe)
Open the installer executable using 7-Zip (or WinRAR or whatever)
Extract pyodbc.pyd and pyodbc-X.X.X-py2.7.egg-info and place them in [python installation directory or virtualenv]\Lib\site-packages
There is no step 4 :)
With Python 3.4, the dependency is on Visual Studio 2010. Installing Visual C++ 2010 Express fixed the problem for me.
Tricking it into using the VS 2008 or 2013 installs that I happened to have didn't work.
You can use easy_install instead of pip it works for me.

What's the official way to get cython working with Enthought Canopy on 64-bit Windows?

I have the "Enthought Canopy" v1.5 distribution of Python 2.7 installed with cython 0.22 (installed via the Canopy package manager) on a Windows 64-bit computer. Trying to compile a cython file leads to the following error:
building 'my_cython_extension' extension
error: Could not find Visual Studio 2008 in your path.
If you do not have Visual Studio 2008 installed, you can use
the MinGW compiler instead. To install mingw, do:
enpkg mingw
To use the MinGW compiler to build an extension module, use
the '-c' flag, e.g.:
python setup.py build_ext -c mingw64
Note that building Python extensions with MinGW is not officially
supported, although it is known to work in many cases.
Visual Studio 2008 is no longer available, and Visual Studio 2008 Express doesn't include a 64-bit compiler. I've read that it's necessary to compile cython with the same compiler that Python was compiled with, which rules out using a newer version of Visual Studio. Additionally, the message clearly states that MinGW is not officially supported.
I've tried installing "Microsoft Visual C++ Compiler for Python 2.7" as per the Cython Wiki, but that has no effect on making the above error message disappear. I've read that updating setuptools may fix this error (as it can find the compiler in different locations than the old version expects to find it), but that didn't work for me, and additionally Enthought have stated on their website that:
Setuptools 0.6 is already supported, and higher versions are not yet compatible with Enthought installers.
... so updating setuptools may break something in Canopy.
Finally, I've tried installing "MS Windows SDK for Windows 7 and .NET Framework 3.5 SP1" as written later on in the cython Wiki, but this also had no effect on making the message disappear, and additionally it's a huge unwieldy package which I'd rather not install just for the compiler if possible.
So, is there a way to get "Microsoft Visual C++ Compiler for Python 2.7" working with cython and Enthought Canopy? If not, what is the official way to install and configure the cython compiler?
It took me the greater part of a day to figure out how to get this working, and I sincerely hope there's a better way, which I would love to hear, but this is what got the job done for me:
1) Install Microsoft Visual C++ 2008 Express edition -- you should find a direct download link by Googling for it.
2) Download the "Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1 (ISO)" image (be sure to choose the 64-bit version GRMSDKX_EN_DVD.iso) from here
3) Extract the iso with 7-zip, or mount / burn it with your preferred software
4) Run setup.exe and install. You can probably safely deselect everything except for "Visual C++ Compilers" and "Windows Headers and Libraries"
5) Copy the file C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\vcvars64.bat to the folder C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64\ and rename it to vcvarsamd64.bat
6) Not sure if this is necessary, but you may need to run the following commands in the Visual Studio command prompt, as written in the cython wiki
set DISTUTILS_USE_SDK=1
setenv /x64 /release
Note 1: Step 1 appears to be necessary because the 64-bit Windows SDK for .net 3.5 doesn't seem to come with a vcvarsall.bat file.
Note 2: Step 5 is necessary because the vcvarsall.bat that comes with Visual C++ 2008 Express points to the wrong location for the 64-bit compiler.
(Enthought Support Here)
For the most up to date info, see our Knowledge Base that summarizes issues and provides useful links to some of the needed components for building extensions on Windows (as well other platforms)
Regarding the "Microsoft Visual C++ Compiler for Python 2.7", we are working on updating Canopy's Python version to 2.7.9 in our next release, which will include needed changes to support Microsoft's new compiler. We expect Canopy 1.5.3 to be released sometime next week.
For using the SDK now, the steps we recommend are:
Download the Windows SDK for Python 2.7 as either a web installer or a DVD ISO.
Install the SDK.
If you use the DVD .iso, the SDK is provided as a DVD .iso image. To install it, you'll need software to mount this as a virtual DVD. Windows 8 provides this, but on Windows 7 you will need something like the following: http://www.microsoft.com/en-us/download/details.aspx?id=38780
Once the SDK is installed, you need to start the SDK Command Prompt and set two environment variables before compiling your extensions:
set DISTUTILS_USE_SDK=1
setenv /x64 /release
Assuming you didn't change the default SDK install path, this should look like:
C:\Program Files\Microsoft SDKs\Windows\v7.0>set DISTUTILS_USE_SDK=1
C:\Program Files\Microsoft SDKs\Windows\v7.0>setenv /x64 /release
(Note: you may need to swap /x64 for /x86 if on a 32-bit system.)
From what I understand, using the SDK Command Prompt to set these variables will help avoid all of the extra configuration/surgery surrounding vcvarsall.bat.

Categories

Resources