Select Visual Studio Version in `distutils.setup(...)` - python

I'm trying to build a simple C++ extension module for Blender through their Python API which, as of the latest version, is 3.5.3 (to my knowledge, one cannot change this). Python 3.5.3 is compiled with MSC v.1800 (i.e., Visual Studio 2013 version 12.0).
I have both Visual Studio 2013 and Visual Studio 2017 installed. When running distutils.setup(...) in my "setup.py" for the extension, it invariably finds only Visual Studio 2017, causing the build to fail (one must match the Visual Studio version to the Python version [1] ).
How can I convince distutils.setup(...) to find the Visual Studio corresponding to the Python that's running?
I tried prepending the Visual Studio 2013 path to the PATH, which does nothing. I also dug into "Lib/distutils/" to try to figure it out, but couldn't (looks like it's registry-based, not PATH-based).
[1] Or else, one gets the mysterious, undocumented error LNK1327: failure during running rc.exe. This is the entire error, though there is also a minor warning before it, which is apparently normal.

The following code sets two environment variables which were (obscurely) documented as a fallback for when the compiler detection fails.
Then, we call "vcvarsall.bat". Python apparently requires the 8.1 Windows SDK, and only the Visual Studio 2017 version of "vcvarsall.bat" supports that. So, we call the 2017 version, but pass -vcvars_ver=12.0 to set for 2013.
SET MSSdk=1
SET DISTUTILS_USE_SDK=1
call "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/vcvarsall.bat" amd64 8.1 -vcvars_ver=12.0
Then, you can run your:
python35.exe setup.py build

Related

After compilation of any Python program with Nuitka I get "is not a valid win32 application" error

I'm trying to compile my Python project to a Windows executable (.exe) using Nuitka. I get no errors/warnings during the compilation process, but when I'm trying to run the resulting executable I get the "C:\Python34\test.exe is not a valid win32 application" error on Windows XP and a similar one on Windows 10, too). The problem persists even when I compile a "Hello, World!" program. Interesting, that I can easily build and run C++ projects from the Visual Studio IDE on the same PC.
I'm using Windows XP (x32 bits), Python 3.4.0 and Microsoft Visual C++ 2008 Professional.
How can I solve this problem?
I have installed Microsoft Visual Studio 2010 Express instead of Microsoft Visual Studio 2008 Professional, and the problem disappeared. It looks like Nuitka just can't work with the previous version of the compiler. They even say in the docs, that Nuitka is designed for Visual Studio 2017+ (but I can't install recent versions on Windows XP), and that other versions may not work correctly.
I used tried the first steps from the Nuitka website and had the same with example 1. What I found: example 1 uses --mingw64 as option.
python -m nuitka --mingw64 hello.py
In example 2/Use Case 1 there is another option introduced:
python -m nuitka --follow-imports program.py
I only copied my hello-code from example 1 (working) and renamed it to program.py (failed - even no executable!). So I am shure, the cause will be the call and not the code.
When I entered --mingw64 to the second call it worked.
My first thought (I am new to this and no expert for nuitka) is that the second example call implicitly uses gcc. At the first time it asks for installing it and then it seems to use it. The first call seems to force mingw64 as compiler. Could also be that the sequence of path entries makes a difference. Just the first entry will be executed unless you force it via an explicit option.

Dependency errors when loading custom dll in python, built with Visual Studio 2017 and boost

I am trying to build the BGSLibrary python module from C++ and boost source. The library compiles without a problem on linux. For windows I am using Visual Studio 2017 and Cmake 3.9. Here were my steps.
Visual Studio Install options
Download Boost, Install Script from within Visual Studio Command Line Terminal
bootstrap.bat
b2 -j%cores% toolset=msvc-14.1 address-model=32 architecture=x86 link=static threading=multi runtime-link=static --build-type=complete stage --with-python
Rename boost_python-vc141-mt-s-1_64.lib to python_boost.lib to satisfy cmake requirements.
CMake Config options
Visual Studio External Include Directories
Visual Studio External Lib Directories
Move a copy of the originally named boost_python-vc141-mt-s-1_64.lib to the bgs build folder.
Execute Visual Studio Build. It successful creates libbgs python dll
According to this question I rename libbgs.dll to .pyd and have the boost lib in the folder. I added bgslibrary/build to path. Unfortunately not quite there yet.
Some dependency issue. A quick look at DependencyWalker is very intimidating, a huge number of problems.
Before I go and try to individual add all those .dll manually to the bgslibrary/build folder, is there a higher level error that I've done. Should I have selected "static library" in the general properties of visual studio, would that make a difference?
Solved. Dependency walker is old enough where it just shows alot of errors. Really it was jut the 4 opencv dlls. Adding opencv/build/release/bin to my path did it.

