This shows you the differences between two versions of the page.
ii:labs:03:tasks:01 [2024/11/06 22:05] florin.stancu created |
ii:labs:03:tasks:01 [2024/11/15 03:31] (current) alexandru.bala [01. [30p] Python environment] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 01. [40p] Python environment ==== | + | ==== 01. [30p] Python environment ==== |
Python libraries are collections of reusable code that provide functionality for a wide range of tasks, from data analysis and machine learning to web development and automation. Libraries are often hosted on the Python Package Index (PyPI) and can be easily installed using package managers like **pip**. | Python libraries are collections of reusable code that provide functionality for a wide range of tasks, from data analysis and machine learning to web development and automation. Libraries are often hosted on the Python Package Index (PyPI) and can be easily installed using package managers like **pip**. | ||
Line 5: | Line 5: | ||
As you work on different //Python// projects, you may need different versions of the same module. Probably even a module that you already have installed system-wide. For this, we use [[https://docs.python.org/3/library/venv.html|virtual environments]]. These environments allow you to install specific module versions in a local directory and alters your shell's environment to prioritize using them. Switching between environments can be as easy as **source**ing another setup script. | As you work on different //Python// projects, you may need different versions of the same module. Probably even a module that you already have installed system-wide. For this, we use [[https://docs.python.org/3/library/venv.html|virtual environments]]. These environments allow you to install specific module versions in a local directory and alters your shell's environment to prioritize using them. Switching between environments can be as easy as **source**ing another setup script. | ||
- | The problem with virtual environments is that they don't mesh well with **apt**. In stead of **apt**, we will use a //Python// module manager called **pip3**. Our suggestion is to use **pip** __only__ in virtual environments. Yes, it can also install modules system-wide, but most modules can be found as **apt** packages anyway. Generally, it is not a good idea to mix package managers! | + | The problem with virtual environments is that they don't mesh well with **apt**. In stead of **apt**, we will use a //Python// module manager called **pip3**. Our suggestion is to use **pip** __only__ in virtual environments. Yes, it can also install modules system-wide, but most modules can be found as **apt** packages anyway. Generally, it is not a good idea to mix package managers (and modern Linux distributions outright deny this with explicit error when trying to install Python packages outside a virtualenv)! |
=== [10p] Task 1.1 - Dependency installation === | === [10p] Task 1.1 - Dependency installation === | ||
Line 14: | Line 14: | ||
$ sudo apt install python3 python3-venv python3-pip | $ sudo apt install python3 python3-venv python3-pip | ||
</code> | </code> | ||
+ | |||
=== [10p] Task 1.2 - Creating the environment === | === [10p] Task 1.2 - Creating the environment === | ||
Line 58: | Line 59: | ||
<note tip> | <note tip> | ||
- | This list of dependencies can be exported (together with the exact version) in a way that allows another user to install the __exact__ same modules in their own virtual environments. The file holding this information in named by convention //requirements.txt//: | + | **WSL and VScode** For those that use wsl, any Library you install and try to use in VScode on windows will not be visible, you must install the wsl extension and open the folder directly from VScode: |
+ | * install the VScode [[https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl|wsl extension]] | ||
+ | * Ctrl + Shift + P -> WSL: open folder in WSL | ||
+ | |||
+ | You might also want to read the official VSCode documentation about [[https://code.visualstudio.com/docs/python/environments|virtual environments]]. | ||
+ | </note> | ||
+ | |||
+ | <note tip> | ||
+ | This list of dependencies can be exported (together with the exact version) in a way that allows another user to install the __exact__ same modules in their own virtual environments. The file holding this information is named by convention //requirements.txt//: | ||
<code bash> | <code bash> | ||
Line 66: | Line 75: | ||
</note> | </note> | ||
- | === [10p] Task D - Testing that it works, with python CLI (REPL) === | + | === [10p] Task 1.4 - Testing that it works, with python CLI (REPL) === |
We can use the **python3** interpreter in interactive mode to quickly test whether our modules installed correctly: | We can use the **python3** interpreter in interactive mode to quickly test whether our modules installed correctly: | ||
Line 81: | Line 90: | ||
If you can't import the **requests** module, try to source the activation script again after installing the packages with **pip**. Some versions of **venv** / **pip** might act up. | If you can't import the **requests** module, try to source the activation script again after installing the packages with **pip**. Some versions of **venv** / **pip** might act up. | ||
</note> | </note> | ||
+ | |||
+ | === [0p] Task 1.5 - Debugging scripts === | ||
+ | |||
+ | If you ever need to debug your python program, the best approach is to open up a shell and run your program a bit tedious (copy pasting each line of the program). However, there's a better method: | ||
+ | |||
+ | <code python> | ||
+ | import IPython | ||
+ | |||
+ | IPython.embed(colors='neutral') | ||
+ | </code> | ||
+ | |||
+ | <note tip> | ||
+ | <code bash> | ||
+ | $ sudo apt install ipython3 | ||
+ | </code> | ||
+ | </note> | ||
+ | |||
+ | This will stop the execution of your script and open an ipython shell that has access to a copy of all your local & global data. Use this to inspect the state of your variables, their types, etc. Exit the shell (Ctrl + D) to continue the execution. | ||
+ |