Extending & Embedding Python Using C - A Module Using Windows |
Written by Mike James | ||||
Wednesday, 08 January 2025 | ||||
Page 1 of 3 Getting started with extending Python is difficult because of the need to set everything up correctly. Not if you follow our instructions for the Windows toolchain. This is an extract from the new book by Mike James that helps you combine the speed and power of C with the versatility and ease-of-programming of Python. Extending & Embedding Python Using CBy Mike JamesBuy from Amazon. ContentsPreface
<ASIN:B0CK3X93KF> Windows is more difficult to get started with than Linux partly because we have to use the Microsoft Visual C++ (MSVC) compiler and this isn’t as well-documented for use on the command line as GCC is. You can use GCC under Windows if you want to, but Python under Windows is compiled using MSVC and this is the recommended compiler for extensions. In the rest of this chapter Visual Studio Code (VS Code) is used as the development environment along with MSVC. The reason for this is that it is open source, relatively easy to use and supports a range of languages including C and Python. To make the procedure as foolproof as possible we first check that it is possible to compile a simple C program and then move on to compiling and using a simple extension. For this simple example, compiling using the Microsoft C/C++ extension is good enough. For a real multi-file project you can use CMake or something similar. It is also true that Visual Studio is also a good choice for developing Python extensions. Its only disadvantage is that it is not open source. VS Code and MSVCThe recommended compiler for Python extensions under Windows is the latest version of Microsoft C/C++ MSVC compiler. Unless you have already installed it or are using Visual Studio, it is unlikely to be available on your system and you will have to install it. This involves installing the Desktop development with C++ workload from the Visual Studio webpage, or you could use the complete Visual Studio installation just to get the desktop development system. When the installer has started select Desktop development with C/C++. After this has installed you can use the Developer Command Prompt to test that the compiler has been installed properly using the command cl. To make VS Code use the compiler you have to start it from the Developer Command Prompt and a fact that isn’t clearly made in the documentation is that the version of the compiler that you use depends on which command prompt is active. The compiler comes in versions that run on 32-bit or 64-bit machines to produce 32-bit or 64-bit code. Which compiler VS Code uses depends on the command prompt used to start it:
On a 64-bit machine you want a 64-bit compiler that produces 64-bit code, so the appropriate choice is the x64 Native Tools command prompt. This is very important as you need to match the compiler to the version of Python that you are running. If you don’t then, even with everything setup correctly, the linker will complain that it can’t find the Python library functions. You can check that you have MSVC installed and accessible by starting the appropriate Native Tools prompt and entering: cl i.e. C L in lower case. You should see something like: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools>cl Microsoft (R) C/C++ Optimizing Compiler Version 19.36.32532 for x64 Copyright (C) Microsoft Corporation. All rights reserved. Once you have checked that you have MSVC installed you can move on to installing VS Code. As this changes very frequently, the best advice is to follow the current instructions on the website: https://code.visualstudio.com/ Install accepting all the defaults unless you know better. Start the appropriate command prompt, which for a 64-bit Python system is x64 Native Tool, and enter code. This runs VS Code and from here you can use it as normal. If you want to use MSVC with the correct architecture via VS Code you have to start VS Code from the Native Tool prompt. If you don’t start VS Code from the correct Native Tool prompt nothing much will work.
If you install the C/C++ extension you will be offered the other two as suggestions and if you install the Extension Pack you get CMake and CMake Tools automatically. CMake is the best way to keep control of a large multi-file project, but you don’t need it for small single-file examples such as Hello World. If VS Code offers to configure CMake for you select the option to ignore CMake. Before moving on to a Hello World example it is worth checking that VS Code can find MSVC. Using the VS Code terminal enter: cl If it doesn’t report the version of the compiler then you have probably not started VS Code from the Native Tool prompt – it is easy to forget! |
||||
Last Updated ( Wednesday, 08 January 2025 ) |