Koushik Ghosh
Koushik Ghosh

Reputation: 21

How to create a virtual environment with Python 3.13 "free-threaded" on Windows?

I am trying to create a virtual environment with Python 3.13 in "free-threaded" mode (GIL disabled) on Windows. I have successfully installed the GIL-free build of Python 3.13, and when I check the installation, I can see the python3.13t.exe interpreter in the installation directory.

However, when I use the command:

python -m venv .venv

The virtual environment is created, but it does not include the python3.13t.exe interpreter. Instead, it defaults to the standard python.exe, which is GIL-enabled.

I've:

  1. Verified that the global Python installation includes python3.13t.exe.

  2. Checked the .venv directory, but it only has the default python.exe.

  3. Attempted to specify the interpreter explicitly using:

    python -m venv --copies .venv
    

But the result is the same.

Edit:

Also tried using the following command:

python3.13t -m venv .venv

This generates both python.exe and python3.13t.exe in the virtual environment, but both appear to be identical with different names. When I run either of them, they both show that they are in free-threaded mode.

Is this the expected behavior when creating a virtual environment with Python 3.13 in free-threaded mode? If not, how can I ensure that the virtual environment only includes the python3.13t.exe interpreter, or at least differentiates it properly from the default python.exe?

Edit 2: code I'm trying to run

import aiohttp
import asyncio
import sys
import time

async def fetch_data(session, url):
    """
    Fetch data from a given URL using an aiohttp session.
    """
    try:
        async with session.get(url) as response:
            data = await response.text()
            print(f"Data fetched from {url[:30]}... (length: {len(data)})")
            return data
    except Exception as e:
        print(f"Failed to fetch data from {url}: {e}")
        return None

async def main():
    """
    Main coroutine to manage the asynchronous fetching of data.
    """
    urls = [
        "https://jsonplaceholder.typicode.com/posts",
        "https://jsonplaceholder.typicode.com/comments",
        "https://jsonplaceholder.typicode.com/albums",
        "https://jsonplaceholder.typicode.com/photos",
        "https://jsonplaceholder.typicode.com/todos",
    ]

    async with aiohttp.ClientSession() as session:
        # Create tasks for fetching data from all URLs
        tasks = [fetch_data(session, url) for url in urls]
        
        # Gather results asynchronously
        results = await asyncio.gather(*tasks)

    print("\nAll tasks completed!")
    for idx, result in enumerate(results):
        print(f"Result {idx + 1} length: {len(result) if result else 'Failed'}")

# Run the main event loop
if __name__ == "__main__":
    if sys._is_gil_enabled():
        print("GIL enabled")
    else:
        print("GIL disabled")
    
    t1 = time.time()
    asyncio.run(main())
    t2 = time.time()

    print(f"Execution time: {t2-t1:.2f}s")

Output: running the code does not output anything output

Upvotes: 2

Views: 882

Answers (2)

Koushik Ghosh
Koushik Ghosh

Reputation: 21

Detour steps:

  1. Create virtual environment with default GIL enabled (non-free threaded) python interpreter, argument '--copies'.
python -mvenv --copies .venv
  1. Copy both python3.13t.exe and pythonw3.13t.exe (which ever required) from default interpreter path to venv\Scripts\ (virtual python interpreter location).
  2. Activate venv
.venv\Scripts\activate
  1. Use the interpreter whichever required.
    Running python in console will use GIL enabled interpreter and python3.13t will use experimental free-threading build as usual.

    Tried:
    Now outputs as expected.
python3.13t aiohttp_file.py

Note: I don't know how it works, just discovered it works.

Upvotes: 0

Steve Barnes
Steve Barnes

Reputation: 28405

On Windows I personally would simply use something like:

py -3.13t -mvenv py13t
py13t\Scripts\activate.bat
python

You should then be in the python shell with initial lines reading something like:

Python 3.13.0 experimental free-threading build (tags/v3.13.0:60403a5, Oct  7 2024, 09:53:29) [MSC v.1941 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Where the experimental free-threading build tells you that you are in the GIL disabled interpreter.

The problem that you are having with the code that you are trying to run is that aiohttp does not currently support running in free threaded mode - even a simple import of it will crash the python3.13t interpreter.

Upvotes: 1

Related Questions