What is a virtual environment? 2 – Setting Up the Python Coding Environment

What is a virtual environment? 2 – Setting Up the Python Coding Environment

All the reasons mentioned in the preceding list are enough to make pipenv our choice for the management and creation of virtual environments.

Now, we’ll proceed with the pipenv installation and with the description of its main commands.

We can install pipenv together with pipenv-pipes by just typing the following command:
sudo python3 –m pip install pipenv pipenv-pipes

Next, let’s create a testing directory named pipenvtest1:
mkdir pipenvtest1 && cd pipenvtest1

With list or tree, we can check whether this folder is empty:
pipenv –venv

We will get the output shown in Figure 2.11. It tells us that, at the moment, there are no virtual environments:

Figure 2.11: pipenv –venv

When executing the pipenv shell command at this point, pipenv will search for a file called Pipfile in the present directory. This file is needed for the creation of the virtual environment. If there is no Pipfile, it will be created together with the virtual environment and we will get a successfully created virtual environment message on the screen:

Figure 2.12: Virtual environment creation

Now, by launching Sublime Text from the working directory, we can see that a Pipfile has been created and we can explore its content. It contains all the information about the virtual environment such as the Python version, installed packages (empty at the moment), and dev packages (empty as well):

Figure 2.13: The Pipfile content

To install any package in our new virtual environment, we just have to type the following:
pipenv install <package_name>

For example, we can try with numpy:

Figure 2.14: Packages installation

As we can read on the screen, the package has been successfully installed, the Pipfile has been updated, it now contains numpy, and a new Pipfile.lock file, containing the list of all the hashes and dependencies, has been created and updated. Let’s see it in Sublime Text:

Figure 2.15: Pipfile with packages and Pipfile.lock

Another very interesting option is to create a requirements file in the Pipfile.lock file that will be very useful when we deploy our web applications. So, to create a requirements.txt file, we can simply type the following:
pipenv lock –r > requirements.txt

Please note that starting from pipenv version 2022.8.13, the previous command has been replaced with the following:
pipenv requirements > requirements.txt

Inside pipenv, we can run any kind of command just by using the run instruction. So, if we want to run, let’s say Python, we can just type the following:
pipenv run python

By running the pipenv check command, you can examine package updates, and if any are found the updates will be executed accordingly.

Uninstalling a package (and removing it from the Pipfile) is very simple. In fact, we just have to type the following:
pipenv uninstall <package_name>

In addition to pipenv, we have also installed pipenv-pipes, which is an intriguing tool. By typing pipes in the terminal, we can obtain a list of the installed virtual environments. We can then navigate through the list using the cursor to select the desired virtual environment for activation, as shown in the following figure:

Figure 2.16: Pipes

When we are in a virtual environment and want to close it to come back to the original status, we just have to type exit in the terminal.

To remove a virtual environment, we can navigate to its directory and execute the following command:
pipenv –rm

This command removes the virtual environment without deleting the files in the directory:

Figure 2.17: Virtual environment deletion

If we want to remove the directory with all its files, we have to do it manually.

Summary

In this chapter, we prepared the ground for our next activities. We learned how to create a developing environment while keeping costs very low (almost free). So, we decided to use Ubuntu as our main OS. After that, we made sure to have Python already available and installed pip. The selection of the IDE was quite straightforward since we chose Sublime Text, a quite light, powerful, and advanced text editor.

After that, we focused on Python’s virtual environment. In this case, our choice was pipenv because it is quite powerful, easy to use, and full of advanced features. We spent some time on a quite complete overview of this tool and its main features and instructions.

Now, everything is ready, so finally, in the next chapter, we are going to take a kind of crash course on the basic features of Streamlit. Are you ready?

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top