How to install Python and PIP on Windows 10. How to set up Python as a web server module

Python is currently very popular and many tools for information gathering, scanning, penetration testing, and security assessments are written in Python. All these tools work seamlessly on Linux. But in fact, the vast majority of tools can work on Windows as well, most of them require just a simple Python installation.

Python is a scripting (interpreted) language, that is, programs do not need to be compiled before running. In this sense, Python is similar to PHP.

There are currently two major versions of Python:

  • 3.x
  • 2.x

They differ significantly from each other, including the syntax, that is, a program written on one version may not work in the interpreter of another version. However, the end of life for Python 2.x has already been set and this version can be considered obsolete. Therefore, I propose to install only Python 3. Although if you do not agree, then write in the comments and I will extend the manual by installing the second version. Although we won’t get the old version, I’ll show in addition how to install PIP – Python package management system – as well as how to configure Python as a web server module – that is, it can perform the same functions as PHP.

How to install Python on Windows

There are also many actively supported branches for Python 3.x:

  • 3.9
  • 3.8
  • 3.7
  • 3.6
  • 3.5

The latest stable release at the time of writing is Python 3.8.x.

Remember that Python will not work on Windows XP or earlier.

By the way, after all, Python is also in the Windows Store,

where it has one star (out of five) and one review (‘nothing works’):

Therefore, we will perform the installation using the official installer – it is very simple.

To download the installation file, go to the download page for installation files for Windows: https://www.python.org/downloads/windows/

Select the latest release of Python 3, in the screenshot it is Python 3.8.0.

Scroll down and find ‘Windows x86-64 executable installer’:

Run the downloaded file:

First, check the ‘Add Python 3.8 to PATH’ checkbox. This is necessary so that when you run the script in Python, you will not need to specify the full path to the interpreter.

Secondly, I recommend changing the folder where the installation will be performed. To do this, click ‘Customize installation’.

We do not change anything in this window, the main thing is to make sure that checkbox opposite pip is selected:

In this window, make sure that ‘Add Python to environment variables’ is checked and change the installation folder to ‘C:\Python\Python38’:

Click the Install button:

And… everything is ready! It was really simple, isn’t it?

In my Windows installation, support of paths over 260 characters is enabled already, but if you haven’t done it yet, the installer will offer it at the end – to do this, click on the ‘Disable path length limit’. This setting does not affect anything badly. But you need to remember that old programs may still be unable to work with files whose paths have more than 260 characters.

To check, open a command prompt: press Win+x and select “Windows PowerShell”:

PowerShell opens. If you are more comfortable with the usual Windows command line (CMD), then press Win+r and type

cmd

In any of the opened windows, enter:

python --version

A version of Python should appear, for example:

Python 3.8.0

How to run Python programs on Windows

Since many Python scripts are utilities with a command line interface, you need to run them on the command line, otherwise when you double-click the program will exit quickly and you will not see anything other than a flickering window.

To start, open a command prompt and run a command of the form:

python SCRIPT.py

An example of a simple hello_world.py program:

x = 1
if x == 1:
    # Намеренно вставленные 4 пробела
    print("x равняется 1.")
print("Goodbye, World!")

Launch:

python hello_world.py

How to install PIP on Windows

PIP is a package manager with which you can search, install, update, and remove Python packages. The packages can be either libraries that are dependencies for other programs, or full-fledged programs. Moreover, PIP correctly processes dependencies, so if a program requires other Python packages, then PIP will also install them.

On Linux, some Python packages are packaged in installation files and distributed through standard repositories – but there are fewer such packages than are available in pip. Therefore, on Linux, pip is also used. As for Windows, here pip is the most convenient way to install the desired package.

When installing the Python environment as described above, the pip package manager must also install. You can check this out:

pip --help

Update important components:

python -m pip install --upgrade pip
pip install --upgrade setuptools

Installing the C++ Build Tool

This is an optional step when installing pip – but if you skip it, you will encounter errors when trying to install some packages.