How to install SIP & PyQT on windows 7

I'm a newbie to Python environment & really struggling to install SIP & PyQt on my Win 7 machine.
This is what I've done so far,
I've downloaded SIP-4.16.9 and extracted files from the zip folder
The extracted folder is then copied to C:\Python34\Lib
Launched a command prompt(cmd) and changed path to C:\Python34\Lib
Then when I entered python configure.py at C:\Python34\Lib and I get the response as below
At this stage I'm not sure whether it's been installed successfully, to verify I looked for SIP.h at C:\Python34\Include(according to above response - in red), but I cant find any such header file, so I assume the installation hasn't been successful.
Could any one help me, what am I missing here?
Additional System Info:
Windows 7 64 Bit
Python 3.4
Environment Variable (Path : C:\Python34;C:\Python34\Lib;C:\Python34\Lib\site-packages)
Note: There are several questions posted before, but none of them helped so far, I'm sure I must be missing something silly.
Thanks for your time.
For next users who need to build sip (or pyqt) in windows using sources
ALL these instructions are for 32-bits, it will work even if you have a 64-bit system.
For Qt 5.9.x :
Since Qt 5.9, there is only one version of Qt downloadable for Windows that includes the pre-built components for MSVC201x and MinGW.
The instructions follow the same steps as for Qt 5.8
Since Microsoft Visual Studio 2015 Community Edition is not available to download anymore, next users should use Microsoft Visual Studio 2017 Community Edition with Qt 5.9 and follow the same instructions below.
Microsoft Visual Studio 2017 Community Edition (32-bits)
https://www.visualstudio.com/downloads/
Qt 5.9.x
http://download.qt.io/archive/qt/
When Installing Qt, make sure to select the correct pre-built components you have (for e.g Microsoft Visual Studio 2017)
For Qt 5.8 and earlier :
Downloads
Download Python 3.6.x (32-bits) any version will do it :
https://www.python.org/downloads/
(if you also have python 2.7, make sure to use verions 3 of python)
Download sources for sip and PyQt5: Under "Source Packages", download the "Windows Source"
https://www.riverbankcomputing.com/software/sip/download
https://www.riverbankcomputing.com/software/pyqt/download5
Microsoft Visual Studio 2015 Community Edition (32-bits)
https://www.microsoft.com/en-us/download/details.aspx?id=48146
Qt 5.8.0 for Windows 32-bit (VS 2015, 1.0 GB)
http://download.qt.io/archive/qt/
Qt download link must match the Visual Studio (VS) version installed (here VS 2015) and the PyQt5 version you need to install
For example:
PyQt5.8.2
Qt5.8.0 (Link for VS 2015)
Microsoft Visual Studio 2015
Compilation
Open a terminal and stay on it.
It is important you stay on the same terminal (cmd.exe) during the whole process so you don't have to source your environment twice.
Let's create an environment to compile PyQt and SIP (in order to compile PyQt, you need first to compile sip)
Create a folder "compilation" in your Desktop for example:
Extract the content of your sip archive in this forlder
Extract the content of your PyQt5 archive in this forlder
You should have now :
.
├── compilation
| ├── sip-4.19.2/
| └── PyQt5_gpl-5.8.2/
Make sure you are using Python 3.x:
1 - SIP
Go to sip-4.19.2 folder and run :
python configure.py
At this step, you did not install sip, you only configured it (basically its installation dir, etc...)
VERY IMPORTANT STEP: source your environment variables by running in the terminal :
Keep the double quotes, windows does not like spaces ...
"C:\Qt\Qt5.8.0\5.8\msvc2015\bin\qtenv2.bat"
After running this command, it automatically change your current directory, stay on the terminal and re-go to your sip-4.19.2 directory and run:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
In windows, "make" is substituted by "nmake" !
Then (always in the same terminal) you run :
nmake
[...]
nmake install
This will create a sip.exe in your Python dir, for me it is C:\Python36, it may elsewhere, depending on where you installed Python.
2 - PyQt5
The steps are almost the same as sip but it will take more time.
Go to PyQt5_gpl-5.8.2 folder and run :
python configure.py --disable QtNfc --confirm-license
Note that this command disable QtNfs because of a compilation problem I could not resolve.. and automatically accepts the license.
Then, run (the compilation will take a while) :
nmake
nmake install
Hope this helps.
There's a ONE SHOT solution to all your installation problems on Windows. Go to this website. http://www.lfd.uci.edu/~gohlke/pythonlibs/. Navigate to the library you want and download the .whl file for your python version and your computer (32 or 64 bit.) Then run pip install YourWhlFileName.whl, and you're done! (CAUTION: MAKE SURE NOT TO CHANGE THE NAME OF THE WHL FILE OR IT WON'T WORK.)
This is for all that want to build PyQt4 with latest Qt4 and Python 3.4.x.
Download Visual Studio 2010 from here:
http://download.cnet.com/Microsoft-Visual-Studio-2010-Ultimate/3000-2383_4-75450998.html
then install it.
Download SIP source code (in my case version 4.19.5) from here:
https://www.riverbankcomputing.com/software/sip/download
Download PyQt4 source code (in my case version 4.12.1) from here:
https://www.riverbankcomputing.com/software/pyqt/download
Extract SIP and PyQt .zip archive in \Users\your_username\Desktop\compilation\
Download latest Qt 4.x (vs2010 version) installer from here:
http://download.qt.io/archive/qt/
Install Qt (in my case qt-opensource-windows-x86-vs2010-4.8.6.exe).
Open a command prompt (use this one for all the process) and execute these commands:
cd \Users\<your_username>\Desktop\compilation\sip-4.19.5
python configure.py
"C:\Qt\4.8.6\bin\qtvars.bat"
In the output take note of QMAKESPEC value, in my case win32-msvc2010, then use it later on --spec option for PyQt4 configure-ng script.
If the current directory has been changed, execute again:
cd \Users\<your_username>\Desktop\compilation\sip-4.19.5
Now, if you use 64-bit version of Python execute:
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
otherwise, if you use 32-bit version of Python execute:
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
nmake
nmake install
cd \Users\<your_username>\Desktop\compilation\PyQt4_gpl_win-4.12.1
Now use previous QMAKESPEC value in --spec option:
python configure-ng.py --spec win32-msvc2010
nmake
nmake install
That's all ;-)
NOTE: in some cases, in order to avoid this error
ImportError: DLL load failed: The specified procedure could not be found.
it is necessary to copy .dll files from
C:\Qt\4.8.6\bin
to
C:\Python34\Lib\site-packages\PyQt4

error: Unable to find vcvarsall.bat to compile python modules with Visual Studio 2008 installed

I', trying to compile a Python modules on Windows 7 ( Python 3.3 ) and getting the error: Unable to find vcvarsall.bat which i read around is related to not having Visual Studio 2008 installed. Bu i have visual studio 2008 installed AND it's in the path, any ideas?
EDIT - i also checked How do I point easy_install to vcvarsall.bat? but no effects, i tried setting the variable but it's ignored.
EDIT 2 - I've found out by looking at the core that it's now looking for VS100COMNTOOLS instead of VS90COMNTOOLS is the VS studio version different?
Python 3.3 on Windows is built with VS2010, which doesn't match your VS2008. Previous versions of Python did indeed use VS2008.
You'll need to get hold of VS2010.

Windows Server cannot execute a py2exe-generated app

A simple python script needs to run on a windows server with no python installed.
I used py2exe, which generated a healthy dist subdirectory, with script.exe that runs fine on the local machine.
However, when I run it on the server (Windows Server 2003 R2), it produces this:
The system cannot execute the specified program.
and ERRORLEVEL is 9020.
Any ideas?
For py2exe to work, you have to include the correct version of the Microsoft C runtime DLL with your application.
For Python2.6, this is MSVCR90.dll version 9.0.21022.8, which can be obtained from the Microsoft Visual C++ 2008 Redistributable Package:
http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&displaylang=en
NOTE that the SP1 of this installer contains a different version of the DLL, and will not work.
I spent a while earlier this year explaining this carefully in the py2exe tutorial, so I'll just link to that:
http://www.py2exe.org/index.cgi/Tutorial#Step5
It could be missing some required DLLs. Try using depends.exe to identify what might be missing. It's probably an msvc-something-something.dll
I did not find the cause to the problem, but using python 2.5 with py2exe on the same script worked fine on the server.
I guess there is something wrong with py2exe under 2.6.
Because Python 2.6 is built against Visual Studio 2008, the target machine must also have the Visual Studio 2008 libraries, available from the Visual C++ 2008 Redistributable package.
I confirmed that on a clean build of Windows XP or Server 2003 (neither of which have the Visual Studio 2008 redistributable), a Python 2.6 py2exe executable will fail with the aforementioned error, but after installing the redistributable package, the executable runs normally.

Categories

Resources