floatingpurr
floatingpurr

Reputation: 8559

Modifying PYTHONPATH using .env files with Pipenv

I'm trying to add a project directory to the PYTHONPATH using pipenv. Following the hint of this post, I created a .env file in order to modify the path used by the virtualenv managed by pipenv.

I created the .env file (in /foo/bar/myProject) as follows:

PYTHONPATH=${PYTHONPATH}:${PWD}

but when I activate the virtualenv, this is the new path:

$ python -c "import sys; print(sys.path)"
['', '/foo/bar/${PYTHONPATH}', '/foo/bar/${PWD}', '/foo/bar/myProject',...]

It correctly adds /foo/bar/myProject to the PYTHONPATH. However, looks like it adds also two extra entries with unsubstituted environment variables.

Why does it happen and how can I avoid this?

Note: I'm using the Z shell (probably it does not matter).

Upvotes: 3

Views: 3926

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 149796

You probably don't have the $PYTHONPATH envariable set in your shell, so pipenv stupidly replaces ${PYTHONPATH} with the value in the .env file (i.e. ${PYTHONPATH}:${PWD}). Then ${PWD} is successfully expanded, giving you the final value PYTHONPATH=${PYTHONPATH}:${PWD}:/foo/bar/myProject. That leads to the weird looking sys.path. You can fix the issue by omitting ${PYTHONPATH} from the value:

PYTHONPATH=${PWD}

or set it to some value before running pipenv:

export PYTHONPATH=/path/to/dir
pipenv shell

Tested with pipenv version 2018.11.26.

Upvotes: 4

Related Questions