|
1 | | -## Configuring Python in CBS Remote Access (RA) |
| 1 | +## What this is for |
2 | 2 |
|
3 | | -**Python is not installed by default at CBS RA (yet).** To activate Python, contact the CBS microdata team at [`microdata@cbs.nl`](mailto:microdata@cbs.nl). |
| 3 | +CBS Remote Access (RA) only lets you use Python packages that CBS has approved and installed in advance. To get a package approved, you submit a file pip requirements.txt file listing exactly which packages (and versions) you need, and CBS installs them for you. |
4 | 4 |
|
5 | | -### Default Python Packages |
| 5 | +This repository helps you build that file correctly, without needing to understand Python packaging in depth. You will: |
6 | 6 |
|
7 | | -By default, some packages are available in Python at CBS RA, such as `pandas`, `pyreadstat` or `matplotlib` |
| 7 | +0. Install a tool called `uv` that does the hard work for you. |
| 8 | +1. Download this repository to your computer. |
| 9 | +2. Write down which packages you want, in a simple text file (`requirements.in`). |
| 10 | +3. Run one command that turns that list into a ready-to-send file (`environment0000.txt`). |
| 11 | +4. Test that file on your own computer before sending it (optional, but recommended). |
| 12 | +5. Email the file to CBS. |
| 13 | +6. Optionally, set up the same packages on your own computer so you can work locally too. |
8 | 14 |
|
9 | | -If you require additional packages or specific versions, follow the steps below to create and submit your own Python environment. |
| 15 | +You only need to follow these steps once per project (and again whenever you want to add or remove a package). |
10 | 16 |
|
11 | 17 | --- |
12 | 18 |
|
13 | | -### Creating a Custom Python Environment |
| 19 | +## Basic configuration |
14 | 20 |
|
15 | | -Follow these instructions to set up and submit a customized Python environment. You need to use a **Windows** computer. |
| 21 | +### Step 0: Install `uv` |
16 | 22 |
|
17 | | -#### Step 1: Check Existing Environment |
| 23 | +`uv` is the tool that does the heavy lifting (figuring out compatible package |
| 24 | +versions). Install it once, following the official instructions: |
| 25 | +https://docs.astral.sh/uv/getting-started/installation/ |
18 | 26 |
|
19 | | -- Check if `environment0000.txt` (replace `0000` with your actual project number) already contains the required packages and suitable versions. |
20 | | -- **If yes:** Send this file directly to CBS. |
21 | | -- **If no:** Continue to Step 2. |
| 27 | +If you've never used a terminal before: a terminal is just a window where you |
| 28 | +type commands instead of clicking buttons. On Windows, open "PowerShell" or |
| 29 | +"Command Prompt"; on Mac, open "Terminal" (both are pre-installed). The |
| 30 | +installation instructions above include a single command to paste in and run. |
22 | 31 |
|
23 | | -#### Step 2: Create the Environment (Windows + Conda) |
| 32 | +### Step 1: Download this repository to your computer |
24 | 33 |
|
25 | | -Install conda locally (only needed if you do not already have Conda installed): |
26 | | -- Follow the official Conda installation instructions [here](https://conda.io/projects/conda/en/latest/user-guide/install/index.html#regular-installation). |
27 | | -- If you're unfamiliar with command-line tools, consider installing [Anaconda](https://www.anaconda.com/products/individual) instead. |
28 | | - |
29 | | -On your local Windows machine: |
| 34 | +If you're comfortable with git: |
30 | 35 |
|
31 | 36 | ```sh |
32 | | -conda create -n 0000 python |
33 | | -conda activate 0000 |
34 | | -conda install pip |
35 | | -pip install package_name |
| 37 | +git clone <repo-url> |
| 38 | +cd cbs_python |
36 | 39 | ``` |
37 | 40 |
|
38 | | -Replace `package_name` with the packages you need (e.g., `pip install numpy`). If you want to install all the packages in the requirements.txt file in this repository, use `pip install -r requirements.txt` |
| 41 | +Otherwise, download the repository as a ZIP from its webpage and unzip it into a |
| 42 | +local folder. |
39 | 43 |
|
40 | | -**Note:** If using Jupyter Notebook or Spyder, install these explicitly, e.g.: |
| 44 | +### Step 2: Say which packages you want |
41 | 45 |
|
42 | | -```sh |
43 | | -pip install jupyter spyder |
44 | | -``` |
| 46 | +Open `requirements.in` in a text editor. It's a plain list of package names, one |
| 47 | +per line, already organized into groups (data handling, visualization, etc.), with |
| 48 | +a short comment next to each one explaining what it's for. |
45 | 49 |
|
46 | | -#### Step 3: Export the Environment |
| 50 | +- To add a package, add a new line with its name. |
| 51 | +- To remove one, delete its line (or put a `#` in front of it to keep it for later). |
47 | 52 |
|
48 | | -Export the environment into a requirements file: |
| 53 | +You don't need to write version numbers — the next step figures those out for you. |
49 | 54 |
|
50 | | -```sh |
51 | | -pip freeze > C:\temp\environment0000.txt |
52 | | -``` |
| 55 | +### Step 3: Let `uv` work out the exact versions |
53 | 56 |
|
54 | | -Check `environment0000.txt` for local paths (`file://`). If found, regenerate using: |
| 57 | +CBS RA runs Windows, so generate a Windows-specific version of the package list. Open a terminal in the repository folder and run: |
55 | 58 |
|
56 | 59 | ```sh |
57 | | -pip list --format=freeze > C:\temp\environment0000.txt |
| 60 | +uv pip compile requirements.in --python-version 3.12 --python-platform windows --no-annotate --no-header --emit-find-links -o environment0000.txt |
58 | 61 | ``` |
59 | 62 |
|
60 | | -#### Step 4: Verify Environment |
| 63 | +`--emit-find-links` is needed because some packages (e.g. the PyTorch Geometric |
| 64 | +ones) aren't on the normal package index — `requirements.in` points `pip` at |
| 65 | +an extra URL for those via a `--find-links` line, and this flag copies that |
| 66 | +line into `environment0000.txt` so the file still works when installed on its |
| 67 | +own with plain `pip`, without `requirements.in` alongside it. |
61 | 68 |
|
62 | | -Validate your environment by removing and recreating it: |
| 69 | +Rename `environment0000.txt` so `0000` matches your project number. This file can |
| 70 | +be installed with plain `pip` (no `uv` needed), which is what CBS RA will do. |
| 71 | + |
| 72 | +### Step 4: Test that it works in your own Windows computer [optional, highly recommended] |
| 73 | + |
| 74 | +Before sending the file to CBS, test it in a fresh, empty environment (a |
| 75 | +"venv"). This catches problems early, while you can still fix them. The |
| 76 | +easiest way is with `uv`, which creates the venv and installs into it without |
| 77 | +needing to activate anything: |
63 | 78 |
|
64 | 79 | ```sh |
65 | | -conda remove -n 0000 --all |
66 | | -conda create -n 0000 |
67 | | -conda activate 0000 |
68 | | -conda install pip |
69 | | -pip install -r C:\temp\environment0000.txt |
| 80 | +uv venv ~/.venvs/cbs-test |
| 81 | +uv pip install -r environment0000.txt --python ~/.venvs/cbs-test |
70 | 82 | ``` |
71 | 83 |
|
72 | | -Test thoroughly before submission by running python and importing your packages one by one. |
73 | | - |
74 | | -#### Step 5: Submit Your Environment |
| 84 | +If this finishes without errors, the file is ready to send. You can also run |
| 85 | +`check_environment.py`, which goes through every package in `requirements.in` |
| 86 | +and checks that it actually imports and reports a version (not just that it |
| 87 | +installed): |
75 | 88 |
|
76 | | -Send your verified `environment0000.txt` (replace 0000 by your project number) to CBS via email. |
| 89 | +```sh |
| 90 | +uv run --python ~/.venvs/cbs-test python check_environment.py |
| 91 | +``` |
77 | 92 |
|
| 93 | +When you're done testing, delete the test environment: |
78 | 94 |
|
| 95 | +```sh |
| 96 | +rm -rf ~/.venvs/cbs-test |
| 97 | +``` |
79 | 98 |
|
80 | | ---- |
| 99 | +(If you want to test with plain `pip` instead — closer to exactly what CBS |
| 100 | +will run — replace the two `uv` commands above with `python -m venv ~/.venvs/cbs-test`, |
| 101 | +then activate it with `~/.venvs/cbs-test/Scripts/activate` on Windows or |
| 102 | +`source ~/.venvs/cbs-test/bin/activate` on Mac/Linux, and run |
| 103 | +`pip install -r environment0000.txt`.) |
81 | 104 |
|
82 | | -## Using Python at CBS RA |
| 105 | +### Step 5: Email environmnet000.txt to CBS |
83 | 106 |
|
84 | | -We recommend to use Python through Visual Studio Code (VS Code), installed by default: |
| 107 | +Email it to CBS and ask them to activate Python in your project and install the environment. |
85 | 108 |
|
86 | | -- In VS Code, select the Python interpreter in the bottom-right corner of the editor. |
| 109 | +### Step 6 [optional]: Work on your own non-Windows computer |
87 | 110 |
|
88 | | -You could also use Python through Jupyter in RA, for that, open an Anaconda terminal in the RA and run: |
| 111 | +The environment created in Step 3 is locked to Windows |
| 112 | +(`--python-platform windows`), so it won't install on macOS or Linux. To work |
| 113 | +locally on a non-Windows computer, compile a second file for your own |
| 114 | +platform — just drop `--python-platform windows` so `uv` resolves wheels for |
| 115 | +whatever computer you're actually running on: |
89 | 116 |
|
90 | 117 | ```sh |
91 | | -conda activate 0000 |
92 | | -jupyter notebook --notebook-dir=H: |
| 118 | +uv pip compile requirements.in --python-version 3.12 --no-annotate --no-header --emit-find-links -o environment_local.txt |
| 119 | +uv venv ~/.venvs/cbs-python |
| 120 | +uv pip install -r environment_local.txt --python ~/.venvs/cbs-python |
93 | 121 | ``` |
94 | 122 |
|
95 | | -This opens Jupyter in your shared directory (`H:`). |
96 | | - |
97 | | ---- |
| 123 | +This creates a real environment (not a throwaway test one like Step 4), so |
| 124 | +you can use it for actual work: |
98 | 125 |
|
99 | | -## Contact |
100 | | - |
101 | | -This documentation is maintained by the [ODISSEI Social Data Science (SoDa)](https://odissei-data.nl/nl/soda/) team. |
102 | | - |
103 | | -For technical questions or suggestions: |
| 126 | +```sh |
| 127 | +uv run --python ~/.venvs/cbs-python jupyterlab |
| 128 | +``` |
104 | 129 |
|
105 | | -- File an issue in the project's issue tracker, or |
106 | | -- Contact [Javier Garcia-Bernardo](https://github.com/jgarciab). |
| 130 | +`uv run <command>` runs any command (Jupyter, a script, etc.) using that |
| 131 | +environment, installing anything missing automatically. You can also run |
| 132 | +`uv run --python ~/.venvs/cbs-python python check_environment.py` here to |
| 133 | +double-check everything imports correctly on your own machine. |
0 commit comments