This shows you the differences between two versions of the page.
ii:labs:03:tasks:01 [2021/11/24 16:26] radu.mantu |
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 ==== |
- | Different //Python// projects usually employ not only different modules, but even different versions of the __same__ module. The reason is that module developers push out new versions very rapidly and project maintainers may be slow on the uptake. Ideally, updates to any module (or package) should not break tools using previous implementations. Each update should add bug fixes and extend the API with new features, but not change it! However, programs are sometime built around some anomalous behavior of an API and bug fixes actually __break__ said program. | + | 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**. |
- | + | ||
- | [[https://ocw.cs.pub.ro/courses/_media/ii/labs/03/tasks/venv-demo.gif|{{ :ii:labs:03:tasks:venv-demo.gif?700 |}}]] | + | |
- | <html><center><i> Click GIF to maximize. </i></center></html> | + | |
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 A - Dependency installation === | + | === [10p] Task 1.1 - Dependency installation === |
First things first, we need **python3**, the **venv** module, and **pip**. These, we can get with **apt** | First things first, we need **python3**, the **venv** module, and **pip**. These, we can get with **apt** | ||
Line 18: | Line 15: | ||
</code> | </code> | ||
- | === [10p] Task B - Creating the environment === | + | |
+ | === [10p] Task 1.2 - Creating the environment === | ||
Assuming that you are in your project's root directory already, we can set up the virtual environment: | Assuming that you are in your project's root directory already, we can set up the virtual environment: | ||
Line 49: | Line 47: | ||
</note> | </note> | ||
- | === [10p] Task C - Fetching modules with pip === | + | === [10p] Task 1.3 - Fetching modules with pip === |
Same as **apt**, **pip** used to have a search function for modules. Unfortunately, they removed this feature due to a high number of queries. Now, to search for modules, you will need to use the [[https://pypi.org/project/pip/|web interface]]. | Same as **apt**, **pip** used to have a search function for modules. Unfortunately, they removed this feature due to a high number of queries. Now, to search for modules, you will need to use the [[https://pypi.org/project/pip/|web interface]]. | ||
Line 56: | Line 54: | ||
<code bash> | <code bash> | ||
- | $ sudo apt install libffi-dev libnacl-dev python3-dev | + | $ pip3 install requests beautifulsoup4 |
- | + | ||
- | $ pip3 install 'py-cord[voice]' PyNaCl ipython | + | |
$ pip3 list | $ pip3 list | ||
</code> | </code> | ||
- | So, what are these, exactly? | + | <note tip> |
- | * **py-cord[voice]**: a //Python// module that interacts with [[https://discord.com/|discord]]. The previous [[https://discordpy.readthedocs.io/en/stable/|discord.py]] project has been discontinued and [[https://docs.pycord.dev/en/master/index.html|pycord]] remains the best alternative. We'll be using this in the following exercise to write a discord bot. | + | **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: |
- | * **PyNaCl**: wrapper of the [[https://nacl.cr.yp.to/|NaCl]] library. This library offers a high-level interface for networking and cryptographic operations. Note that the library must be installed using **apt** and that which we install with **pip** is only a wrapper. | + | * install the VScode [[https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl|wsl extension]] |
- | * **ipython**: a more interactive //Python//. See the next task for some details, but there's really not much to it. | + | * 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> | <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//: | + | 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 76: | Line 75: | ||
</note> | </note> | ||
- | === [10p] Task D - Testing that it works, with ipython === | + | === [10p] Task 1.4 - Testing that it works, with python CLI (REPL) === |
- | In the previous lab, you used the **python3** interpreter in interactive mode. Now, we upgrade to **ipython**. This new interpreter offers an enhanced user experience by means of color coding, tab completion, multi-line editing, etc. For scripting purposes, **python3** should remain your interpreter of choice. But for prototyping, we suggest using this. For now, let's see if the **discord** module in the **py-cord[voice]** package is available to import. | + | We can use the **python3** interpreter in interactive mode to quickly test whether our modules installed correctly: |
<code python> | <code python> | ||
- | $ ipython | + | $ python3 |
- | Python 3.9.7 (default, Oct 10 2021, 15:13:22) | + | Python 3.12.7 (main, Oct 1 2024, 11:15:50) [GCC 14.2.1 20240910] on linux |
- | Type 'copyright', 'credits' or 'license' for more information | + | Type "help", "copyright", "credits" or "license" for more information. |
- | IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help. | + | >>> import requests |
+ | >>> help(requests.get) | ||
+ | </code> | ||
- | In [1]: import discord | + | <note important> |
+ | 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> | ||
- | In [2]: discord? | + | === [0p] Task 1.5 - Debugging scripts === |
- | In [3]: discord?? | + | 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: |
- | In [4]: help(discord) | + | <code python> |
+ | import IPython | ||
+ | |||
+ | IPython.embed(colors='neutral') | ||
</code> | </code> | ||
- | Note how in stead of **help**, in **ipython** we can ''?'' or ''??'' to something to access a brief / more complete documentation. | + | <note tip> |
- | + | <code bash> | |
- | <note important> | + | $ sudo apt install ipython3 |
- | If you can't import the **discord** module, try to source the activation script again after installing the packages with **pip**. Some versions of **venv** / **pip** might act up. | + | </code> |
</note> | </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. | ||
+ |