Python for AI: A Crash Course

Nov 16 2024 · Python 3.12, JupyterLab 4.2.4

Lesson 03: Working with Python Libraries & Package Management

Virtual Environment Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Demo

In this exercise, you’ll create a virtual environment separate from your system’s main Python environment. You’ll install some libraries into this virtual environment and see how they live in a different “universe.”

Set Up the Virtual Environment

You’ll need a directory for this exercise’s virtual environment and Jupyter notebook files. Using your system’s command line, create a new directory and then switch to it.

python -m venv my_env

Activate the Virtual Environment

Now that you’ve created a virtual environment, it’s time to activate it. Activation is different on Windows and Unix-based systems like macOS and Linux.

my_env\Scripts\activate
source my_env/bin/activate

Create a New Notebook in the my_env Environment

Create a new notebook that uses a library that will only exist in the my_env environment and not in your system’s main Python environment.

! pip install vader-sentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

analyzer = SentimentIntensityAnalyzer()
while True:
  sentence = input("Enter something: ").strip()
  if not sentence:
    break
  analysis = analyzer.polarity_scores(sentence)
  print(analysis)
! pip install --upgrade vader-sentiment

Deactivate the Virtual Environment

Save the notebook under the name vader.ipynb. Then, go to the command line and stop JupyterLab by typing control-C.

deactivate
ModuleNotFoundError: No module named 'vaderSentiment'
See forum comments
Cinema mode Download course materials from Github
Previous: Virtual Environments Next: Conclusion