Multiple Tests Mixing Up Python Version Between Test Venv And System
Describe the Bug
When running the test suite in a virtual environment where the Python version used in the virtual environment is different from the system Python, it seems that the fixtures use the wrong (system) Python version rather than the version used in the virtual environment (and expected by the tests).
To Reproduce
The issue can be reproduced by creating a virtual environment with a different Python version than the system Python, and then running the test suite.
Example Steps
- Create a virtual environment with Python 3.12:
pdm venv create 3.12
- Install the required packages in the virtual environment:
pdm install -dGtest
- Run the test suite:
pdm run pytest -n12 --dist=worksteal
Expected Behavior
The test suite should use the consistent Python version specified in the virtual environment.
Environment Information
The environment information is as follows:
- PDM version: 2.24.0
- Python Interpreter: /tmp/pdm/.venv/bin/python (3.12)
- Project Root: /tmp/pdm
- Local Packages: None
Verbose Command Output
The verbose command output is not provided.
Additional Context
The issue is not specific to PDM and can be reproduced with pure pdm
venv.
Are You Willing to Submit a PR to Fix This Bug?
Yes, I would like to submit a PR to fix this bug.
Code Snippet
The code snippet that reproduces the issue is as follows:
import sys
import os
def test_project_packages_path(project):
packages_path = project.environment.packages_path
version = ".".join(map(str, sys.version_info[:2]))
if os.name == "nt" and sys.maxsize <= 2**32:
assert packages_path.name == version + "-32"
else:
assert packages_path.name == version
This code snippet uses the sys.version_info
attribute to get the Python version, which is expected to be 3.12 in the virtual environment. However, the test fails because the system Python version is 3.13.
Fix
The fix for this issue is to use the Python version specified in the virtual environment instead of the system Python version. This can be achieved by using the python_full_version
attribute from the pdm info --env
command, which returns the Python version used in the virtual environment.
Updated Code Snippet
The updated code snippet is as follows:
import sys
import os
def test_project_packages_path(project):
packages_path = project.environment.packages_path
version = ".".join(map(str, sys.version_info[:2]))
env_info = pdm.info("--env")
python_full_version = env_info["python_full_version"]
if os.name == "nt" and sys.maxsize <= 2**32:
assert packages_path.name == python_full_version + "-32"
else:
assert packages_path.name == python_full_version
This updated code snippet uses thepython_full_versionattribute from the
pdm info --env` command to get the Python version used in the virtual environment, which is expected to be 3.12.
Conclusion
Q: What is the issue with the test suite?
A: The issue with the test suite is that it is using the wrong Python version, which is the system Python version instead of the version used in the virtual environment.
Q: Why is this happening?
A: This is happening because the fixtures are using the sys.version_info
attribute to get the Python version, which is the system Python version.
Q: How can I reproduce this issue?
A: You can reproduce this issue by creating a virtual environment with a different Python version than the system Python, and then running the test suite.
Q: What are the steps to reproduce this issue?
A: The steps to reproduce this issue are:
- Create a virtual environment with Python 3.12:
pdm venv create 3.12
- Install the required packages in the virtual environment:
pdm install -dGtest
- Run the test suite:
pdm run pytest -n12 --dist=worksteal
Q: What is the expected behavior?
A: The expected behavior is that the test suite uses the consistent Python version specified in the virtual environment.
Q: How can I fix this issue?
A: You can fix this issue by using the Python version specified in the virtual environment instead of the system Python version. This can be achieved by using the python_full_version
attribute from the pdm info --env
command.
Q: What is the updated code snippet?
A: The updated code snippet is as follows:
import sys
import os
def test_project_packages_path(project):
packages_path = project.environment.packages_path
version = ".".join(map(str, sys.version_info[:2]))
env_info = pdm.info("--env")
python_full_version = env_info["python_full_version"]
if os.name == "nt" and sys.maxsize <= 2**32:
assert packages_path.name == python_full_version + "-32"
else:
assert packages_path.name == python_full_version
Q: Why is this fix necessary?
A: This fix is necessary because the fixtures are using the sys.version_info
attribute to get the Python version, which is the system Python version. By using the python_full_version
attribute from the pdm info --env
command, we can get the Python version used in the virtual environment.
Q: Can this issue be avoided in the future?
A: Yes, this issue can be avoided in the future by using the python_full_version
attribute from the pdm info --env
command instead of the sys.version_info
attribute.
Q: What are the benefits of using the python_full_version
attribute?
A: The benefits of using the python_full_version
attribute are:
- It provides the correct Python version used in the virtual environment.
- It avoids the issue of using the system Python version instead of the virtual environment Python version.
- It the test suite more reliable and consistent.
Q: Can this issue be fixed in other ways?
A: Yes, this issue can be fixed in other ways, such as:
- Using a different method to get the Python version, such as using the
platform.python_version()
function. - Using a different approach to test the Python version, such as using a separate test for the Python version.
However, using the python_full_version
attribute from the pdm info --env
command is the most straightforward and reliable way to fix this issue.