pytest causes a problem for other tools like tox due `py`
See original GitHub issueI think that these changes are not well implemented, causing problems with tox. When tox and pytest are installed at the same time, tox will not work with an error AttributeError: module 'py' has no attribute 'io'.
More information could be found in the bug#2540.
An example of how to reproduce an error.
$ lxc launch ubuntu-daily:jammy tox
$ lxc shell tox
root@tox:~# su ubuntu
ubuntu@tox:/root$ cd ~
ubuntu@tox:~$ sudo apt update && sudo apt install tox -y
ubuntu@tox:~$ tee -a tox.ini <<EOF
[tox]
skipsdist=True
envlist = test
[testenv:test]
deps = pytest
commands = pytest --version
EOF
ubuntu@tox:~$ tox
tox
test create: /home/ubuntu/.tox/test
test installdeps: pytest
test installed: attrs==22.1.0,exceptiongroup==1.0.4,iniconfig==1.1.1,packaging==21.3,pluggy==1.0.0,pyparsing==3.0.9,pytest==7.2.0,tomli==2.0.1
test run-test-pre: PYTHONHASHSEED='3983464967'
test run-test: commands[0] | pytest --version
pytest 7.2.0
_________________________________________________________________ summary __________________________________________________________________
test: commands succeeded
congratulations :)
ubuntu@tox:~$ pip3 install pytest
Defaulting to user installation because normal site-packages is not writeable
Collecting pytest
Using cached pytest-7.2.0-py3-none-any.whl (316 kB)
Requirement already satisfied: tomli>=1.0.0 in ./.local/lib/python3.10/site-packages (from pytest) (2.0.1)
Requirement already satisfied: exceptiongroup>=1.0.0rc8 in ./.local/lib/python3.10/site-packages (from pytest) (1.0.4)
Requirement already satisfied: iniconfig in ./.local/lib/python3.10/site-packages (from pytest) (1.1.1)
Requirement already satisfied: pluggy<2.0,>=0.12 in /usr/lib/python3/dist-packages (from pytest) (0.13.0)
Requirement already satisfied: packaging in /usr/lib/python3/dist-packages (from pytest) (21.3)
Requirement already satisfied: attrs>=19.2.0 in /usr/lib/python3/dist-packages (from pytest) (21.2.0)
Installing collected packages: pytest
WARNING: The scripts py.test and pytest are installed in '/home/ubuntu/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pytest-7.2.0
ubuntu@tox:~$ tox
Traceback (most recent call last):
File "/usr/bin/tox", line 33, in <module>
sys.exit(load_entry_point('tox==3.21.4', 'console_scripts', 'tox')())
File "/usr/bin/tox", line 25, in importlib_load_entry_point
return next(matches).load()
File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in load
module = import_module(match.group('module'))
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/usr/lib/python3/dist-packages/tox/__init__.py", line 32, in <module>
from .session import cmdline # isort:skip
File "/usr/lib/python3/dist-packages/tox/session/__init__.py", line 20, in <module>
from tox import reporter
File "/usr/lib/python3/dist-packages/tox/reporter.py", line 127, in <module>
_INSTANCE = Reporter()
File "/usr/lib/python3/dist-packages/tox/reporter.py", line 32, in __init__
self._reset(**kwargs)
File "/usr/lib/python3/dist-packages/tox/reporter.py", line 38, in _reset
self.tw = py.io.TerminalWriter()
AttributeError: module 'py' has no attribute 'io'
System parameters:
ubuntu@tox:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Codename: jammy
ubuntu@tox:~$ python3 --version
Python 3.10.6
ubuntu@tox:~$ sudo apt info tox
Package: tox
Version: 3.21.4-1ubuntu1
Built-Using: alabaster (= 0.7.12-1), sphinx (= 4.3.2-1)
Priority: optional
Section: universe/python
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Python Team <team+python@tracker.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 1788 kB
Depends: python3, python3-setuptools, python3-filelock, python3-importlib-metadata | python3 (>> 3.8), python3-packaging, python3-pluggy (>= 0.12.0-1~), python3-py, python3-six (>= 1.14.0), python3-toml, python3-virtualenv, python3:any, libjs-sphinxdoc (>= 4.3)
Homepage: https://tox.readthedocs.io/
Download-Size: 260 kB
APT-Manual-Installed: yes
APT-Sources: http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
Description: virtualenv-based automation of test activities
Tox as is a generic virtualenv management and test command line tool
you can use for:
.
* checking your package installs correctly with different Python
versions and interpreters
* running your tests in each of the environments, configuring your
test tool of choice
* acting as a frontend to Continuous Integration servers, greatly
reducing boilerplate and merging CI and shell-based testing.
ubuntu@tox:~$ pip3 show pytest
Name: pytest
Version: 7.2.0
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email:
License: MIT
Location: /home/ubuntu/.local/lib/python3.10/site-packages
Requires: attrs, exceptiongroup, iniconfig, packaging, pluggy, tomli
Required-by:
Issue Analytics
- State:
- Created 10 months ago
- Reactions:1
- Comments:13 (8 by maintainers)
Top Results From Across the Web
`pytest-benchmark`: AttributeError: module 'py' has no attribute ...
When I run test cases with 7.2.0 I get this error: ... pytest causes a problem for other tools like tox due py...
Read more >pytest cannot import module while python can - Stack Overflow
I am a bit puzzled, it looks like this indicates an import error, but Python does it fine so why is there a...
Read more >Effective Python Testing With Pytest
This tutorial will help you understand some of the tools pytest provides to keep your testing efficient and effective even as it scales....
Read more >Full pytest documentation
Start here¶ · Install pytest · Create your first test · Run multiple tests · Assert that a certain exception is raised ·...
Read more >Automated Testing in Python with pytest, tox, and GitHub Actions
Take your Python project to the next level of professionalism.Automated testing in Python is an important way to take your Python project to ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

ok first off this is free software so nothing needs to be resolved – it’s not appropriate to demand work in this way
as above,
--system-site-packagesis already a hack at best – you’re explicitly deviating from the libraries your OS packager intended you to use – why not use your packager’spytesttoo if you want to use system site packages?the python community at large does not recommend using
--system-site-packages– “use a virtualenv and install what you need in that virtualenv” has been the overwhelming advice for over a decade at this point. bothvirtualenvandvenvdefault to this mode and only in exceptional cases do you use--system-site-packagesit absolutely is –
pyis deprecated, so it’s only as “permanent” aspy– I’d recommend moving off of--system-site-packagespersonally or eliminating / upgrading whatever part of your stack is dependent on the deprecatedpylibraryyou only need it for
py– not all your packagesthe larger python community is not using, and does not recommend,
--system-site-packagesandpyis deprecated – if you need to bring in a deprecated library in a not-recommended setup this is by all means acceptable.Team, this issue still needs to be resolved.
This is still broken when using
--system-site-packagesunless--ignore-installedis used. This is broken in a non-intuitive way with the only solution is finding the closed issues in this project. I appreciate having a workaround but that is not a viable permanent solution as it removes the ability to leverage system-site-packages. Using--ignore-installedhas the side-effect of reinstalling all packages even if there are no changes from the last time you ran it.Installing modules in the system-site-packages (or in the distro) and using them in conjunction with a virtual environment is a valid and supported approach even if it is not convenient for the way the pytest
pyshim works. pytest breaks this scenario with an unintended side-effect and there should be an open issue for this tracking its resolution. We can’t say this is working as intended or be satisfied with the poor user experience this causes to the larger python community.