For some reason, the number of views is steadily increasing "How to run Python on Windows without polluting the environment as much as possible" series [^ 1].
It seems that there is unexpected demand, so I will describe my recent favorite method.
Unlike the Embeddable version introduced in the past, this procedure is characterized by the fact that tkinter
and venv
can be used, and unlike the WSL version, it is not subject to HW restrictions.
Also, the procedure is only 2 steps, which is very easy.
Using the package management tool "Scoop" for Windows, install Python without polluting the Windows environment as much as possible so that you can develop and check the operation. ** (* The user environment variable PATH will be added / changed, so if you are interested in it, please withdraw here) **
First, install the package management tool "Scoop". Scoop itself is a very lightweight tool, and the modules installed by Scoop are installed in the user folder by default, so they do little to pollute the Windows system environment.
Please refer to the following article for a simple installation method of Scoop. [Environment construction using Scoop]-Qiita
If you want to use it for the time being, execute the following command from PowerShell to install Scoop. When you see the policy change message, enter "Y" to proceed.
Set-ExecutionPolicy RemoteSigned -scope CurrentUser
invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
After installing Scoop, install Python. Execute the following command from PowerShell or the command prompt.
scoop install python
** Python installation is complete. ** **
To run Python, start it with python
or python3
from a command prompt.
Then install the required modules with pip install
or create a virtual environment with python -m venv
.
The Python installed in this procedure calls the command in % userprofile% \ scoop \ shims
at startup, but the entity is located in% userprofile% \ scoop \ apps \ python
.
The latest version is basically installed for installation by Scoop.
For example, if you execute scoop install python
, Python 3.8.5
will be installed at the time of this article (2020/09/02).
If you want to install another version, you'll need to do a little extra work.
If you need an older version of Python 2.7
, 3.5
, 3.6
, 3.7
, please do the following:
scoop install git #Install Git as you will need it for the following tasks
scoop bucket add versions #Add versions bucket
scoop install python37 # python3.Install the "latest version" of 7
You can check the installable version with scoop search python
after executing scoop bucket add versions
with the above command.
#Search for packages
scoop search python
#=>'main' bucket:
#=> aws (1.18.44) --> includes 'python.exe'
#=> python (3.8.5)
#=> winpython (3.8.5.0)
#=>
#=>'versions' bucket:
#=> anaconda2 (2019.10) --> includes 'python.exe'
#=> miniconda2 (4.7.12.1) --> includes 'python.exe'
#=> python-alpha (3.9.0b5)
#=> python-beta (3.8.4rc1)
#=> python27-beta (2.7.18rc1)
#=> python27 (2.7.18)
#=> python35 (3.5.4)
#=> python36 (3.6.8)
#=> python37 (3.7.9)
In addition, only the latest version of each version can be installed by this procedure. There is also a way to specify a fine revision, but I will omit it here.
As mentioned above, the command installed by Scoop calls the command % userprofile% \ scoop \ shims
, but when multiple versions are installed, this is the last one installed. It may be overwritten by and run with an unintended version.
Basically, it is possible to execute with the intended version with the command according to the version.
(Python
or python37
in the above example)
However, common commands such as python3
and ʻidel / idle3will be executed with the last installed version. To fix this, you can use the
scoop reset command to fix the commands in
% userprofile% \ scoop \ shims`.
In the case of the above example, it will be as follows.
#Check the current version(Final installation is 3.7.9)
python3 -V
#=>Python 3.7.9
# 3.7.Switch from 9 to the latest version
scoop reset python
#=>Resetting python (3.8.5).
#=>Linking ~\scoop\apps\python\current => ~\scoop\apps\python\3.8.5
#=>Creating shim for 'python'.
#=>WARN Overwriting shim to python.exe installed from python37
#=>Creating shim for 'pythonw'.
#=>WARN Overwriting shim to pythonw.exe installed from python37
#=>Creating shim for 'python3'.
#=>WARN Overwriting shim to python.exe installed from python37
#=>Creating shim for 'idle'.
#=>WARN Overwriting shim to idle.bat installed from python37
#=>Creating shim for 'idle3'.
#=>WARN Overwriting shim to idle.bat installed from python37
#Confirmation of version change(3.7.9 to 3.8.Has changed to 5)
python3 -V
#=>Python 3.8.5
#3 from the latest version.Change to 7 series
scoop reset python37
#=> (Output omitted)
#Confirmation of version change(3.8.5 to 3.7.Has changed to 9)
python3 -V
#=>Python 3.7.9
I don't have version control the above way.
Directly hit the actual file of the desired version of Python located in % userprofile% \ scoop \ apps \
, and create and manage a virtual environment for each development environment with venv
.
#Virtual environment creation example(PowerShell)
#When executing at the command prompt, "~"" Part%userprofile%To
# 「activate."ps1" to "activate".Please change to "bat"
#Create folder for virtual environment(Please change to a suitable folder if necessary)
mkdir c:\venv
##################################################
# Python3.8.Creating a virtual environment for 5(=>c:\venv\python38)
##################################################
~\scoop\apps\python\3.8.5\python -m venv c:\venv\python38
C:\venv\python38\Scripts\activate.ps1 #Activate virtual environment
python -V #Version confirmation
#=>Python 3.8.5
pip install numpy #numpy "latest" version installation
pip install pandas #Install the "latest" version of pandas
pip freeze #Check installed modules
#=>numpy==1.19.1
#=>pandas==1.1.1
#=>(Omitted below)
deactivate #Get out of the virtual environment
##################################################
# Python3.7.Creating a virtual environment for 9(=>c:\venv\python37)
##################################################
~\scoop\apps\python37\3.7.9\python -m venv c:\venv\python37
C:\venv\python37\Scripts\activate.ps1 #Activate virtual environment
python -V #Version confirmation
#=>Python 3.7.9
pip install numpy==1.15 # numpy 1.15 installations
pip install pandas==1.0 # pandas 1.0 installation
pip freeze #Check installed modules
#=>numpy==1.15.0
#=>pandas==1.0.0
#=>(Omitted below)
deactivate #Get out of the virtual environment
##################################################
#Confirmation of real environment
##################################################
python -V
#=>Python 3.8.5
pip freeze
#=> (No return value)
[^ 1]: [How to run Python on Windows without polluting the environment as much as possible (Windows 10, version 1607 or later using WSL only)]
[How to run Python on Windows without polluting the environment as much as possible (Python embeddable version)]
Recommended Posts