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

Add an example VENV_DIR constant to dev session in cookbook? #588

Closed
NickleDave opened this issue Mar 23, 2022 · 2 comments · Fixed by #591
Closed

Add an example VENV_DIR constant to dev session in cookbook? #588

NickleDave opened this issue Mar 23, 2022 · 2 comments · Fixed by #591

Comments

@NickleDave
Copy link
Contributor

NickleDave commented Mar 23, 2022

Hi nox devs and maintainers, thank you for this great tool.

I'd like to suggest a very minor tweak to the dev session example in the cookbook
https://nox.thea.codes/en/stable/cookbook.html#instant-dev-environment

This example is exactly one of the things that I was hoping nox could give me.
But I got confused because I defined my VENV_DIR as simply pathlib.Path('.venv') which resulted in the following cryptic error:

$ nox -s dev      
nox > Running session dev
nox > Creating virtual environment (virtualenv) using python in .nox/dev
nox > python -m pip install virtualenv
nox > virtualenv .venv
nox > Program .venv/bin/python not found.
nox > Session dev failed.

After staring at examples in Scikit-HEP I realized that what I needed to do was chain a call of Path.resolve() method onto my VENV_DIR:

VENV_DIR = pathlib.Path('./.venv').resolve()   # nox will find this because of the absolute path 

Not quite clear to me why my simple pathlib.Path('venv') without the resolve doesn't work -- somehow nox is runnning things from a place that is not in my cwd? (I realize this is just my blissful ignorance about the $PATH speaking)

Anyway, I'm suggesting to just add an example VENV_DIR like this to the dev recipe, e.g.

import os

import nox

# It's a good idea to keep your dev session out of the default list
# so it's not run twice accidentally
nox.options.sessions = [...] # Sessions other than 'dev'

VENV_DIR = pathlib.Path('./.venv').resolve()   # need to use absolute path 

@nox.session
def dev(session: nox.Session) -> None:
    ...

There is a comment explaining that VENV_DIR needs to be defined but the bit about needing to resolve() it would help I think

@NickleDave NickleDave changed the title Add an VENV constant to dev session in cookbook? Add an example VENV_DIR constant to dev session in cookbook? Mar 23, 2022
@FollowTheProcess
Copy link
Collaborator

When run, Nox will change directories to the one containing the noxfile.py See here:

nox/nox/tasks.py

Lines 94 to 99 in bf1c226

# Move to the path where the Noxfile is.
# This will ensure that the Noxfile's path is on sys.path, and that
# import-time path resolutions work the way the Noxfile author would
# guess. The original working directory (the directory that Nox was
# invoked from) gets stored by the .invoke_from "option" in _options.
os.chdir(noxfile_parent_dir)

That could be why your non-resolved path is causing some weirdness. As for including the example I'm all for it, if you've tripped up this way there's a good chance others will too.

The cookbook is just a part of the project docs at docs/cookbook.rst if you fancy sending a PR 👍🏻

@NickleDave
Copy link
Contributor Author

When run, Nox will change directories to the one containing the noxfile.py
That could be why your non-resolved path is causing some weirdness.

Thank you @FollowTheProcess -- my cwd is the same as where noxfile.py lives when I run into this issue so I'm not sure what's going on. Might be very machine specific

As for including the example I'm all for it, if you've tripped up this way there's a good chance others will too.
The cookbook is just a part of the project docs at docs/cookbook.rst if you fancy sending a PR 👍🏻

Will do.
I very much appreciate the cookbook you all added, I did just think this minor tweak might help someone avoid getting tripped up the same way

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

Successfully merging a pull request may close this issue.

2 participants