In fact, when installing packages using pip, sometimes compilation is also performed, so that this happens without problems, you also need to install the ‘C ++ Build Tools’. To install them, download and run the vs_buildtools.exe file. In the ‘Workloads’ tab, check the ‘C++ build tools’ checkbox and click the ‘Install’ button:

How to use PIP on Windows

To install, run a command of the form:

pip install PACKAGE

As an example, let's try to install the hashID program – this is a tool written in Python 3 that supports the identification of more than 220 unique hash types using regular expressions.

pip install hashID

Example of running hashID on Windows after installation:

hashid "3af0389f093b181ae26452015f4ae728:user"

How to set up Python as a module of Apache web server on Windows

Python can work as an Apache module, in this case Python scripts will be executed as, for example, PHP scripts are executed. Moreover, both interpreters can work simultaneously.

I assume that you have installed Python according to the above manual, and the web server is installed according to the tutorial ‘How to install web server on Windows 10 (Apache 2.4, PHP 7, MySQL 8.0 and phpMyAdmin)’.

Configuring Apache to run Python CGI

Open the Apache configuration file httpd.conf for editing, I have it located on the path C:\Server\bin\Apache24\conf\httpd.conf.

Find the line there

Options Indexes FollowSymLinks

and add ExecCGI to it. You should get the following line (ATTENTION: the set of options you may have is different):

Options Indexes FollowSymLinks ExecCGI

Now find the line:

#AddHandler cgi-script .cgi

Uncomment it, i.e. remove the # character at the beginning of the line and add .py to the end of the line. The new line will look something like this:

AddHandler cgi-script .cgi .py

Restart Apache

c:\Server\bin\Apache24\bin\httpd.exe -k restart

Create a Python Test Page

In the folder for your sites (I have it C:\Server\data\htdocs\) create a test.py file and copy it into it:

#!C:\Python\Python38\python.exe
print ("Content-type: text/html\n\n")
print
print ("<html><head>")
print ("")
print ("</head><body>")
print ("Hello.")
print ("</body></html>")

Pay attention to the very top line, that is, to C:\Python\Python38\python.exe – you need to replace it with your value, pointing to the python.exe file.

Open the address with the file: http://localhost/test.py

The word should appear

Hello

As shown in the screenshot below:

Python Source Editors

If you install Python not only to run programs, but also to learn the Python programming language, then you will need source code editors. I can recommend a couple of absolutely free ones:

  • NetBeans is a very powerful, but also quite resource-intensive IDE (integrated development environment). I have been working in this IDE for many, many years. Python support is enabled when the corresponding plugin is installed.
  • Notepad++ is a very resource-friendly code editor. Supports many programming languages.

How to fix Error Microsoft Visual C++ 14.0 is required

When using pip package manager on Windows, you may encounter an error:

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools":

Starting in 2017, the newest version of Visual Studio can replace any required version of Build Tools C++. To install the latest version, download and run the vs_buildtools.exe file. In the ‘Workloads’ tab, check the ‘C++ build tools’ checkbox and click the ‘Install’ button:

Additionally, update setuptools:

pip install --upgrade setuptools

Python compilation errors

Sometimes when installing packages using pip, you will encounter errors like this:

      C:\Users\MiAl\AppData\Local\Temp\pip-install-ozk99u7o\pycairo\cairo\pycairo.h(37): fatal error C1083: ЌҐ г¤ Ґвбп ®вЄалвм д ©« ўЄ«о祭ЁҐ: cairo.h: No such file or directory,
      error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.23.28105\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

It says that the compilation failed because the cairo.h file was not found.

One more example:

    netfilterqueue.c
    netfilterqueue.c(434): fatal error C1083: ЌҐ г¤ Ґвбп ®вЄалвм д ©« ўЄ«о祭ЁҐ: netinet/ip.h: No such file or directory,
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.23.28105\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

This error says that the compilation failed because the netinet/ip.h file was not found.

Exactly the same errors due to the lack of files with the .h extension can also occur on Linux. Files with the extension .h are the so-called headers – that is, source code files for import into other applications. In each case, you will have to individually find out which program the missing file belongs to and install this package.

Recommended for you:

Leave a Reply

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