How to setup the python environment

TIPS: In Windows parts of this instruction, use User as the username.

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:

TIPS: The naming pattern of msi installer for Windows is PowerShell-<version>-win-x64.msi. Select PowerShell-7.0.2-win-x64.msi if the lastest release version of PowerShell is 7.0.2.

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

TIP: Default install location of PowerShell is C:\Program Files\PowerShell, you can change it if you want.

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:

TIPS: Default install location of Miniconda is C:\Users\<username>\miniconda3, you can change it if you want.

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

, and see the first line in the window must like:

(base) PS C:\Users\User>

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:

TIPS: Default install location of Visual Studio Code is C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code, you can change it if you want.

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:

TIPS: Default install location of Git is C:\Program Files\Git, you can change it if you want.

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

TIPS: If it is failed to execute, you need to reinstall Miniconda.

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 is base) 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

TIPS: If your fullname is John Q. Public and email is john@example.com, you need to execute:

 git config --global user.name "John Q. Public"
 git config --global user.email "john@example.com"

6. Create a conda environment dev for development in Visual Studio Code

1. 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 is dev.

  • --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",
 }

TIPS: If you are using Linux, just ignore line 2 to 6, only:

 {
     "python.pythonPath": "~/miniconda3/envs/dev/python",
 }

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 FileNew 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.

最后更新于

这有帮助吗?