Skip to content

Commit

Permalink
improved default python vars (#1100)
Browse files Browse the repository at this point in the history
* feat: expanded default python variables

This set of environment variables seems to be the best set of default production settings
for most python projects.

* docs: adding py variable docs

* lint python file

* update python snapshots

---------

Co-authored-by: Jake Runzer <[email protected]>
  • Loading branch information
iloveitaly and coffee-cup authored May 17, 2024
1 parent eb07dd2 commit 3baec23
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 4 deletions.
17 changes: 17 additions & 0 deletions docs/pages/docs/providers/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,20 @@ python main.py
These directories are cached between builds

- Install: `~/.cache/pip`

## Environment Variables

The following environment variables are set by default:

```shell
PYTHONFAULTHANDLER=1
PYTHONUNBUFFERED=1
PYTHONHASHSEED=random
PYTHONDONTWRITEBYTECODE=1

PIP_NO_CACHE_DIR=1
PIP_DISABLE_PIP_VERSION_CHECK=1
PIP_DEFAULT_TIMEOUT=100
```

These can be overwritten by the `--env` option.
25 changes: 21 additions & 4 deletions src/providers/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ impl Provider for PythonProvider {
plan.set_start_phase(start);
}

plan.add_variables(EnvironmentVariables::from([(
"PYTHONUNBUFFERED".to_owned(),
"1".to_owned(),
)]));
plan.add_variables(PythonProvider::default_python_environment_variables());

if app.includes_file("poetry.lock") {
plan.add_variables(EnvironmentVariables::from([(
Expand Down Expand Up @@ -301,6 +298,26 @@ impl PythonProvider {
.map(|m| m.get(2).unwrap().as_str().to_string()))
}

fn default_python_environment_variables() -> EnvironmentVariables {
let python_variables = vec![
("PYTHONFAULTHANDLER", "1"),
("PYTHONUNBUFFERED", "1"),
("PYTHONHASHSEED", "random"),
("PYTHONDONTWRITEBYTECODE", "1"),
("PIP_NO_CACHE_DIR", "1"),
("PIP_DISABLE_PIP_VERSION_CHECK", "1"),
("PIP_DEFAULT_TIMEOUT", "100"),
];

let mut env_vars = EnvironmentVariables::new();

for (key, value) in python_variables {
env_vars.insert(key.to_owned(), value.to_owned());
}

env_vars
}

fn get_nix_python_package(app: &App, env: &Environment) -> Result<Pkg> {
// Fetch python versions into tuples with defaults
fn as_default(v: Option<Match>) -> &str {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__multiple_providers.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ expression: plan
"NIXPACKS_METADATA": "node,python,go,deno",
"NODE_ENV": "production",
"NPM_CONFIG_PRODUCTION": "false",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__node_python.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ expression: plan
"NIXPACKS_METADATA": "node,python",
"NODE_ENV": "production",
"NPM_CONFIG_PRODUCTION": "false",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_2_runtime.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_django.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python,django,postgres",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_django_mysql.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python,django",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python,django,postgres",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_numpy.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_pdm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ expression: plan
"variables": {
"NIXPACKS_METADATA": "python,pdm",
"NIXPACKS_PDM_VERSION": "2.7.4",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_poetry.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ expression: plan
"variables": {
"NIXPACKS_METADATA": "python,poetry",
"NIXPACKS_POETRY_VERSION": "1.3.1",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_postgres.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python,postgres",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_procfile.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/generate_plan_tests__python_setuptools.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ expression: plan
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
Expand Down

0 comments on commit 3baec23

Please sign in to comment.