Skip to content

Conversation

@matteius
Copy link
Member

Summary

Adds a new PIPENV_PROJECT_DIR environment variable that is automatically set when running pipenv run or pipenv shell. This variable contains the path to the project root directory (where the Pipfile is located).

Motivation

This addresses a long-standing feature request (#2241) from 2018. Users needed a way to consistently run scripts that require project root relative paths, regardless of their current working directory.

Use Cases

  1. Running scripts from subdirectories:

    cd project/src
    pipenv run test  # Now works because script can use $PIPENV_PROJECT_DIR
  2. Scripts in [scripts] section:

    [scripts]
    test = "pytest $PIPENV_PROJECT_DIR/tests"
    mypy = "mypy --config-file=$PIPENV_PROJECT_DIR/mypy.ini $PIPENV_PROJECT_DIR/src"
  3. Django projects with src layout:

    [scripts]
    manage = "python $PIPENV_PROJECT_DIR/src/manage.py"

Changes

  1. pipenv/routines/shell.py:

    • Set PIPENV_PROJECT_DIR in both do_shell() and do_run()
  2. docs/configuration.md:

    • Added new "Runtime Environment Variables" section
    • Documents PIPENV_ACTIVE, PIPENV_PROJECT_DIR, and VIRTUAL_ENV
    • Provides usage examples

Testing

# From project root
$ pipenv run bash -c 'echo $PIPENV_PROJECT_DIR'
/home/user/myproject

# From subdirectory
$ cd src && pipenv run bash -c 'echo $PIPENV_PROJECT_DIR'
/home/user/myproject

Related Environment Variables

Variable Description
PIPENV_ACTIVE Set to 1 when inside a Pipenv environment
PIPENV_PROJECT_DIR Path to project root (NEW)
PIPENV_PIPFILE Full path to Pipfile (user-configurable)
VIRTUAL_ENV Path to the virtualenv

Fixes #2241


Pull Request opened by Augment Code with guidance from the PR author

Adds a new PIPENV_PROJECT_DIR environment variable that is automatically
set when running 'pipenv run' or 'pipenv shell'. This variable contains
the path to the project root directory (where the Pipfile is located).

This allows scripts defined in the [scripts] section of Pipfile to
reference project-relative paths regardless of the current working
directory:

  [scripts]
  test = "pytest $PIPENV_PROJECT_DIR/tests"
  mypy = "mypy --config-file=$PIPENV_PROJECT_DIR/mypy.ini src"

This is particularly useful when:
- Running pipenv commands from subdirectories
- Scripts need to reference config files in the project root
- You have a src layout and need consistent paths

Also documents the runtime environment variables (PIPENV_ACTIVE,
PIPENV_PROJECT_DIR, VIRTUAL_ENV) in the configuration docs.

Fixes #2241
@matteius matteius merged commit 9411b68 into main Dec 10, 2025
19 of 20 checks passed
@matteius matteius deleted the feature/pipenv-project-dir-2241 branch December 10, 2025 23:08
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

Successfully merging this pull request may close these issues.

Provide a way to consistently run scripts that require project root relative paths

2 participants