Get site-packages directory in nox

504 views Asked by At

In tox, I can get the site-packages directory of the current environment with the envsitepackagesdir magic variable. How do I do the same thing in nox? This is particularly useful when trying to get coverage of my package.

import nox

@nox.session()
def test(session):
    session.install('.')
    session.install('pytest', 'pytest-cov')
    session.run('pytest', '--cov', f'{session.virtualenv.site_packages_dir}/mypackage')
2

There are 2 answers

0
drhagen On

As long as you are actually installing a nox environment (not using venv_backend='none'), you can calculate this from the location of the virtual environment and platform.sys:

import nox

@nox.session()
def test(session):
    if platform.system() == "Windows":
        site_packages = f'{session.virtualenv.location}/Lib/site-packages'
    else:
        site_packages = f'{session.virtualenv.location}/lib/python{session.python}/site-packages'

    session.install('.')
    session.install('pytest', 'pytest-cov')
    session.run('pytest', '--cov', f'{site_packages}/mypackage')
0
drhagen On

The envsitepackagesdir is not actually necessary for coverage. You can pass the module name to --cov and coverage will compute the coverage on that module wherever it is imported:

import nox

@nox.session()
def test(session):
    session.install('.')
    session.install('pytest', 'pytest-cov')
    session.run('pytest', '--cov', 'mypackage')