.py | Python's Interpreter, Editor, and Virtual Environment

MountAye

Jun 29, 2021


In the previous post “Configuring Python Development Environment on Windows 10”, I did roughly three things:

  • First, I installed the Python interpreter.
  • Second, I installed VSCode on the computer.
  • Third, as we know, is the virtual environment, virtualenv.

If there’s something else worth mentioning, it’s to add a bit of “what” alongside the “how”. To make the article flow better, I plan to rearrange the order: first, talk about the editor, then the interpreter, and finally, the virtual environment. Although the article is based on Python, these concepts apply to almost all general-purpose programming languages. Because I’m not from a computer science background, this article is just my study notes, so if there are any inaccuracies, feel free to point them out.

Editor

First, let’s do an experiment. Rename the Python file hello.py created in the previous tutorial, change the extension .py to .txt, then double-click to open the file. What happens? In the Windows system, the most ordinary Notepad window pops up, displaying Python statements in black text on a white background, unlike in VSCode where different statements have different colors, but the content is exactly the same.

Furthermore, you can directly execute the content of a .txt file in the command line with the Python interpreter (py -m hello.txt), with the same effect as .py.

The .py extension doesn’t do anything special; like a .txt file, its content is simply a string of characters readable by humans. Such files are called text files. An editor is a program that handles text files. Notepad is one type of editor, as is VSCode.

Since Notepad can already write Python code, why bother installing VSCode? Indeed, some people really only use Notepad or other built-in editors of the operating system to write Python code (there’s even a video on YouTube of someone writing code in Microsoft Word); then debug the program using the command line. However, VSCode is specifically developed for programmers, providing many features that default editors lack, such as syntax highlighting, which highlights functions, variables, and keywords in different colors.

Compiler / Interpreter

In addition to text files, the second type of file mentioned in this article is a binary file.

Computers cannot directly understand characters recognized by humans. At the lowest level, classic computers understand only 0s and 1s represented in different ways. Although all files are ultimately binary at the lowest level, the term “binary file” is generally used specifically to refer to files other than text files. Since image files, video files, etc., have their own names, the files referred to by this term are basically related to software programs, files that can be executed by computers.

Humans recognize characters, computers recognize only 0s and 1s, so the most intuitive approach is to translate text files into binary files. This translation process is called compilation, and the software that can accomplish this is called a compiler. Once compilation is complete, the compiler exits, and to execute the program, the computer simply executes the compiled executable file.

However, compilation has a problem: the entire software needs to be compiled into a complete executable with all source code text files written. As the scale of the software grows, the resources and time required for compilation increase; once a modification is made, the entire project needs to be recompiled. Many times, we only want to quickly know the effect of a command in a large project; compilation is not suitable for such scenarios.

Thus, the interpreter emerged. This type of program is much more complex than a compiler. It runs whenever we execute a command in a programming language, allowing us to enter commands one by one, remembering the context of the code, and even remembering the results of our previous commands. The cost is that even for large projects, it needs to analyze and interpret line by line, and its computational resource overhead and speed are not as efficient as compilation.

Python is a (official implementation) programming language that uses an interpreter. The python-***.exe file we download from the official website is the Python interpreter. This also means that Python programs tend to perform less well than programs written by C/C++ programmers of the same level. However, due to its suitability for trial and error with single-line execution, Python is quite popular in research fields.

Of course, Python’s characteristics are far more than just being an interpreted language. It is also:

Virtual Environment

Once I asked my girlfriend what IDE she uses to write Python, and she proudly replied that her MacBook comes with Python, so she can run it directly from the command line… The problem wasn’t that she didn’t answer my question directly (actually, thinking about it, she did answer part of the question), but rather that running the system’s built-in Python interpreter or other programming language interpreters or compilers directly from the command line is a dangerous practice common among beginner programmers.

Fortunately, Windows is fine; after all, it’s an operating system aimed at the general consumer, so it doesn’t come with Python pre-installed. However, for more geek-oriented operating systems like Linux and the BSD family, things are different. These operating systems (or their distributions) often come pre-installed with Python. This Python isn’t intended for users to develop their own programs but rather for the many system tools written in Python to call upon. Since the version of Python is usually controlled by the package manager of the distribution, it often lags behind the latest Python version for some time to avoid bugs in the new version and to give developers of system tools time to update their code. So, if you use this version of Python for development and accidentally upgrade Python, it’s likely to cause some system functionality to malfunction.

A virtual environment is Python’s solution to this problem. We can install a version of Python different from the system’s default version but not add this interpreter to the system path, so the operating system is unaware of the existence of this version of Python. When creating a virtual environment, we specify the use of this specific version of Python. So, after activating the virtual environment, it becomes the Python we need for development, and exiting the virtual environment returns to the Python used by system tools.

Moreover, even if two projects use the same version of Python and both use the system’s default version, virtual environments still have a place. Most programs require dependency onlibrary code written by others, called libraries. Different projects may depend on different library code, or different versions of the same library. In this case, you can create separate virtual environments and install the libraries corresponding to each project. Projects can be isolated from each other.

Editor + Interpreter / Compiler + Virtual Environment Management + … = IDE

It’s too cumbersome to install so many different programs for Python development. Can’t it be done with just one click? Of course it can. A program that integrates various tools used in the development process is called an integrated development environment (IDE). For Python, the most famous IDE is undoubtedly Anaconda.

Then why don’t I use it? Of course, I’ve used it before, but after hearing about the fame of VSCode and trying it out, I don’t want to go back. Writing Python requires editing .py files, writing blog posts requires editing .md files, and some blog features require JavaScript. Essentially, all these tasks involve editing text files. In an editor like VSCode, everything is done together. So, programs like Spyder and Jupyter Lab in Anaconda seem redundant.

“Doing one thing well” is also part of the Unix philosophy. But the problem is that different people have different definitions of “one thing”. Some people think making breakfast is one thing, while others think it involves several things like heating milk, frying eggs, and toasting bread. Who is right?

Perhaps both are right, but Editor + Interpreter + … is upstream on the chain of contempt compared to IDE. You may disagree with this, but you should know it; otherwise, when others show off, you might feel embarrassed for not being prepared.