Python: Improved error message when virtualenv doesn't exist (#5069)

This commit is contained in:
Justin Van Patten 2020-07-28 20:28:14 +00:00 committed by GitHub
parent b1ee3a3da6
commit b364296521
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 7 deletions

View file

@ -15,6 +15,9 @@ CHANGELOG
- Add ability to create a stack based on the config from an existing stack
[#5062](https://github.com/pulumi/pulumi/pull/5062)
- Python: Improved error message when `virtualenv` doesn't exist
[#5069](https://github.com/pulumi/pulumi/pull/5069)
## 2.7.1 (2020-07-22)
- Fix logic to parse pulumi venv on github action

View file

@ -751,8 +751,11 @@ func pythonCommands() []string {
commands = append(commands, "source venv/bin/activate")
}
// Install dependencies within the virtualenv
commands = append(commands, "pip3 install -r requirements.txt")
// Update pip, setuptools, and wheel within the virtualenv.
commands = append(commands, "python -m pip install --upgrade pip setuptools wheel")
// Install dependencies within the virtualenv.
commands = append(commands, "python -m pip install -r requirements.txt")
return commands
}

View file

@ -178,7 +178,7 @@ func (host *pythonLanguageHost) Run(ctx context.Context, req *pulumirpc.RunReque
virtualenv = filepath.Join(cwd, virtualenv)
}
if !python.IsVirtualEnv(virtualenv) {
return nil, errors.Errorf("%q doesn't appear to be a virtual environment", virtualenv)
return nil, python.NewVirtualEnvError(host.virtualenv, virtualenv)
}
cmd = python.VirtualEnvCommand(virtualenv, "python", args...)
} else {

View file

@ -34,7 +34,16 @@ if [ -n "${virtualenv:-}" ] ; then
# Run python from the virtual environment.
"$virtualenv/bin/python" -u -m pulumi.policy "$1" "$2"
else
echo "\"$virtualenv\" doesn't appear to be a virtual environment"
if [ -d "$virtualenv" ]; then
1>&2 echo "The 'virtualenv' option in PulumiPolicy.yaml is set to \"$virtualenv\", but \"$virtualenv\" doesn't appear to be a virtual environment."
else
1>&2 echo "The 'virtualenv' option in PulumiPolicy.yaml is set to \"$virtualenv\", but \"$virtualenv\" doesn't exist."
fi
1>&2 echo "Run the following commands to create the virtual environment and install dependencies into it:"
1>&2 echo " 1. python3 -m venv $virtualenv"
1>&2 echo " 2. $virtualenv/bin/python -m pip install --upgrade pip setuptools wheel"
1>&2 echo " 3. $virtualenv/bin/python -m pip install -r $PWD/requirements.txt"
1>&2 echo "For more information see: https://www.pulumi.com/docs/intro/languages/python/#virtual-environments"
exit 1
fi
else

View file

@ -28,7 +28,12 @@ if defined pulumi_runtime_python_virtualenv (
"%pulumi_runtime_python_virtualenv%\Scripts\python.exe" -u -m pulumi.policy %pulumi_policy_python_engine_address% %pulumi_policy_python_program%
exit /B
) else (
echo "%pulumi_runtime_python_virtualenv%" doesn't appear to be a virtual environment
echo The 'virtualenv' option in PulumiPolicy.yaml is set to %pulumi_runtime_python_virtualenv%, but %pulumi_runtime_python_virtualenv% doesn't appear to be a virtual environment. 1>&2
echo Run the following commands to create the virtual environment and install dependencies into it: 1>&2
echo 1. python -m venv %pulumi_runtime_python_virtualenv% 1>&2
echo 2. %pulumi_runtime_python_virtualenv%\Scripts\python.exe -m pip install --upgrade pip setuptools wheel 1>&2
echo 3. %pulumi_runtime_python_virtualenv%\Scripts\python.exe -m pip install -r %cd%\requirements.txt 1>&2
echo For more information see: https://www.pulumi.com/docs/intro/languages/python/#virtual-environments 1>&2
exit 1
)
) else (

View file

@ -22,7 +22,16 @@ if [ -n "${PULUMI_RUNTIME_VIRTUALENV:-}" ] ; then
# Run python from the virtual environment.
"$PULUMI_RUNTIME_VIRTUALENV/bin/python" -u -m pulumi.dynamic $@
else
echo "\"$PULUMI_RUNTIME_VIRTUALENV\" doesn't appear to be a virtual environment"
if [ -d "$PULUMI_RUNTIME_VIRTUALENV" ]; then
1>&2 echo "The 'virtualenv' option in Pulumi.yaml is set to \"$PULUMI_RUNTIME_VIRTUALENV\", but \"$PULUMI_RUNTIME_VIRTUALENV\" doesn't appear to be a virtual environment."
else
1>&2 echo "The 'virtualenv' option in Pulumi.yaml is set to \"$PULUMI_RUNTIME_VIRTUALENV\", but \"$PULUMI_RUNTIME_VIRTUALENV\" doesn't exist."
fi
1>&2 echo "Run the following commands to create the virtual environment and install dependencies into it:"
1>&2 echo " 1. python3 -m venv $PULUMI_RUNTIME_VIRTUALENV"
1>&2 echo " 2. $PULUMI_RUNTIME_VIRTUALENV/bin/python -m pip install --upgrade pip setuptools wheel"
1>&2 echo " 3. $PULUMI_RUNTIME_VIRTUALENV/bin/python -m pip install -r $PWD/requirements.txt"
1>&2 echo "For more information see: https://www.pulumi.com/docs/intro/languages/python/#virtual-environments"
exit 1
fi
else

View file

@ -11,7 +11,12 @@ if defined PULUMI_RUNTIME_VIRTUALENV (
"%PULUMI_RUNTIME_VIRTUALENV%\Scripts\python.exe" -u -m pulumi.dynamic %*
exit /B
) else (
echo "%PULUMI_RUNTIME_VIRTUALENV%" doesn't appear to be a virtual environment
echo The 'virtualenv' option in Pulumi.yaml is set to %PULUMI_RUNTIME_VIRTUALENV%, but %PULUMI_RUNTIME_VIRTUALENV% doesn't appear to be a virtual environment. 1>&2
echo Run the following commands to create the virtual environment and install dependencies into it: 1>&2
echo 1. python -m venv %PULUMI_RUNTIME_VIRTUALENV% 1>&2
echo 2. %PULUMI_RUNTIME_VIRTUALENV%\Scripts\python.exe -m pip install --upgrade pip setuptools wheel 1>&2
echo 3. %PULUMI_RUNTIME_VIRTUALENV%\Scripts\python.exe -m pip install -r %cd%\requirements.txt 1>&2
echo For more information see: https://www.pulumi.com/docs/intro/languages/python/#virtual-environments 1>&2
exit 1
)
) else (

View file

@ -81,6 +81,29 @@ func IsVirtualEnv(dir string) bool {
return false
}
// NewVirtualEnvError creates an error about the virtual environment with more info on how to resolve the issue.
func NewVirtualEnvError(dir, fullPath string) error {
pythonBin := "python3"
if runtime.GOOS == windows {
pythonBin = "python"
}
venvPythonBin := filepath.Join(fullPath, virtualEnvBinDirName(), "python")
message := "doesn't appear to be a virtual environment"
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
message = "doesn't exist"
}
commandsText := fmt.Sprintf(" 1. %s -m venv %s\n", pythonBin, fullPath) +
fmt.Sprintf(" 2. %s -m pip install --upgrade pip setuptools wheel\n", venvPythonBin) +
fmt.Sprintf(" 3. %s -m pip install -r requirements.txt\n", venvPythonBin)
return errors.Errorf("The 'virtualenv' option in Pulumi.yaml is set to %q, but %q %s; "+
"run the following commands to create the virtual environment and install dependencies into it:\n\n%s\n\n"+
"For more information see: https://www.pulumi.com/docs/intro/languages/python/#virtual-environments",
dir, fullPath, message, commandsText)
}
// ActivateVirtualEnv takes an array of environment variables (same format as os.Environ()) and path to
// a virtual environment directory, and returns a new "activated" array with the virtual environment's
// "bin" dir ("Scripts" on Windows) prepended to the `PATH` environment variable and `PYTHONHOME` variable