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.

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.

Better Stars solution

I’m going through Python Crash Course, 2nd edition, by Eric Matthes.

He posts solutions for “Try It Yourself” questions for all chapters except 12, 13, and 14, which cover his Alien Invasion project. This is the solution I came up with for 13-2. Better Stars, on p. 264.

This post will likely only make sense to people also working through the problem sets in the book.

Screenshots of two iterations of the running game:

Main File



Settings File

‘Star’ File

I created the star image using Krita.

Establishing the Python Path in Windows

In my quest to learn Python, I hit upon an early snag of not being able to access it in the command window / Powershell of my Windows 10 system.

I had downloaded Python 3.9.0 from python.org; during the installation process I made sure the option to establish a “PATH” was checked. Per various sources in my internet search, this should have been enough to avoid this particular issue.

Nonetheless, post-installation, when I entered “python” after the prompt in the command window, I was informed that “python” was not recognized.

My search on the internet gave me a partial solution, which was to add a “path” in Environment Variables.

To get there:

Windows System → Control Panel → System and Security → System → Advanced system settings → Environmental Variables… → System variables

Select the “Path” variable and Edit

Select New

From there I was to paste in the location (aka path) of the Python 3.9 .exe file on my computer.

Windows, however, appeared to have hidden the actual path of the Python .exe; I searched around for it in vain in File Explorer.*

The way I ended up identifying Python’s path was by going to the Downloads file and opening up the Python 3.9 installer I had used, which provided the location of where Python was to have been installed.

I typed in a couple layers of the path in File Explorer. Sure enough, I found the Python folder.

I copied and pasted, and added the Python path to the System Variables.

Then I opened the command window and got this:

Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

Yay!

I did a happy dance.


*I’ve since discovered at the Microsoft Support site that, yes, there are hidden files, but these can be made viewable in File Explorer by changing the default hidden folder and search settings.

As a test, I did this, then searched for “python”.

It turns out my original method was more efficient; the search produced a lot of options that I, novice that I am, would have to sort through in order to find the right path.