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. ====
  45. 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.
  46. ====
  47. Obtain an activation script that is not permanently embedded into your `bashrc`:
  48. [subs="+quotes"]
  49. ----
  50. $ *[ -e ~/.bash_profile ] && mv ~/.bash_profile{,-backup}*
  51. $ *touch ~/.bash_profile*
  52. $ *mamba init bash*
  53. no change /opt/miniforge/condabin/conda
  54. ...
  55. no change /opt/miniforge/etc/profile.d/conda.csh
  56. modified /foo/bar/.bash_profile
  57. ==> For changes to take effect, close and re-open your current shell. <==
  58. Added mamba to /foo/bar/.bash_profile
  59. ==> For changes to take effect, close and re-open your current shell. <==
  60. $ *(echo '#!/bin/false'; cat ~/.bash_profile) > conda-init.sh*
  61. $ *[ -e ~/.bash_profile-backup ] && mv ~/.bash_profile{-backup,} || rm -f ~/.bash_profile*
  62. ----
  63. For activation at any time, source the script:
  64. [subs="+quotes"]
  65. ----
  66. $ *source conda-init.sh*
  67. (base) $
  68. ----
  69. === Creating a Conda Environment ===
  70. You can create any number of environments in Conda.
  71. Let's create the first one for SciKit-Learn in this section. PyTorch, and TensorFlow are described below, although essential steps are the same.
  72. Step one is always identifying the version of Python that the environment works best with, also in terms of all of its dependencies.
  73. 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.
  74. 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.
  75. Cross-check with https://scikit-learn.org/stable/install.html[latest stable SciKit-Learn release].
  76. Create an environment description, say `envs/env-sklearn-16.yml`.
  77. .An example SciKit-Learn environment specification file for Conda
  78. [source,yaml]
  79. ----
  80. ---
  81. name: sklearn-16
  82. channels:
  83. - conda-forge
  84. dependencies:
  85. - python>=3.13,<3.14
  86. - numpy
  87. - cython
  88. - pandas
  89. - matplotlib
  90. - seaborn
  91. - scipy
  92. - scikit-learn>=1.6.1,<1.7.0
  93. ...
  94. ----
  95. +
  96. ====
  97. 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.
  98. ====
  99. Now tell `mamba` (or `conda`) to create it:
  100. [subs="+quotes"]
  101. ----
  102. (base) $ *mamba env create -y -f ./envs/env-sklearn-16.yml*
  103. Channels:
  104. - conda-forge
  105. Platform: osx-arm64
  106. Collecting package metadata (repodata.json): done
  107. Solving environment: done
  108. Downloading and Extracting Packages:
  109. ...
  110. Preparing transaction: done
  111. Verifying transaction: done
  112. Executing transaction: done
  113. ...
  114. ----
  115. Activate it (some checks along the way to show you how the entire thing works):
  116. [subs="+quotes"]
  117. ----
  118. (base) $ *which python*
  119. /opt/miniforge/bin/python
  120. (base) $ *python --version*
  121. Python 3.12.9
  122. (base) $ *mamba env list*
  123. # conda environments:
  124. #
  125. base * /opt/miniforge
  126. sklearn-16 /opt/miniforge/envs/sklearn-16
  127. (base) $ *mamba activate sklearn-16*
  128. (sklearn-16) $ *which python*
  129. /opt/miniforge/envs/sklearn-16/bin/python
  130. (sklearn-16) $ *python --version*
  131. Python 3.13.2
  132. (sklearn-16) $ *python3*
  133. Python 3.13.2 | packaged by conda-forge | (main, Feb 17 2025, 14:02:48) [Clang 18.1.8 ] on darwin
  134. Type "help", "copyright", "credits" or "license" for more information.
  135. >>> *import sklearn*
  136. >>> *sklearn.show_versions()*
  137. System:
  138. python: 3.13.2 | packaged by conda-forge | (main, Feb 17 2025, 14:02:48) [Clang 18.1.8 ]
  139. executable: /opt/miniforge/envs/sklearn-16/bin/python3
  140. machine: macOS-15.4-arm64-arm-64bit-Mach-O
  141. Python dependencies:
  142. sklearn: 1.6.1
  143. pip: 25.0.1
  144. setuptools: 78.1.0
  145. numpy: 2.2.4
  146. scipy: 1.15.2
  147. Cython: 3.0.12
  148. pandas: 2.2.3
  149. matplotlib: 3.10.1
  150. joblib: 1.4.2
  151. threadpoolctl: 3.6.0
  152. Built with OpenMP: True
  153. threadpoolctl info:
  154. user_api: blas
  155. internal_api: openblas
  156. num_threads: 10
  157. prefix: libopenblas
  158. filepath: /opt/miniforge/envs/sklearn-16/lib/libopenblas.0.dylib
  159. version: 0.3.29
  160. threading_layer: openmp
  161. architecture: VORTEX
  162. user_api: openmp
  163. internal_api: openmp
  164. num_threads: 10
  165. prefix: libomp
  166. filepath: /opt/miniforge/envs/sklearn-16/lib/libomp.dylib
  167. version: None
  168. >>> *exit()*
  169. ----
  170. 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:
  171. [subs="+quotes"]
  172. ----
  173. (sklearn-16) $ *mamba env update -y -f ./env-sklearn-16.yml*
  174. ...
  175. ----
  176. +
  177. ====
  178. WARNING: Without the `-n` option, `env update` is always applied to _current_ environment.
  179. ====
  180. === Creating Other Environments ===
  181. 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.
  182. .An example PyTorch environment specification file for Conda
  183. [source,yaml]
  184. ----
  185. ---
  186. name: pytorch-26
  187. channels:
  188. - conda-forge
  189. dependencies:
  190. - python>=3.12,<3.13
  191. - numpy
  192. - pandas
  193. - matplotlib
  194. - seaborn
  195. - pytorch>=2.6,<2.7
  196. ...
  197. ----
  198. [subs="+quotes"]
  199. ----
  200. (base) $ *mamba env create -y -f ./env-pytorch-26.yml*
  201. ...
  202. ----
  203. .An example TensorFlow environment specification file for Conda
  204. [source,yaml]
  205. ----
  206. ---
  207. name: tf-216
  208. channels:
  209. - apple
  210. - conda-forge
  211. dependencies:
  212. - python>=3.9
  213. - numpy>=1.19.5
  214. - pandas>=1.1.5
  215. - matplotlib>=3.3.4
  216. - tensorflow-deps
  217. - pip>=25.0
  218. - pip:
  219. - tensorflow-macos
  220. - tensorflow-metal
  221. ...
  222. ----
  223. [subs="+quotes"]
  224. ----
  225. (base) $ *mamba env create -y -f ./env-tf-216.yml*
  226. ...
  227. ----
  228. ====
  229. NOTE: Unlike those in the listings above, the included environment specification files already contain all the relevant components and their versions.
  230. ====
  231. === Working With Environments ===
  232. Outside of an integrated environment such as VSCode and JupyterLab, you can use the `mamba` or `conda` command to inspect and switch between environments.
  233. [subs="+quotes"]
  234. ----
  235. (base) $ *mamba env list*
  236. # conda environments:
  237. #
  238. base * /opt/miniforge
  239. pytorch-26 /opt/miniforge/envs/pytorch-26
  240. sklearn-16 /opt/miniforge/envs/sklearn-16
  241. tf-216 /opt/miniforge/envs/tf-216
  242. (base) $ *conda env list*
  243. # conda environments:
  244. #
  245. base * /opt/miniforge
  246. pytorch-26 /opt/miniforge/envs/pytorch-26
  247. sklearn-16 /opt/miniforge/envs/sklearn-16
  248. tf-216 /opt/miniforge/envs/tf-216
  249. ----
  250. You can switch between them by using the `activate` and `deactivate` commands.
  251. [subs="+quotes"]
  252. ----
  253. (base) $ *mamba activate sklearn-16*
  254. (sklearn-16) $ *mamba env list*
  255. # conda environments:
  256. #
  257. base /opt/miniforge
  258. pytorch-26 /opt/miniforge/envs/pytorch-26
  259. sklearn-16 * /opt/miniforge/envs/sklearn-16
  260. tf-216 /opt/miniforge/envs/tf-216
  261. (sklearn-16) $ *mamba deactivate*
  262. (base) $
  263. ----
  264. You can remove any environment using the `env remove` command.
  265. [subs="+quotes"]
  266. ----
  267. (base) $ *mamba env remove -y -n pytorch-26*
  268. Remove all packages in environment /opt/miniforge/envs/pytorch-26:
  269. ...
  270. Preparing transaction: done
  271. Verifying transaction: done
  272. Executing transaction: done
  273. ----