iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🗂

Installing Python libraries without 32-bit binaries on Windows

に公開

Overview

How to coexist 32-bit and 64-bit

  • To coexist 32-bit and 64-bit Python environments, it seems best to create a virtual environment using virtualenv.

Resolving pip install errors in 32-bit Python

  • When running pip install in a 32-bit version results in an "error: metadata-generation-failed", it appears that pre-built 32-bit Python packages are unavailable, so you need to build them yourself.
  • Although it may seem difficult, it is surprisingly easy using the following steps:
  1. Install Visual Studio. (In my case, I installed VS2022)
  2. Open "x86 Native Tools Command Prompt for VS2022" and run pip install from this tool. If you are using a virtual environment, activate it within the prompt before running pip install.

Background

To use a service called JRA-VAN Data Lab., it is necessary to use a module called JV-Link, but this is an ancient ActiveX COM control that can only be used from 32-bit Python.

In my previous article, I used 32-bit Anaconda to quickly set up the environment, and I have actually built and still operate a horse racing prediction environment.

At that time, I was unaware of the usefulness of Python virtual environments (or rather, the harm of not using them), so I just threw everything into Anaconda's "base" environment.

As the years passed, I vaguely understood that it is better to build environments with virtual environments when using Python for various purposes. After building a new PC and performing a clean install of Windows 11, I tried to build a virtual environment using only the libraries I needed. (As a result, I struggled quite a bit.)

How to Coexist 32-bit and 64-bit Environments

32-bit Python is required when using JV-Link, but I thought it would be safer to use 64-bit Python for general purposes, so I looked into ways to make them coexist.

Installation itself is not a problem

Python itself installs both 32-bit and 64-bit versions into different folders by default, so coexistence is not a particular issue.
In my case, I installed both the 32-bit and 64-bit versions of Python 3.12,
and folders named "python312" for the 64-bit version and "Python312-32" for the 32-bit version
were created under "User Directory\AppData\Local\Programs\Python".

Set up the 32-bit environment as a virtual environment using virtualenv

Generally, venv is used to create virtual environments, but my current understanding is that it is for managing library versions while the version of Python itself remains fixed.
In this case, since the requirement is similar to managing the version of Python itself, I will use virtualenv.
I wanted to make 64-bit Python the default, so I installed virtualenv on the 64-bit Python using pip.

There are various ways to check the preferred Python version and architecture, but simply typing "python" in the terminal seems to be the easiest. Use "exit()" to leave.
If it says "64 bit (AMD64)", it is running on 64-bit Python.
If it shows 32-bit, it might be easiest to remove the 32-bit path from the environment variables and keep only the 64-bit one.

> python
Python 3.12.6 (tags/v3.12.6:a4a2d2b, Sep  6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Install virtualenv after confirming it is running on the 64-bit version

> pip install virtualenv

Create a 32-bit Python (python312-32) virtual environment named "py32" using virtualenv

> cd (folder where you want to create the virtual environment)
> virtualenv -p python312-32 py32

This creates a virtual environment under the "py32" folder.

Activate the virtual environment and confirm it is a 32-bit environment.
It shows "32 bit (Intel)", confirming it is running on the 32-bit version.

> .\py32\scripts\activate
(py32)>python
Python 3.12.6 (tags/v3.12.6:a4a2d2b, Sep  6 2024, 19:51:05) [MSC v.1940 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

In this way, the 64-bit environment is the default, and when you want to use the 32-bit environment, you can coexist by creating a virtual environment with virtualenv.

Resolving pip install errors in 32-bit Python

Error when 32-bit wheels are not provided

In most cases, libraries can be installed with pip install even in a 32-bit environment, but there are some libraries where errors occur and the installation does not complete.
For example, when trying to pip install pandas for data analysis:

(py32)> pip install pandas
...
error: subprocess-exited-with-error
...
error: metadata-generation-failed

Installation fails like this.

Upon investigation, it turns out that starting from version 2.1, pandas has stopped providing 32-bit wheels (binary packages).
https://github.com/pandas-dev/pandas/issues/55616

It is suggested to either build the library from source or use WSL, but since I am using ActiveX, WSL doesn't seem to be an option[1].

I thought it might work if I used an older version before 2.1 when 32-bit binaries were provided, but then I ran into issues where the Numpy version didn't match and things didn't go well... I struggled quite a bit.

Do I have to build it?

Eventually, I decided I had no choice but to build it, so I prepared by referring to the link provided in the aforementioned thread about discontinued support.
https://pandas.pydata.org/docs/development/contributing_environment.html

I installed the Build Tools for Visual Studio 2022 following the instructions for Windows in "Step 1: install a C compiler".

Normally, I should have just proceeded to Step 2 and Step 3, but I found information that running pip install after opening the "x86 Native Tools Command Prompt for VS 2022" worked for someone.
https://github.com/pandas-dev/pandas/issues/58134

I thought, "Wait, is it really that simple?" So, I opened the "x86 Native Tools Command Prompt for VS 2022", activated the 32-bit environment I had created, and ran pip install [2].

> .\py32\scripts\activate
(py32)> pip install pandas
...
Successfully installed numpy-2.1.1 pandas-2.2.3 pytz-2024.2 six-1.16.0 tzdata-2024.2

And it actually worked! This solved the problem I had been agonizing over for about a week. Once the installation is successful, it can be used in the usual terminal as well, so there are no issues.

Similar errors occurred with other libraries besides pandas, but I was able to handle them using the same method.

脚注
  1. I have a memory of running JV-Link via WINE over 20 years ago, so it might be possible, but it's a hassle. ↩︎

  2. Since it mentioned using meson, I installed it beforehand with pip install meson. I'm not sure if it's mandatory. ↩︎

Discussion