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.
Create a new environment named my_env by entering the following on the command line:
python -m venv my_env
This creates a virtual environment named my_env and a directory with the same name in the current directory. Use the ls command to get a listing of the current directory and confirm that the my_env directory was created.
Enter pip list on the command line. You’ll see a list of all the Python packages currently installed on your system. If you’ve been following the lesson exercises, this list should be fairly long.
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.
On Windows, enter the following on the command line:
my_env\Scripts\activate
On macOS and Linux, use this command instead:
source my_env/bin/activate
After a moment or two, the activation process will complete, and you’ll see that your command-line prompts are preceded by (my_env) on Windows and my_env on macOS and Linux. You’re now in the my_env virtual environment.
To really confirm that you’re in the virtual environment, use the pip list command again. This time, the list it produces is quite short — in fact, there’s only one package: pip itself!
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.
Launch JupyterLab by entering jupyter lab on the command line and create a new notebook.
In a new code cell, run the following:
! pip install vader-sentiment
This will install VADER Sentiment Analysis, a library that provides a function for analyzing sentiment in social media posts. It is highly unlikely that it’s installed in your system’s main Python environment.
Note: If you were wondering, VADER is short for “Valence Aware Dictionary and sEntiment Reasoner.”
Because you installed VADER while the my_env environment was active, it’s installed in the my_env environment and available while that environment is active. It’s not installed in your system’s main Python environment.
Play around with VADER a little. Enter the following into a code cell and run it:
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)
Note: You may run into a
ModuleNoFoundErroron Mac. Running the following may remedy the issue:! pip install nltk import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer nltk.download('vader_lexicon')
The code above prompts the user to enter a sentence and returns VADER’s sentiment analysis of that sentence. VADER’s output is a dictionary containing ratings for negative, neutral, and positive sentiment, followed by a compound value derived from those ratings.
Try entering sentences like these:
- I rather liked the movie.
- I really liked the movie.
- I *really* liked the movie!
- The movie had its moments, but dragged on.
- I didn’t like the movie.
- I hated the movie!
- I REALLY hated that horrible movie!
- 👿!
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.
When the prompt reappears, deactivate the virtual environment by entering the following:
deactivate
You’ll be back in your system’s main Python environment. Your command line prompts will no longer be preceded by the virtual environment’s name.
While in the same directory but no longer in the virtual environment, relaunch JupyterLab, open vader.ipynb and try running the code cell that asks for input again. This time, you should get an error message like this:
ModuleNotFoundError: No module named 'vaderSentiment'
This makes sense. VADER is installed only in the my_env environment, not this one.