Activating the Virtual Environment

I am just beginning Project 3 – Web Applications in Eric Matthes’ Python Crash Course, 2nd ed. I hit some snags to solve at the very start, first getting the directory set up in the command prompt terminal, then activating the “virtual environment” in that directory.

Challenge 1: Set up a Directory for the Virtual Environment

On page 380, under the header, Creating a Virtual Environment, the book tells you to:

Create a new directory for your project called learning_log, switch to that directory in a terminal, and enter the following code to create a virtual environment:

This took some unpackaging for newbie-me:
1. First of all, what is a directory?
2. Second, how do I create a new one?
3. And how do I switch to that directory in a terminal?

Answers I found in my search through the googles:
1. A directory is simply a folder in which files are stored.
2. I ended up creating a directory/folder like I usually do, in the GUI (another relatively new term for me, graphical user interface) on my computer, clicking on the “New Folder” icon in the File Explorer, in the parent folder of my choice.

However, that felt like a cowardly dodge, and I wanted to learn how to create one in my PowerShell terminal.

I found a terrific tutorial on how to do this on a website called teckangaroo.com, “New Directory and File in 1 step PowerShell using New-Item”. It was the best explanation I stumbled upon in my web search. Thank you, Teckangaroo/IamBatman.

I followed the tutorial and successfully created a sample directory/folder.

Steps:
i. Use File Explorer on GUI and go to location where you want the new directory.

ii. Click on address bar and type:


PowerShell


iii. A PowerShell terminal should have opened up in that location rather than PS C:> or PS C:\Users\Me> or wherever it usually opens up.*

iv. Type:


new-item “floating_world” -itemtype “directory”


floating_world was the new directory I created.



A screen capture of PowerShell and the new directory, floating_world. What I typed is underlined in orange.

***27 January 2021 UPDATE*** I’ve since been using the New-Item script per Microsoft’s documentation, creating both directories and files, e.g.:


New-Item -Path . -Name “floating_world” -ItemType “directory”


3. *The teckangaroo.com tutorial includes the explanation, above, of how to switch the terminal to a directory.

Alternatively, I found out that I can switch the terminal to the directory by using the command “cd” (as in “current directory”) at the command prompt, followed by a space, then paste in the path to the directory obtained via the File Explorer in the GUI. Or if in the parent directory, type the name of the directory, e.g.:


cd floating_world


Challenge 2: Activating the Virtual Environment in Windows

The next issue I ran into a couple of paragraphs later, Activating the Virtual Environment, was not being able to activate it based on the instructions provided in the book. My computer system denied me access.

The most definitive explanation of a way out of my quagmire was python.org’s venv – Creation of Virtual Environments: open a PowerShell terminal and after the prompt type:


Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser



A screenshot of the solution (underlined in orange)

Next, I opened a second PowerShell terminal and did the following (highlighted in orange in the screenshot below):
1. use the “cd” prompt and paste in the path to the learning_log directory or merely:


cd learning_log


Now learning_log is in the terminal.

2. per the book’s instructions, create a virtual environment file named ll_env (short for learning log environment) by typing:


python -m venv ll_env


3. activate the virtual environment by typing:


ll_env\Scripts\Activate


After this I was able to proceed with the book instructions and create a project in Django.