Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest causes a problem for other tools like tox due py #10520

Closed
rgildein opened this issue Nov 21, 2022 · 13 comments
Closed

pytest causes a problem for other tools like tox due py #10520

rgildein opened this issue Nov 21, 2022 · 13 comments

Comments

@rgildein
Copy link

I 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: 
@hloeung
Copy link

hloeung commented Nov 21, 2022

Digging through closed issues, there's been quite a few.

#10505
#10479
#10451
#10428
#10424
#10422
#10420

@RonnyPfannschmidt
Copy link
Member

@asottile i believe we need to make our hack search for py on sys.path as it seems like it also conflicts with distro hacks, not just.. - -system-site-packages

@hloeung
Copy link

hloeung commented Nov 21, 2022

Thanks for your time with this one @RonnyPfannschmidt and @asottile

@asottile
Copy link
Member

@hloeung you need to pip install py --ignore-installed just like what I told you in the tox tracker

@rgildein
Copy link
Author

HI @asottile, sorry I missed your replied in the tox tracker. I do not think that this is proper solution, because if I do not know about it, pytest will basically destroyed my tox. This should be fixed, because the solution to reinstall py is really unintuitive.

@asottile
Copy link
Member

use a virtualenv or install py are your choices

@asottile
Copy link
Member

also you didn't miss my message, you quoted it and ignored it

@rgildein
Copy link
Author

You are right, I missed it because you mention that I need install py, but py was installed by tox and pytest just broke it. Yes using virtualenv is solution, but if someone wants to have pytest installed without venv or another package installed by apt have pytest as dependencies, it will break tox.

@asottile
Copy link
Member

normally it won't but the way your packager sets things up causes this cascade

@RonnyPfannschmidt
Copy link
Member

I'd like to check if we can extend the hack in case we find py as distribution, if its reasonably trivial we can slot it for merge else we slot it for documentation

@asottile
Copy link
Member

I'm opposed to that, it's not simple and quite fragile and there's an easy solution already

it's also too late (we already discussed this)

@KevinMarquette
Copy link

Team, this issue still needs to be resolved.

This is still broken when using --system-site-packages unless --ignore-installed is 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-installed has 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 py shim 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.

@asottile
Copy link
Member

Team, this issue still needs to be resolved.

ok first off this is free software so nothing needs to be resolved -- it's not appropriate to demand work in this way

This is still broken when using --system-site-packages unless --ignore-installed is used

as above, --system-site-packages is already a hack at best -- you're explicitly deviating from the libraries your OS packager intended you to use -- why not use your packager's pytest too 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. both virtualenv and venv default to this mode and only in exceptional cases do you use --system-site-packages

that is not a viable permanent solution

it absolutely is -- py is deprecated, so it's only as "permanent" as py -- I'd recommend moving off of --system-site-packages personally or eliminating / upgrading whatever part of your stack is dependent on the deprecated py library

Using --ignore-installed has the side-effect of reinstalling all packages even if there are no changes from the last time you ran it.

you only need it for py -- not all your packages

We can't say this is working as intended or be satisfied with the poor user experience this causes to the larger python community.

the larger python community is not using, and does not recommend, --system-site-packages and py is deprecated -- if you need to bring in a deprecated library in a not-recommended setup this is by all means acceptable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants