mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
Add scripts for virtual environments
This commit adds two rudimentary scripts for interacting with Conda environments and Python virtual environments created with `venv`. Both scripts follow the same pattern: a table of new environment variables is created via the script and the user must activate it by loading it with `load-env`: `load-env (conda-env name-of-env)` The environments are deactivated by sourcing a script, which uses `unlet-env` to remove the environment variables, and restores the old path. In both cases, nesting virtual environments is not supported. As soon as an environment is activated, all information from the previous one is overwritten.
This commit is contained in:
parent
f23f784f57
commit
2b3312fc06
4 changed files with 67 additions and 0 deletions
39
virtual_environments/conda.nu
Normal file
39
virtual_environments/conda.nu
Normal file
|
@ -0,0 +1,39 @@
|
|||
def conda-env [env-name] {
|
||||
let conda-info = (conda info --envs --json | from json)
|
||||
let suffix = (if $env-name == "base" {""} {(["envs" $env-name] | path join)})
|
||||
let env-dir = ([$conda-info.root_prefix $suffix] | path join)
|
||||
let old-path = ($nu.path | str collect (path-sep))
|
||||
let env-path = (if (windows?) { (conda-create-path-windows $env-dir) } { (conda-create-path-unix $env-dir) })
|
||||
let new-path = ([$env-path $nu.path] | flatten | str collect (path-sep))
|
||||
[[name, value];
|
||||
[CONDA_DEFAULT_ENV $env-name]
|
||||
[CONDA_PREFIX $env-dir]
|
||||
[CONDA_PROMPT_MODIFIER $"[($env-name)]"]
|
||||
[CONDA_SHLVL "1"]
|
||||
[CONDA_OLD_PATH $old-path]
|
||||
[PATH $new-path]]
|
||||
}
|
||||
|
||||
def conda-create-path-windows [env-dir] {
|
||||
[
|
||||
$env-dir
|
||||
([$env-dir "Scripts"] | path join)
|
||||
([$env-dir "Library" "mingw-w64"] | path join)
|
||||
([$env-dir "Library" "bin"] | path join)
|
||||
([$env-dir "Library" "usr" "bin"] | path join)
|
||||
]
|
||||
}
|
||||
|
||||
def conda-create-path-unix [env-dir] {
|
||||
[
|
||||
([$env-dir "bin"] | path join)
|
||||
]
|
||||
}
|
||||
|
||||
def windows? [] {
|
||||
(sys).host.name == "Windows"
|
||||
}
|
||||
|
||||
def path-sep [] {
|
||||
if (windows?) { ";" } { ":" }
|
||||
}
|
6
virtual_environments/conda_deactivate.nu
Normal file
6
virtual_environments/conda_deactivate.nu
Normal file
|
@ -0,0 +1,6 @@
|
|||
let-env PATH = $nu.env.CONDA_OLD_PATH
|
||||
unlet-env CONDA_PROMPT_MODIFIER
|
||||
unlet-env CONDA_PREFIX
|
||||
unlet-env CONDA_SHLVL
|
||||
unlet-env CONDA_DEFAULT_ENV
|
||||
unlet-env CONDA_OLD_PATH
|
19
virtual_environments/venv.nu
Normal file
19
virtual_environments/venv.nu
Normal file
|
@ -0,0 +1,19 @@
|
|||
def venv [name] {
|
||||
let venv-path = ($name | path expand)
|
||||
let venv-bin-path = ([$venv-path "bin"] | path join)
|
||||
let old-path = ($nu.path | str collect (path-sep))
|
||||
let new-path = ($nu.path | prepend $venv-bin-path | str collect (path-sep))
|
||||
[[name, value];
|
||||
[PATH $new-path]
|
||||
[VENV_OLD_PATH $old-path]
|
||||
[VIRTUAL_ENV $venv-path]]
|
||||
}
|
||||
|
||||
def windows? [] {
|
||||
(sys).host.name == "Windows"
|
||||
}
|
||||
|
||||
def path-sep [] {
|
||||
if (windows?) { ";" } { ":" }
|
||||
}
|
||||
|
3
virtual_environments/venv_deactivate.nu
Normal file
3
virtual_environments/venv_deactivate.nu
Normal file
|
@ -0,0 +1,3 @@
|
|||
let-env PATH = $nu.env.VENV_OLD_PATH
|
||||
unlet-env VIRTUAL_ENV
|
||||
unlet-env VENV_OLD_PATH
|
Loading…
Add table
Add a link
Reference in a new issue