How to setup the python environment
WARNING: In Linux parts of this instruction, only apply to any Debian-based distributions, e.g. Debain, Ubuntu, Linux Mint, elementary OS, etc.
If you are using the distribution that its package manager is not apt
, you need to convert all commands use apt
to the version of the package manager of the distribution that you are using, click here to see how to convert.
0. Install PowerShell (only for Windows)
1. Go to PowerShell Github lastest release page and download msi installer for Windows, see the red box in the following image:

2. Open the installer to install PowerShell, make sure every options in the installation are the same as the following image:

1. Install Miniconda
1. Go to Miniconda official site and click Miniconda3 Windows 64-bit to download the installer, see the red box in the following image:

2. Open the installer to install Miniconda, make sure every options in the installation are the same as the following image:

3. Open Start Menu, goto Anaconda3 (64-bit), open Anaconda Powershell Prompt (Miniconda3) and execute the following commands line by line:
conda init
cd "$([Environment]::GetFolderPath('Personal'))\PowerShell"
Write-Output "chcp 65001`ncls" | Out-File "Profile.ps1" "utf8" -Append
4. Close Anaconda Powershell Prompt (Miniconda3), open PowerShell
WARNING: PowerShell here is referred to PowerShell (or pwsh.exe) that installed from step 0, not legacy Windows PowerShell (or powershell.exe) integrated in Windows. To open PowerShell, you can:
Open Start Menu, goto PowerShell and open PowerShell 7 (x64) if version 7 of PowerShell is installed.
Open Run dialog with keyboard shortcut
⊞ Win
+R
, input pwsh and press OK.Use Windows Search, search pwsh and you can find it.
, and see the first line in the window must like:
(base) PS C:\Users\User>
If yes, Miniconda is successfully installed on Windows.
2. Install Visual Studio Code
1. Go to Visual Studio Code official site and click Download for Windows to download the installer, see the red box in the following image:

2. Open the installer to install Visual Studio Code, make sure every options in the installation are the same as the following image:

3. Install Git
1. Go to Git official site and click Download <version>
for Windows to download the installer, see the red box in the following image:

2. Open the installer to install Git, make sure every options in the installation are the same as the following image:

4. Configure and update conda
1. Open PowerShell (Windows) or terminal (Linux).
2. Execute the following command to check the version of conda:
conda --version
3. Execute the following commands line by line to configure and update conda:
conda config --set channel_priority strict
conda update --all -y
config --set channel_priority strict
means set the channel priority to strict globally, click here for more details about channel priority.update --all -y
means update all packages in current conda environment (now isbase
) without confirm.
5. Configure Git
1. Open PowerShell (Windows) or terminal (Linux).
2. Execute the following command to check the version of Git:
git --version
6. Create a conda environment dev
for development in Visual Studio Code
dev
for development in Visual Studio Code1. Open PowerShell (Windows) or terminal (Linux).
2. Execute the following command to create a conda environment:
conda create -n dev --strict-channel-priority -y python=3 jupyter pylint autopep8 rope
create
means create a conda environment.-n dev
means name of the environment isdev
.--strict-channel-priority
means when creating new environment, all packages that installed will follow the strict channel priority.-y
means without confirm.python=3 jupyter pylint autopep8 rope
lists all packages will be installed when creating new environment, they are:python=3
: Python 3.jupyter
: Jupyter, include IPython and Jupyter Notebook, click here for more details.pylint
: code analysis tool for Python, required in Visual Studio Code, click here for more details.autopep8
: formatting tool for Python, required in Visual Studio Code, click here for more details.rope
: refactoring tool for Python, required in Visual Studio Code, click here for more details.
3. Execute the following command to activate environment dev
:
conda activate dev
This command will activate environment dev
over from base
.
4. Execute the following command to set the channel priority of environment dev
to strict:
conda config --env --set channel_priority strict
This command will set the channel priority of current environment (now is dev
) to strict.
5. Execute the following command to deactivate current environment:
conda deactivate
This command will deactivate environment dev
back to base
.
7. Configure Visual Studio Code
1. Open Visual Studio Code, you can open it from:
Start Menu (Windows) or Applications Overview (Linux).
Open Command Prompt, PowerShell (Windows) or terminal (Linux), and execute
code
.
After you opened it, you must see the interface like the following image:

2. Follow the instructions of the following image to install Python extension:

3. Open settings of Visual Studio Code by:
Follow the instructions of the following image:

Use keyboard shortcut
Ctrl
+,
.
4. Click Open Settings (JSON) button in the upper right corner of Visual Studio Code, see the red box in the following image:

5. Copy the following content into editor area:
{
"terminal.external.windowsExec": "<install location of PowerShell>\\7\\pwsh.exe",
"terminal.integrated.shell.windows": "<install location of PowerShell>\7\\pwsh.exe",
"terminal.integrated.shellArgs.windows": [
"-NoLogo"
],
"python.pythonPath": "~/miniconda3/envs/dev/python",
}
WARNING: If you are using Windows, You need to replace <install location of PowerShell>
to the install location of PowerShell that you installed and escape \
to \\
.
For example, if your PowerShell is installed to default location C:\Program Files\PowerShell, you need to replace <install location of PowerShell>
to C:\\Program Files\\PowerShell
, like the following image:

6. Reopen Visual Studio Code.
8. Run Python script in Visual Studio Code
1. Create a new file in Visual Studio Code by:
New file link in the Welcome page.
Select File → New File in menu bar.
Use keyboard shortcut Ctrl+N.
2. Write the following script into editor area:
print('Hello, World!')
This script means print Hello, World!
in terminal.
3. Save to a Python file (with file extension .py), like helloworld.py in the following image:

4. Click Run Python File in Terminal button in the upper right corner of Visual Studio Code, see the red box in the following image:

After clicked it, you must see the result like the following image:

You can see Hello, World!
in terminal area.
最后更新于
这有帮助吗?