Precautions that must be understood when building a PYTHON environment

There are 3 types of installation formats

In order to build a python development environment, you will usually download the installer from the official page below. Python Releases for Windows https://www.python.org/downloads/windows/

For example, as of January 21, 2020, python3.8.1 is distributed in the following format.

To explain in order from the top

--Instruction (help) --64bit version *** Embedded *** zip file --64bit version installer --64bit version web installer --32bit version *** Embedded *** zip file --32bit version installer --32bit version web installer

Will be. Isn't it basically one of the installers (~ installer) to be used for personal study purposes?

I will explain each in detail. In addition, Download Windows help file and Download Windows x86 ~ are not explained. (Do you understand the meaning without explaining that it deviates from the purpose?)

Download Windows x86-64 executable installer The most orthodox installer. It is an installer format file, and since the installer itself contains all the files, it can be installed without an internet environment. In addition, you can set the environment in detail, such as whether to make it available only to specific users, to register the python path in the environment variable, or to install py.exe (details will be described later). *** If you have no reason, or if you are new to python or don't want to read the entire python manual, we highly recommend installing in this format. *** ***

Download Windows x86-64 web-based installer Download A web installer version of the Windows x86-64 executable installer. To put it simply, the web installer is a format that installs the files at the time of installation via *** network ***, and the installer itself is very lightweight. It is no different from a normal installer except that a networked environment is required for installation. It is often used for things that require the installation of *** Un GB ***, such as visual studio and rockstar game launcher.

Download Windows x86-64 embeddable zip file *** Problem child. *** *** If you translate it, it will be an embedded format distribution file. It contains the (*** minimum ) configuration ( + α super-special configuration file ***) required to start python in a zip file without going through the installer. , It is a Suguremono that can be used for Sugu if it is thawed. *** Of course there is a price to pay. *** ***

*** Besides, it is a Han-like specification without pip. (If you want to use pip, you need to go through the program that configures pip after completing the environment construction) ***

What is the price of embeddable zip file?

This is the main subject of this article. In this distribution format, some of the included files are not found in other installers. The file is *** python 〇〇.__pth (where 〇 is the python version) ***. This guy strips the fangs of bad boys who don't read every corner of the python manual.

