kuza
kuza

Reputation: 3031

Is it possible to use environment variables from “.env” in packages section of the “Pipfile”

I have to use limited credentials to be able to install my packages from the private repository but I wish not to commit them with my Pipile.

Here is a simple legitimate use case:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
pytest = "*"

[packages]
my-package = {git = "https://${USER}:${TOKEN}@bitbucket.org/my-team/my-package.git",ref = "v0.1"}

[requires]
python_version = "3.7"

[scripts]
show-credentials = "echo ${USER}:${TOKEN}"

And having in .env file this content:

USER=foo
TOKEN=bar

Executing pipenv run show-credentials will show current credentials as foo:bar but won’t respect these environment variables for installing my-package.

Is there any possible workaround or it might be a good feature request for the pipenv?

Upvotes: 6

Views: 4890

Answers (3)

chrimaho
chrimaho

Reputation: 684

As mentioned in @davegravy's answer, the .env file is only loaded during pipenv run and pipenv shell.

One alternative might be to call pipenv install from within the pipenv run.
Like this:

pipenv run pipenv install

In this way, the .env will be loaded by pipenv run, then it will successfully executed in pipenv run.

Note, this method will only work if you have pipenv already installed within your existing pipenv environment.

Upvotes: 0

Tlaloc-ES
Tlaloc-ES

Reputation: 5282

In the doc tells that you can https://pipenv-fork.readthedocs.io/en/latest/advanced.html

but I tried and fails I put a Github issue because doesnt work https://github.com/pypa/pipenv/issues/5033

Pipenv fails if you put ${user}:${password} but not ${userpassword}

Upvotes: 0

davegravy
davegravy

Reputation: 934

It's not clear from the documentation but from testing it appears .env is only read during pipenv run and pipenv shell. It's not run during pipenv sync or pipenv install.

I say this because it doesn't appear to inject into the [[source]] section unless the environment variables are set outside the virtual env... i.e. USER=my_user TOKEN=my_token pipenv install

Upvotes: 5

Related Questions