Skip to content

Commit 2598df4

Browse files
author
Dale Roberts
committed
Review response
1 parent d5c65e5 commit 2598df4

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

posts/2024-06-05-mixing-python-envs.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ There is no conda distribution for this package, it can only be installed by `pi
3232
package also has an extensive list of dependencies, most of which are available in the `conda/analysis3` environment, so using the `analysis3`
3333
environment as a base will save quite a lot of disk space.
3434

35-
You may be tempted to load the `conda/analysis3` environment and `pip install --user` the xWMB package.
35+
The simplest approach is to load the `conda/analysis3` environment and `pip install --user` the xWMB package.
3636
<terminal-window typingDelay=30 lineDelay=200>
3737
<terminal-line data="input">module use /g/data/hh5/public/modules</terminal-line>
3838
<terminal-line data="input">module load conda/analysis3</terminal-line>
@@ -44,24 +44,23 @@ You may be tempted to load the `conda/analysis3` environment and `pip install --
4444
<terminal-line>Successfully installed regionate-0.0.1 sectionate-0.2.1 xbudget-0.1.0 xgcm-0.8.2.dev15+g7492277 xwmb-0.1.1 xwmt-0.1.1</terminal-line>
4545
</terminal-window>
4646

47-
The trouble with this is that it can lead to problems in the future. The `analysis3` module
47+
This is sufficient in certain cases, for instance, quickly testing user code, or when long-term
48+
reproducibility is not necessary. The downside with this approach is that it can lead to problems in the future. First of all, everything will be installed in your `/home` directory, which has a small quota and can easily be filled by local `pip` installations. Another consideration is that the `analysis3` module
4849
is updated to every 3 months as new environments are released. For example, if you load `conda/analysis3` today,
4950
you'll get the `conda/analysis3-24.01` environment. If you were to load the same module a couple of months from
5051
now, you'll get the `conda/analysis3-24.04` environment. Our policies around updating the conda environments
5152
can be found [here](https://climate-cms.org/cms-wiki/services/services-conda.html). As time goes on, your
5253
locally installed package will get further and further out of date, and could potentially conflict with newer
53-
packages installed in updated analysis environments.
54+
packages installed in updated analysis environments.
5455

55-
This is one of the most common issues we see come into our helpdesk. A user will open a JupyterLab session in ARE
56-
and an important package like `dask` or `xarray` will fail to import due to a conflict in the user's local
57-
environment. In fact, in our advice to users, we recommend the following Advanced Settings for ARE environments:
56+
In our advice to users, we recommend the following Advanced Settings for ARE environments:
5857

5958
![ARE recommended settings](../images/ARE-recommended.PNG "ARE recommended JupyterLab settings")
6059

61-
Note the `PYTHONNOUSERSITE=1` environment variable. This will have the effect of preventing anything installed with `pip install --user` from loading at all.
60+
Note the `PYTHONNOUSERSITE=1` environment variable. This will have the effect of preventing anything installed with `pip install --user` from loading at all. This setting also works in the terminal and can be used as a first step to diagnosing any issues with python package imports.
6261

6362
## Virtual environments
64-
Instead, we recommend creating a [virtual environment](https://docs.python.org/3/library/venv.html). A virtual environment is a self-contained Python
63+
What we recommend is creating a [virtual environment](https://docs.python.org/3/library/venv.html). A virtual environment is a self-contained Python
6564
environment built on top of an existing Python installation. The self-contained
6665
nature of a virtual environment means that any issues due to conflicting
6766
dependencies can be avoided entirely. Being able to base the environment on top of
@@ -96,12 +95,21 @@ at the time of its creation, so it avoids the out-of-date dependencies issue men
9695
<terminal-line>lrwxrwxrwx 1 dr4292 v45 67 Jun 5 14:43 /home/563/dr4292/xwmb_venv/bin/python3 -> /g/data/hh5/public/apps/miniconda3/envs/analysis3-24.01/bin/python3</terminal-line>
9796
</terminal-window>
9897

99-
There are a few different ways that this virtual environment can be used in scripts.
100-
Immediately after loading the `conda/analysis3` module, you can place the command
101-
`source xwmb_venv/bin/activate` in your script and any `python` command or
102-
script with a `#!/usr/bin/env python` [shebang line](https://en.wikipedia.org/wiki/Shebang_(Unix))
103-
will now run under the new virtual environment. If you're running a script directly, you can
104-
change the path in the shebang line to the path to the `python` symlink in the virtual environment:
98+
Immediately after loading the `conda/analysis3` module, you can run
99+
`source xwmb_venv/bin/activate` and any `python` command or script will now run under the new virtual environment.
100+
```
101+
#!/usr/bin/env bash
102+
#PBS -l walltime=1:00:00,mem=4GB,ncpus=1,storage=gdata/hh5
103+
104+
module use /g/data/hh5/public/modules
105+
module load conda/analysis3
106+
source xwmb_venv/bin/activate
107+
108+
python3 my_script.py
109+
```
110+
111+
If you're running a script directly, you can
112+
change the path in the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) line to the path to the `python` symlink in the virtual environment:
105113
```
106114
#!/home/563/dr4292/xwmb_venv/bin/python3
107115
@@ -170,9 +178,7 @@ While CMS tries to make the `hh5` conda analysis environments as comprehensive a
170178
always install the packages you need. By creating a virtual environment using the `conda/analysis3`
171179
environment as your base,
172180
its possible to add in packages CMS can't install, without having the overhead of maintaining
173-
your own large conda environment. Furthermore, using a virtual environment avoids the
174-
headaches associated with installing software using `pip install --user` as anything
175-
installed will be pinned to a specific conda environment. Virtual environments are simple to
181+
your own large conda environment. Furthermore, using a virtual environment is more stable and easier to maintain and reuse than installing software using `pip install --user`. Virtual environments are simple to
176182
install, and simple to delete and re-create as necessary. Virtual environments can also be seamlessly integrated into
177183
ARE JupyterLab sessions, allowing you to use it anywhere you would use an `analysis3` conda environment.
178184

0 commit comments

Comments
 (0)