Nox is a command-line tool that automates testing in multiple Python environments, s imilar to tox. Unlike tox, Nox uses a standard Python file for configuration.
Install nox via pip:
$ pip install --user noxNox is configured via a noxfile.py file in your project’s directory. For example,
import nox
@nox.session
def tests(session):
session.install('pytest')
session.run('pytest')
@nox.session
def lint(session):
session.install('flake8')
session.run('flake8', '--import-order-style', 'google')By default, nox uses virtualenv as default backend to create the virtual environment.
You are not limited to virtualenv though, there is a selection of backends you can choose from as venv, conda, mamba:
@nox.session(venv_backend="venv")
def tests(session):
passBy default, when calling nox command (see 2. Activate the environment),
nox will create a new virtual environment for each session using the same interpreter that Nox uses.
Same as tox, you do not need to activate the virtual environment manually. to run both of these sessions mentioned above, use the following command:
$ noxBy default, under the project directory, nox will create a cache directory .nox which contains the
virtual environments defined with nox sessions.
You can specify the Python packages that are required for running the tests in the virtual environment, like this:
session.install("package1", "package2", ...)If you have a list of packages in requirements.txt, you can use
session.install("-r", "requirements.txt")If your project is a Python package and you want to to install it:
session.install("-e", ".")No need to deactivate the virtual environment manually, nox will take care of it automatically.
To remove the cached virtual environments, you can run the following command:
$ rm -rf .noxYou can tell Nox to use a different Python interpreter/version by specifying the python argument (or its alias py) to @nox.session:
@nox.session(python="3.12")
def tests(session):
passYou can also tell Nox to run your session against multiple Python interpreters. Nox will create a separate virtualenv and run the session for each interpreter you specify.
@nox.session(python=["3.11", "3.12", "pypy-6.0"])
def tests(session):
passFor more information about nox, please look into the official documentation
Happy Coding!