01. [20p] Enhancing your environment

Just like last time, you'll need to create a virtualenv in order to install specific libraries.

Click GIF to maximize.

[10p] Task A - Dependencies

First, if you reinstalled your Linux/WSL/VM from last time, ensure that you have the prerequisites installed (it never hurts!):

sudo apt install python3 python3-venv python3-pip

Navigate to your work directory, e.g., lab04 and build your virtual environment:

mkdir -p lab04 && cd lab04
python3 -m venv .venv
# don't forget to activate your environment each time you start a new shell:
source .venv/bin/activate

Now, to get to the specifics of today's lab, we need some extra dependencies:

# you need extra system libraries installed with your distro's package manager
sudo apt install libffi-dev libnacl-dev python3-dev
# and our target Python libraries:
pip3 install 'py-cord[voice]' PyNaCl ipython
pip3 list

So, what are these, exactly?

  • PyNaCl: wrapper of the 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.
  • py-cord[voice]: a Python module that interacts with discord. The previous discord.py project has been discontinued and pycord remains the best alternative. We'll be using this in the following exercise to write a discord bot.
  • ipython: a more interactive Python. See the next task for some details, but there's really not much to it.

[10p] Task B - Testing that it works, with ipython

In the previous labs, 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.

$ ipython
Python 3.9.7 (default, Oct 10 2021, 15:13:22) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.
 
In [1]: import discord
 
In [2]: discord?
 
In [3]: discord??
 
In [4]: help(discord)

Note how in stead of help, in ipython we can ? or ?? to something to access a brief / more complete documentation.

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.

ii/labs/04/tasks/01.txt ยท Last modified: 2024/11/20 16:02 by florin.stancu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0