In python, when called by specifying a file as a command line argument (Command like python C: \ ~~ \ main.py) There is a behavior to add the directory path of the file specified in the command line argument to sys.path [0] as the module reference setting, which is the official document. It is described in. [https://docs.python.org/ja/3/library/sys.html?highlight=sys%20path]

This allows you to read a module in another py file under the same directory in your program. Below is an example of sys.path [0] in your environment.

['(Full path of py file directory passed as command line argument)',
 'C:\\(python installation directory)\\python37.zip',
 'C:\\(python installation directory)\\DLLs',
 'C:\\(python installation directory)\\lib',
 'C:\\(python installation directory)',
 'C:\\Users\\(username)\\AppData\\Roaming\\Python\\Pytho
 'C:\\(python installation directory)\\lib\\site-packages',
 'C:\\(python installation directory)\\lib\\site-packages\\win32',
 'C:\\(python installation directory)\\lib\\site-packages\\win32\\li
 'C:\\(python installation directory)\\lib\\site-packages\\Pythonwin
 '(Full path of py file directory passed as command line argument)']

It's as per the official documentation.

However, in the embeddable zip file, it looks like this:

 ['C:\\(python installation directory)\\python37.zip',
 'C:\\(python installation directory)',
 'C:\\(python installation directory)\\lib\\site-packages']* Added because it was configured with pip

????? The path you are referring to is clearly missing. In the installer version, it works as described, but in *** embeddable zip file, the directory of the file specified in the command line argument is not specified in the module reference path. *** *** Other than that, the reference path is different. About this behavior [https://docs.python.org/ja/3/using/windows.html] The behavior is explained in (3.8. Searching for modules) in the above manual.

In the embeddable zip file, if *** [python 〇〇._pth] exists, it will start in quarantine mode, so you need to specify all module reference settings yourself. *** ***

It's quarantine. isolation. Murahachibu mode.

As a countermeasure for this,

-(1) Set the path of the module to be referenced in the source code one by one. -② Create [appropriate name.pth] in the directory containing python.exe, and describe the directory path that you want to refer to when you start python.exe. --③ Delete the python 〇〇 .__ pth file (release quarantine mode)

There are some disadvantages, but each has its own disadvantages.

――If it is ①, you need to understand all the modules that your source refers to, and describe them comprehensively (pip package, self-made module, etc.) --In case of (2), every time a small python program increases, it is necessary to describe it in the pth file, and if a file or variable name with the same name exists, an overloading error will occur. ――If you delete ③, it will behave the same as the installer version, but you will not be able to make detailed settings. (Since it is assumed that you will make detailed settings when using the embeddable zip file, then you should use the installer from the beginning.)

Besides that, the difference from the installer version is

--py.exe is not included

There is. py.exe is called a python launcher, which manages multiple python versions and launches the appropriate version according to the source code. (It is effective when installing multiple versions for the purpose of trying a new version.)

In the installer version, you can only install python in a user-specific directory (under AppData on Windows) unless you make special settings at the time of installation, but there is a reason to deal with security issues. (Because if you install python.exe in a directory that can be accessed by all users, you can execute malicious script files and scripts)

The embeddable zip file does not impose any restrictions and can be installed anywhere you like, so use it only if you can manage everything yourself.

There are multiple installers in python, but you need to understand the differences in the included ones and the differences in behavior. (Not limited to python)

So when do you use the embeddable zip file?

In conclusion, it is used when distributing what you created to a terminal without a python environment. (Installed on the back end of the server etc ...) Also, it is necessary to assume that the python version will not be updated (the python.exe itself will not be updated once it is distributed). Even if you need to update python, you have to handle version control yourself because there is no py.exe.

Finally

Not limited to python, but it is said that there are multiple installation formats *** There are multiple for some reason. *** *** If you use it without confirming that it is similar to the installation format of other apps or past experience *** I'm sure it's a painful return. *** ***

Recommended Posts

Precautions that must be understood when building a PYTHON environment
Building a Python virtual environment
Building a Python virtual environment
Building a Python environment on Mac
Error when building mac python environment
Building a Python environment on Ubuntu
Building a virtual environment with Python 3
Precautions when creating a Python generator
[Pyenv] Building a python environment with ubuntu 16.04
Building a Python3 environment with Amazon Linux2
Precautions when pickling a function in python
Building a Python 3.6 environment with Windows + PowerShell
[Mac] Building a virtual environment for Python
Building a Python development environment for AI development
Building a python environment with virtualenv and direnv
Building an environment that uses Python in Eclipse
Building a Python environment with WLS2 + Anaconda + PyCharm
[Python] Web development preparation (building a virtual environment)
A memo when creating a python environment with miniconda
Think about building a Python 3 environment in a Mac environment
Building a Python environment on a Sakura VPS server
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Write about building a Python environment for writing Qiita Qiita
Building a Docker working environment for R and Python
Building a TensorFlow environment that uses GPU on Windows 10
What's in that variable (when running a Python script)
Building and enabling a python virtual environment, etc. (venv)
Procedure for building a CDK environment on Windows (Python)
conda memorandum: Building a Python environment with supercomputer ITO
Building a Python environment for programming beginners (Mac OS)
Memo for building a machine learning environment using Python
Building a python environment for artificial intelligence (Chainer / TensorFlow / CSLAIER)
Building a Python environment on a Mac and using Jupyter lab
Summary from building Python 3.4. * From source to building a scientific computing environment
The shortest memo for building a python environment that takes less than 5 minutes to work.
Use a macro that runs when saving python with vscode
Building a development environment for Android apps-creating Android apps in Python
Building a Hy environment for Lisper who hasn't touched Python
About the contents of wscript when building a D language environment like that with Waf
[Python] Building a virtual python environment for the pyramid tutorial (summary)
Build a Python environment offline
Until building a Python development environment using pyenv on Ubuntu 20.04
Here's a summary of things that might be useful when dealing with complex numbers in Python
About the matter that torch summary can be really used when building a model with Pytorch
[Python] Make a graph that can be moved around with Plotly
Building a Docker working environment for R and Python 2: Japanese support
How about Anaconda for building a machine learning environment in Python?
Building a Windows 7 environment for getting started with machine learning with Python
A record that GAMEBOY could not be done in Python. (PYBOY)
From building a Python environment for inexperienced people to Hello world
From installing Ansible to building a Python environment in Vagrant's virtual environment
I made a shuffle that can be reset (reverted) with Python
When creating an environment that uses python django on Ubuntu 12.04 LTS
Everything from building a Python environment to running it on Windows
Building a kubernetes environment with ansible 2
Precautions when using pit in Python
Create a Python environment on Mac (2017/4)
Create a virtual environment with Python!
[Python] Be careful when using print
Create a python environment on centos
Precautions when using phantomjs from python