Setting up a virtual environment

A virtual environment is simply a set of folders created in a project's directory that contain all of the necessary binaries and libraries needed to run that project.

To create a virtual environment for our blackjack game to use, open up a terminal window and move into the outer blackjack directory. Now type the following:

$ python3 -m venv env
$ source env/bin/activate

This first command will create a folder called env in your blackjack folder. This folder contains everything that will be needed to run our blackjack application.

The next command tells our terminal to use the content of our virtual environment instead of the system-wide versions of Python and its packages.

To confirm this, open up the REPL now and check out the system path:

>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/home/David/Dropbox/packtbook/Code/blackjack/env/lib64/python3.6/site-packages', '/home/David/Dropbox/packtbook/Code/blackjack/env/lib/python3.6/site-packages']

Take note of the last two items in my path. Both now contain my virtual environment's env folder, showing that the Python interpreter is checking inside it for packages.

Now that we have created a separate environment for our blackjack game to run from, we can install the pygame library. We could do this in one of two ways. 

If we have no intentions of moving or sharing our blackjack game, simply running pip install pygame will bring in the library ready for use.

However, if we wish to make our game as portable as possible, or if we had a much larger amount of external dependencies, we could create a requirements.txt file. This file would contain a newline-separated list of packages that our application requires to run and ensures that anyone else obtaining the code is able to easily locate and install them all.

As our application only requires the pygame library, this requirements.txt file would just contain the word pygame. We would then install the pygame library with the command pip install -r requirements.txt.

Go ahead and install pygame by either of the two methods mentioned earlier.

With the dependencies handled, we can now create our casino_sounds package.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset