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

environment variables in pipfile scripts are not recognized correctly #3901

Closed
Madoshakalaka opened this issue Aug 19, 2019 · 17 comments
Closed
Labels
Category: CLI Issue relates to the CLI triage Type: Possible Bug This issue describes a possible bug in pipenv.

Comments

@Madoshakalaka
Copy link

Madoshakalaka commented Aug 19, 2019

When running scripts from pipfile. Unix style environment variables are wrongly recognized as commands.

Expected result

hello=world is recognized as setting an environment variable

Actual result

Error: the command hello=world (from generic-script) could not be found within PATH.

Steps to replicate

  • have the following part in pipfile
    [scripts]
    generic-script = 'hello=world python --version'
    
  • $ pipenv run generic-script
@Madoshakalaka
Copy link
Author

fyi: npm scripts in package.json handles this correctly

@Madoshakalaka
Copy link
Author

a workaround is to use $ env hello=world <commands> on unix systems.

@frostming frostming added Category: CLI Issue relates to the CLI Type: Bug 🐛 This issue is a bug. labels Aug 20, 2019
@frostming frostming added Type: Enhancement 💡 This is a feature or enhancement request. and removed Type: Bug 🐛 This issue is a bug. labels Aug 20, 2019
@danieldaeschle
Copy link

What about multiple environment arguments?

@Hammond95
Copy link

Hello everyone,
I am trying to contribute to the project trying to solve this issue.

I made a first hackish version that fixes the error of the issue.

Something that is missing, is defining which use cases this feature should cover.
Some examples:

  • do we want to solve variables into the command? ${MYVAR} or $MY_VAR
  • do we want to allow the execution of multiple commands if separated by semicolon? echo "hello"; python --version
  • do we want to allow multiple command execution with | or && or || operators?

I think that the purpose of this feature was to give an easy tool for a developer to run local script to do tests.

@jyotirmay123
Copy link

jyotirmay123 commented Oct 26, 2021

I want to use something like below:

[scripts]
docker_build = "docker build --build-arg PYPI_CREDS=$env:PYPI_CREDS -t myimage:latest ."

Whereas PYPI_CREDS has already been defined and loaded by pipenv from .env file.
I am using windows OS.

Now, it is not able to resolve the env variable passed in docker_build script.

Any solution for this?

@jyotirmay123
Copy link

I want to use something like below:

[scripts]
docker_build = "docker build --build-arg PYPI_CREDS=$env:PYPI_CREDS -t myimage:latest ."

Whereas PYPI_CREDS has already been defined and loaded by pipenv from .env file. I am using windows OS.

Now, it is not able to resolve the env variable passed in docker_build script.

Any solution for this?

Okay I got a solution, I needed to use cmd.exe format of accessing the env variable, like below:

[scripts]
docker_build = "docker build --build-arg PYPI_CREDS=%PYPI_CREDS% -t myimage:latest ."

@matteius
Copy link
Member

@Madoshakalaka Could you check if new version 2022.3.23 which has a fix for something similar to this, solves this ticket?

@matteius
Copy link
Member

Its pipenv==2022.3.24 now.

@matteius matteius added the Status: Awaiting Update ⏳ This issue requires more information before assistance can be provided. label Mar 23, 2022
@piranna
Copy link

piranna commented Apr 21, 2022

Why It got closed?

@matteius
Copy link
Member

@piranna because of recent improvements to pipenv and a lack of response on this issue. Should it not be considered closed?

@piranna
Copy link

piranna commented Apr 21, 2022

Should it not be considered closed?

If it's still not possible to define an environment variable in the scripts field in a similar way to how it can be done in npm package.json file, then no, I think it should not be closed. Multiple commands or other advances syntax features can be moved to a script file, but environment variables are needed to exec the commands and falls in the scope of the scripts field, if you don't want to add syntax to it at least provide a way to define them, like an env dict field or something...

@matteius
Copy link
Member

If it's still not possible to define an environment variable in the scripts field in a similar way to how it can be done in npm package.json file, then no, I think it should not be closed.

@piranna There is a new version of pipenv, can you try it and report back? There are so many things going on that I cannot personally triage everything. All I know is that you can define whatever variables you want in a .env file or your environment and then use those in scripts. I ever added a test case about it: https://github.com/pypa/pipenv/pull/4979/files#diff-3fe0ab87ef8fd3ad2eeccf25f9f90375d6013b1a6ff813b883f98aad05db9a8dR214

@jyotirmay123
Copy link

jyotirmay123 commented Apr 22, 2022

If it's still not possible to define an environment variable in the scripts field in a similar way to how it can be done in npm package.json file, then no, I think it should not be closed.

@piranna There is a new version of pipenv, can you try it and report back? There are so many things going on that I cannot personally triage everything. All I know is that you can define whatever variables you want in a .env file or your environment and then use those in scripts. I ever added a test case about it: https://github.com/pypa/pipenv/pull/4979/files#diff-3fe0ab87ef8fd3ad2eeccf25f9f90375d6013b1a6ff813b883f98aad05db9a8dR214

No, You have tested by defining one env variable and accessing it. It worked for me as well.
But when you do it to pass to a different command like

[scripts]
docker_build="docker build -t $HELLO ."

this did not work before, but with the version above pipenv>=2022-03-24 it works. Check my another comment below for descriptive answer which I tried out.

@jyotirmay123
Copy link

jyotirmay123 commented Apr 22, 2022

@Madoshakalaka Could you check if new version 2022.3.23 which has a fix for something similar to this, solves this ticket?

sorry for late response.. I just tested with updated version of pipenv and it works. The pipenv version currently I am using is pipenv, version 2022.4.8.
For better visibility, this below stuff works:

.env

NOTEBOOK_DIR=path-to-your-notebook-dir

pipfile

[scripts]
notebook="jupyter notebook --notebook-dir=$NOTEBOOK_DIR"

terminal

pipenv run notebook   🥇 

@matteius

@matteius matteius reopened this Apr 22, 2022
@matteius matteius added triage Type: Possible Bug This issue describes a possible bug in pipenv. and removed Status: Awaiting Update ⏳ This issue requires more information before assistance can be provided. Type: Enhancement 💡 This is a feature or enhancement request. labels Apr 22, 2022
@matteius
Copy link
Member

Wait I just re-opened this and then re-read the comments. Are you saying @jyotirmay123 it does in fact work pipenv>=2022-03-24 it works? That would make sense since the fix got merged to main on March 12th. I opened this back up to not loose track of it, but if we don't need it to be open, let's close it @piranna

@jyotirmay123
Copy link

jyotirmay123 commented Apr 25, 2022

Wait I just re-opened this and then re-read the comments. Are you saying @jyotirmay123 it does in fact work pipenv>=2022-03-24 it works? That would make sense since the fix got merged to main on March 12th. I opened this back up to not loose track of it, but if we don't need it to be open, let's close it @piranna

@matteius yes, it does work now with the updated version. Thank you very much for the hard work. Cheers.
The ticket can be closed now.

@melyux
Copy link

melyux commented Jun 12, 2024

I think this should be re-opened, because the same syntax that would work in npm's package.json and normal shells, like this

[scripts]
test = "HELLO=world python myscript.py"

doesn't work with Pipfile, even in version 2023.12.1. It says:

Error: the command HELLO=world (from test) could not be found within PATH.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: CLI Issue relates to the CLI triage Type: Possible Bug This issue describes a possible bug in pipenv.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants