How to setup the python environment

circle-info

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

circle-exclamation

0. Install PowerShell (only for Windows)

1. Go to PowerShell Github lastest release pagearrow-up-right and download msi installer for Windows, see the red box in the following image:

circle-info

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:

circle-info

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 sitearrow-up-right 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:

circle-info

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:

4. Close Anaconda Powershell Prompt (Miniconda3), open PowerShell

circle-exclamation

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

circle-check

2. Install Visual Studio Code

1. Go to Visual Studio Code official sitearrow-up-right 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:

circle-info

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 sitearrow-up-right 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:

circle-info

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:

circle-info

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:

  • config --set channel_priority strict means set the channel priority to strict globally, click herearrow-up-right 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:

circle-info

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

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:

  • 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 herearrow-up-right for more details.

    • pylint: code analysis tool for Python, required in Visual Studio Code, click herearrow-up-right for more details.

    • autopep8: formatting tool for Python, required in Visual Studio Code, click herearrow-up-right for more details.

    • rope: refactoring tool for Python, required in Visual Studio Code, click herearrow-up-right for more details.

3. Execute the following command to activate environment 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:

This command will set the channel priority of current environment (now is dev) to strict.

5. Execute the following command to deactivate current environment:

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:

circle-info

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

circle-exclamation

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:

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.

最后更新于