python-setenv 0.0.1__py3-none-any.whl → 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-setenv
3
+ Version: 0.0.2
4
+ Summary: Set env (e.g. HF_TOKEN) from colab/kaggle secrets or dotenv .env env files
5
+ Author-email: ffreemt <yucongo+fmt@gmail.com>
6
+ License-File: LICENSE
7
+ Requires-Python: >=3.8
8
+ Requires-Dist: python-dotenv>=1.0.0
9
+ Description-Content-Type: text/markdown
10
+
11
+ <!---[![pytest](https://github.com/ffreemt/python-setenv/actions/workflows/routine-tests.yml/badge.svg)](https://github.com/ffreemt/python-setenv/actions)-->
12
+ # python-setenv
13
+ [![pytest](https://github.com/ffreemt/python-setenv/actions/workflows/routine-tests.yml/badge.svg)](https://github.com/ffreemt/python-setenv/actions)[![python](https://img.shields.io/static/v1?label=python+&message=3.8%2B&color=blue)](https://www.python.org/downloads/)[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)[![PyPI version](https://badge.fury.io/py/python-setenv.svg)](https://badge.fury.io/py/python-setenv)
14
+
15
+ Set an environ variable from colab, kaggle or dotenv (search default .env/dotenv/env)
16
+
17
+ ### Why `python-setenv`
18
+
19
+ colab and kaggle both provide a way to manage secrets (typically API tokens).
20
+
21
+ `python-setenv` is mainly for running ipynb (jupyter notebook) files in colab/kaggle or cloud instance when we need to set an environ variable, for example, `HF_TOKEN` to download models or datasets from huggingdace hub, other scenarios include `WANDB_API_KEY` or `NGROK_AUTHCODE` or `OPENAI_API_KEY` etc.
22
+
23
+ When running an ipynb in a cloud instance, we may use `dotenv` (`pip install python-dotenv`) to set environ varibales based on `.env`.
24
+
25
+ ### Install it
26
+ ```
27
+ pip install python-setenv
28
+ ```
29
+
30
+ ### Setup Secrets or Upload `.env`
31
+
32
+ * In colab, set Secrets
33
+
34
+ <img src="https://github.com/ffreemt/python-setenv/raw/main/img/colab.png" width="300" />
35
+
36
+ <!---![](img/colab.png)-->
37
+
38
+ * In kaggle, set Add-ons/Secrets
39
+
40
+ <img src="https://github.com/ffreemt/python-setenv/raw/main/img/kaggle.png" width="300" />
41
+
42
+ <!---![](./img/kaggle.png)-->
43
+
44
+ * In other jupyter environ/cloud instance, upload .env, with contents, e.g.
45
+ ```
46
+ HF_TOKEN=...
47
+ WANDB_API_KEY=...
48
+ ```
49
+ In some cases, files start with a dot are not allowed. Rename `.env` to `dotenv` or `env` instead, `setenv` will auto-search for `.env`, `dotenv` and `env`.
50
+
51
+ ## Use it
52
+ ```
53
+ from setenv import setenv
54
+
55
+ # e.g.
56
+ setenv("HF_TOKEN")
57
+ setenv("WANDB_API_KEY")
58
+ setenv("NGROK_AUTHCODE")
59
+ ```
60
+
61
+ Sometimes we want to set HF_TOKEN to HF_TOKEN_W (with write-permission).
62
+ ```
63
+ from setenv import setenv
64
+ setenv(env_var="HF_TOKEN", source_var="HF_TOKEN_W")
65
+ ```
66
+ This is effectively equivalent to `os.environ["HF_TOKEN"] = get_secret("HF_TOKEN_W")` when in colab or kaggle, or `os.environ["HF_TOKEN"] = dotenv.dotenv_values().get("HF_TOKEN_W")`.
@@ -0,0 +1,4 @@
1
+ python_setenv-0.0.2.dist-info/METADATA,sha256=K5Tw5D4A-3vDL08QweD3BxHFAYhZUzCOVL9bszefpIg,2847
2
+ python_setenv-0.0.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
3
+ python_setenv-0.0.2.dist-info/licenses/LICENSE,sha256=_uswrdKCdw7w6eijHhtUmEBb6YEg7uk5lYnf_FJpS3o,1085
4
+ python_setenv-0.0.2.dist-info/RECORD,,
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ffreemt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,7 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: python-setenv
3
- Version: 0.0.1
4
- Summary: Set env (e.g. HF_TOKEN) from colab/kaggle secrets or dotenv .env env files
5
- Author-email: ffreemt <yucongo+fmt@gmail.com>
6
- Requires-Python: >=3.10
7
- Requires-Dist: python-dotenv>=1.2.1
@@ -1,5 +0,0 @@
1
- setenv/__init__.py,sha256=ZRkV6G4CkdVIZwsVXXtn7jrP8YSIHKPDLOifoI7z8rc,3558
2
- setenv/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- python_setenv-0.0.1.dist-info/METADATA,sha256=ZT4-kPoD3xl7iNF_aV6exyWxGX-QpK0YmcW8zUDFD9s,247
4
- python_setenv-0.0.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
5
- python_setenv-0.0.1.dist-info/RECORD,,
setenv/__init__.py DELETED
@@ -1,131 +0,0 @@
1
- """Set an environ var in colab/kaggle/dotenv(search for .env dotenv env).
2
-
3
- !pip install -q python-dotenv
4
- """
5
- # pylint: disable=import-outside-toplevel,too-many-statements,too-many-branches
6
-
7
- import os
8
- # import sys
9
-
10
- from pathlib import Path
11
- from dotenv import dotenv_values, find_dotenv
12
- # from loguru import logger
13
-
14
- __version__ = "0.0.1"
15
-
16
-
17
- def hello() -> str:
18
- return "Hello from python-setenv!"
19
-
20
-
21
- # def set_env(
22
- def setenv(
23
- env_var="HF_TOKEN", source_var=None, envfile=None, override=False,
24
- # verbose=False
25
- ):
26
- """
27
- Set environ var via google userdat/kaggle_secrets/[.env/dotenv/env].
28
-
29
- Args:
30
- ----
31
- env_var:
32
- env var to set, default HF_TOKEN.
33
- source_var:
34
- var from google/kaggle secret/userdata
35
- or defined in .env/dotenv/env
36
- envfile: file to read from, default, search
37
- for ['.env', 'dotenv', 'env']
38
- override:
39
- reset if set to True
40
- x verbose:
41
- for loguru.logging, print TRACE if set
42
-
43
- Returns:
44
- -------
45
- value of the env var.
46
- """
47
- _ = '''
48
- if verbose:
49
- logger.remove()
50
- logger.add(sys.stderr, level="TRACE") # default "DEBUG"
51
- # '''
52
- if not override and os.getenv(env_var):
53
- return os.getenv(env_var)
54
-
55
- # if override:
56
- save_env_var = os.getenv(env_var)
57
- if os.getenv(env_var) is not None:
58
- del os.environ[env_var]
59
-
60
- if source_var is None:
61
- source_var = env_var
62
-
63
- # if KAGGLE
64
- if "KAGGLE_KERNEL" in " ".join(os.environ.keys()):
65
- # import kaggle_web_client # type: ignore
66
- from kaggle_secrets import UserSecretsClient # type: ignore
67
-
68
- user_secrets = UserSecretsClient()
69
- try:
70
- os.environ[env_var] = user_secrets.get_secret(source_var)
71
- return os.getenv(env_var)
72
- except Exception:
73
- return ""
74
-
75
- # try colab COLAB_NOTEBOOK
76
- if "COLAB_NOTEBOOK" in " ".join(os.environ.keys()):
77
- from google.colab import userdata # noqa # type: ignore
78
- try:
79
- os.environ[env_var] = userdata.get(source_var)
80
- return os.getenv(env_var)
81
- except Exception:
82
- return ""
83
-
84
- # .env dotenv env
85
- # envfile = None
86
-
87
- # cwd
88
- print(f"cwd: {Path.cwd()}, {envfile=}")
89
-
90
- if envfile is None:
91
- for _ in [".env", "dotenv", "env"]:
92
- # filepath = Path.cwd() / _
93
- # envfile = find_dotenv(filepath.as_posix())
94
- filepath = _
95
- envfile = find_dotenv(filepath)
96
- if envfile:
97
- # logger.trace(f"Found {envfile=}")
98
- print(f"Found {envfile=}")
99
- break
100
-
101
- if envfile:
102
- print(f"loading {envfile=} with dotenv_values(envfile)")
103
- if dotenv_values(envfile).get(source_var):
104
- _ = dotenv_values(envfile).get(source_var)
105
- if _: # need to be a str
106
- os.environ[env_var] = _
107
- print(f"{envfile=}: set {env_var}={source_var}")
108
- if os.getenv(env_var):
109
- return os.getenv(env_var)
110
-
111
- # try manual input?
112
- print(
113
- f"""
114
- Unable to set {env_var}={source_var},
115
- not in colab or Secrets not set, not kaggle
116
- or Secrets not set, no .env/dotenv/env file
117
- in the current working dir or parent dirs or env not present."""
118
- )
119
-
120
- # restore?
121
- if override:
122
- # save_env_var may be None
123
- if save_env_var:
124
- if os.getenv(env_var) is not None:
125
- os.environ[env_var] = save_env_var
126
-
127
- print(f"Restore {env_var=}")
128
-
129
- return ""
130
-
131
- __all__ = ("__version__", "setenv",)
setenv/py.typed DELETED
File without changes