GETTING_RUNNING.adoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. == Getting Running ==
  2. Create a system-independent Python installation. Trust me, you want to divorce Python from your system.
  3. So many frameworks and libraries have certain expectations as to what Python version you should be using, that if you want to switch between them without containerised versions, the best way is to create several independent installations.
  4. https://conda-forge.org[Conda-Forge] is a community GitHub organisation containing repositories of https://conda.org[Conda] recipes.
  5. Conda is a _"language-agnostic, multi-platform package management ecosystem for projects"_.
  6. Conda installer is called https://conda-forge.org/download/[Miniforge].
  7. It will help you maintain several independent environments, each with its own set of Python modules.
  8. What's best, it uses cached versions of packages so no long download waits whenever you want to initialise a similar, but not quite the same, environment.
  9. Think of it as Python _virtualenv_ on steroids.
  10. === Installing and Preparing Conda ===
  11. Fresh installation:
  12. [subs="+quotes"]
  13. ----
  14. $ *mkdir /opt/miniforge*
  15. $ *bash ~/Downloads/Miniforge3-24.11.3-2-MacOSX-arm64.sh -b -f -p /opt/miniforge*
  16. ----
  17. For updates:
  18. [subs="+quotes"]
  19. ----
  20. $ *bash ~/Downloads/Miniforge3-24.11.3-2-MacOSX-arm64.sh -b -f -u -p /opt/miniforge*
  21. ----
  22. It is also possible to update Conda from a running environment:
  23. [subs="+quotes"]
  24. ----
  25. (base) $ *conda update -y -n base -c conda-forge conda*
  26. Channels:
  27. - conda-forge
  28. - apple
  29. Platform: osx-arm64
  30. ...
  31. The following packages will be downloaded:
  32. package | build
  33. ---------------------------|-----------------
  34. conda-24.11.3 | py39h2804cbe_0 899 KB conda-forge
  35. ------------------------------------------------------------
  36. Total: 899 KB
  37. ...
  38. Downloading and Extracting Packages:
  39. Preparing transaction: done
  40. Verifying transaction: done
  41. Executing transaction: done
  42. ----
  43. ====
  44. WARNING: It is not recommended to update Conda solely based on a prompt that a newer version is available. Consult the Conda website and verify it is considered stable before doing an update.
  45. ====
  46. Obtain an activation script that is not permanently embedded into your `bashrc`:
  47. [subs="+quotes"]
  48. ----
  49. $ *[ -e ~/.bash_profile ] && mv ~/.bash_profile{,-backup}*
  50. $ *touch ~/.bash_profile*
  51. $ *mamba init bash*
  52. no change /opt/miniforge/condabin/conda
  53. ...
  54. no change /opt/miniforge/etc/profile.d/conda.csh
  55. modified /foo/bar/.bash_profile
  56. ==> For changes to take effect, close and re-open your current shell. <==
  57. Added mamba to /foo/bar/.bash_profile
  58. ==> For changes to take effect, close and re-open your current shell. <==
  59. $ *(echo '#!/bin/false'; cat ~/.bash_profile) > conda-init.sh*
  60. $ *[ -e ~/.bash_profile-backup ] && mv ~/.bash_profile{-backup,} || rm -f ~/.bash_profile*
  61. ----
  62. For activation at any time, source the script:
  63. [subs="+quotes"]
  64. ----
  65. $ *source conda-init.sh*
  66. (base) $
  67. ----
  68. === Creating a Conda Environment ===
  69. You can create any number of environments in Conda.
  70. Let's create the first one for SciKit-Learn in this section. PyTorch, and TensorFlow are described below, although essential steps are the same.
  71. Step one is always identifying the version of Python that the environment works best with, also in terms of all of its dependencies.
  72. Sometimes, the toolkit will suggest the steps for the package manager we chose (Conda). I propose you completely ignore this and just roll your own environment. It will be for the better once you hit some dependency issues (and you usually will) - you will at least be familiar with the components you chose and the process of replacing them and/or adding more.
  73. Check https://www.python.org/downloads/[Current Python Release Status]. As of the time of this writing, 3.13 was the latest non-pre-release version.
  74. Cross-check with https://scikit-learn.org/stable/install.html[latest stable SciKit-Learn release].
  75. Create an environment description, say `envs/env-sklearn-16.yml`.
  76. .An example SciKit-Learn environment specification file for Conda
  77. [source,yaml]
  78. ----
  79. ---
  80. name: sklearn-16
  81. channels:
  82. - conda-forge
  83. dependencies:
  84. - python>=3.13,<3.14
  85. - numpy
  86. - cython
  87. - pandas
  88. - matplotlib
  89. - seaborn
  90. - scipy
  91. - scikit-learn>=1.6.1,<1.7.0
  92. ...
  93. ----
  94. ====
  95. IMPORTANT: I suggest you initially use semantic version spec only for the components you really care about (such as Python and SciKit-Learn), and let Conda figure out the other component versions. After that, you can look at what is installed using `pip list installed` and update the versions in the environment file to reflect your system. This will ensure that repeated installations will use the same component versions, thus giving you predictable behaviour.
  96. ====
  97. Now tell `mamba` (or `conda`) to create it:
  98. [subs="+quotes"]
  99. ----
  100. (base) $ *mamba env create -y -f ./envs/env-sklearn-16.yml*
  101. Channels:
  102. - conda-forge
  103. Platform: osx-arm64
  104. Collecting package metadata (repodata.json): done
  105. Solving environment: done
  106. Downloading and Extracting Packages:
  107. ...
  108. Preparing transaction: done
  109. Verifying transaction: done
  110. Executing transaction: done
  111. ...
  112. ----
  113. Activate it (some checks along the way to show you how the entire thing works):
  114. [subs="+quotes"]
  115. ----
  116. (base) $ *which python*
  117. /opt/miniforge/bin/python
  118. (base) $ *python --version*
  119. Python 3.12.9
  120. (base) $ *mamba env list*
  121. # conda environments:
  122. #
  123. base * /opt/miniforge
  124. sklearn-16 /opt/miniforge/envs/sklearn-16
  125. (base) $ *mamba activate sklearn-16*
  126. (sklearn-16) $ *which python*
  127. /opt/miniforge/envs/sklearn-16/bin/python
  128. (sklearn-16) $ *python --version*
  129. Python 3.13.2
  130. (sklearn-16) $ *python3*
  131. Python 3.13.2 | packaged by conda-forge | (main, Feb 17 2025, 14:02:48) [Clang 18.1.8 ] on darwin
  132. Type "help", "copyright", "credits" or "license" for more information.
  133. >>> *import sklearn*
  134. >>> *sklearn.show_versions()*
  135. System:
  136. python: 3.13.2 | packaged by conda-forge | (main, Feb 17 2025, 14:02:48) [Clang 18.1.8 ]
  137. executable: /opt/miniforge/envs/sklearn-16/bin/python3
  138. machine: macOS-15.4-arm64-arm-64bit-Mach-O
  139. Python dependencies:
  140. sklearn: 1.6.1
  141. pip: 25.0.1
  142. setuptools: 78.1.0
  143. numpy: 2.2.4
  144. scipy: 1.15.2
  145. Cython: 3.0.12
  146. pandas: 2.2.3
  147. matplotlib: 3.10.1
  148. joblib: 1.4.2
  149. threadpoolctl: 3.6.0
  150. Built with OpenMP: True
  151. threadpoolctl info:
  152. user_api: blas
  153. internal_api: openblas
  154. num_threads: 10
  155. prefix: libopenblas
  156. filepath: /opt/miniforge/envs/sklearn-16/lib/libopenblas.0.dylib
  157. version: 0.3.29
  158. threading_layer: openmp
  159. architecture: VORTEX
  160. user_api: openmp
  161. internal_api: openmp
  162. num_threads: 10
  163. prefix: libomp
  164. filepath: /opt/miniforge/envs/sklearn-16/lib/libomp.dylib
  165. version: None
  166. >>> *exit()*
  167. ----
  168. If you want to later update some of the environment components, you can do so by editing the env file and issuing the following command:
  169. [subs="+quotes"]
  170. ----
  171. (sklearn-16) $ *mamba env update -y -f ./env-sklearn-16.yml*
  172. ...
  173. ----
  174. ====
  175. WARNING: Without the `-n` option, `env update` is always applied to _current_ environment.
  176. ====
  177. === Creating Other Environments ===
  178. You can do the same with other environments: PyTorch, TensorFlow, etc. Some of these may even come with hardware acceleration support for your computer system.
  179. .An example PyTorch environment specification file for Conda
  180. [source,yaml]
  181. ----
  182. ---
  183. name: pytorch-26
  184. channels:
  185. - conda-forge
  186. dependencies:
  187. - python>=3.12,<3.13
  188. - numpy
  189. - pandas
  190. - matplotlib
  191. - seaborn
  192. - pytorch>=2.6,<2.7
  193. ...
  194. ----
  195. [subs="+quotes"]
  196. ----
  197. (base) $ *mamba env create -y -f ./env-pytorch-26.yml*
  198. ...
  199. ----
  200. .An example TensorFlow environment specification file for Conda
  201. [source,yaml]
  202. ----
  203. ---
  204. name: tf-216
  205. channels:
  206. - apple
  207. - conda-forge
  208. dependencies:
  209. - python>=3.9
  210. - numpy>=1.19.5
  211. - pandas>=1.1.5
  212. - matplotlib>=3.3.4
  213. - tensorflow-deps
  214. - pip>=25.0
  215. - pip:
  216. - tensorflow-macos
  217. - tensorflow-metal
  218. ...
  219. ----
  220. [subs="+quotes"]
  221. ----
  222. (base) $ *mamba env create -y -f ./env-tf-216.yml*
  223. ...
  224. ----
  225. ====
  226. NOTE: Unlike those in the listings above, the included environment specification files already contain all the relevant components and their versions.
  227. ====
  228. === Working With Environments ===
  229. Outside of an integrated environment such as VSCode and JupyterLab, you can use the `mamba` or `conda` command to inspect and switch between environments.
  230. [subs="+quotes"]
  231. ----
  232. (base) $ *mamba env list*
  233. # conda environments:
  234. #
  235. base * /opt/miniforge
  236. pytorch-26 /opt/miniforge/envs/pytorch-26
  237. sklearn-16 /opt/miniforge/envs/sklearn-16
  238. tf-216 /opt/miniforge/envs/tf-216
  239. (base) $ *conda env list*
  240. # conda environments:
  241. #
  242. base * /opt/miniforge
  243. pytorch-26 /opt/miniforge/envs/pytorch-26
  244. sklearn-16 /opt/miniforge/envs/sklearn-16
  245. tf-216 /opt/miniforge/envs/tf-216
  246. ----
  247. You can switch between them by using the `activate` and `deactivate` commands.
  248. [subs="+quotes"]
  249. ----
  250. (base) $ *mamba activate sklearn-16*
  251. (sklearn-16) $ *mamba env list*
  252. # conda environments:
  253. #
  254. base /opt/miniforge
  255. pytorch-26 /opt/miniforge/envs/pytorch-26
  256. sklearn-16 * /opt/miniforge/envs/sklearn-16
  257. tf-216 /opt/miniforge/envs/tf-216
  258. (sklearn-16) $ *mamba deactivate*
  259. (base) $
  260. ----
  261. You can remove any environment using the `env remove` command.
  262. [subs="+quotes"]
  263. ----
  264. (base) $ *mamba env remove -y -n pytorch-26*
  265. Remove all packages in environment /opt/miniforge/envs/pytorch-26:
  266. ...
  267. Preparing transaction: done
  268. Verifying transaction: done
  269. Executing transaction: done
  270. ----