Create a Python Development Environment with VS Code

Naga Sanka
Level Up Coding
Published in
7 min readJun 13, 2020

--

Develop in Windows with Linux Terminal

MobaXterm Teminal in VS Code and MobaXterm

In this post, let’s see how to set up a clean environment in Windows using Visual Studio Code.

First, we need to download VS Code Installer from official site. As I prefer to try everything as non-admin user, download the 64 bit User Installer 1.46.0 version. Run the downloaded “VSCodeUserSetup-x64–1.46.0.exe”, install with these options: location - “C:\Users\UserName\Apps\VSCode”, No Start Menu folder, Select all Additional Tasks. Launch VS Code, we have a nice, clean editor for our development.

If you prefer not to share all your data usage and errors to Microsoft, open settings File > Preferences > Settings, search for telemetry, and uncheck the Telemetry: Enable Telemetry setting.

When you open VS Code, you will see the Welcome Tab. Click on Install support for Python in Customize section, this will install Python extension for VS Code.

When you open new terminal (Terminal > New Terminal), we can either see Windows Power Shell or Command Prompt depending upon your system. I am much comfortable to use Linux commands rather than Windows commands. Let’s see how can we change the default terminal.

MobaXterm

There are many options available to get Linux Terminal in Windows like PuTTY, Git Bash, WSL, MobaXterm, Cygwin. MobaXterm is powerful and it gives other cool features like file browser along with terminal, sftp, X server to list some of them. Download the Portable edition MobaXterm Home Edition v20.4. Extract the downloaded zip file in your favorite location: “C:\Users\UserName\Apps\MobaXterm”. You can choose any folder, but make sure there are no spaces in your path because Unix/Linux file system does not like spaces in path. Inside this folder, create another empty folder with name “slash”. Open the MobaXterm executable. We need to update few settings to keep our preferences/apps to retain on every restart of MobaXterm. Open configuration (Settings > Configuration > General), choose “slash” folder for root directory and “C:\Users\UserName” for home directory. Then it will ask to restart the application. Click the “Start local terminal” to see the familiar bash terminal.

Add Windows Path to MobaXterm

If you want to open Windows applications from MobaXterm, Select “Use Windows PATH” in MobaXterm Settings > Terminal.

Install Additional Packages

MobaXterm includes most of the commonly used commands by default. If you need any additional packages/commands, we can easily add them to it. You can visit their plugin page, download “.mxt3” file and move them to the installed folder where the MobaXterm_.exe exists, restart the Terminal to get the command working. You can also install other commands for example make, Start MobaXterm, click on “Packages” button, search for your required package and click Install/update button. This will download necessary libraries and other dependencies and install the package. Once installation is finished, close MobaXterm and Open a new Terminal in VS Code, now you can check the command like“make -v”.

Update Terminal in VS Code

Now the final and important part, assigning default terminal in VS Code. First we need to find the bash.exe in “slash” folder that we created above. It should be “slash\bin\bash.exe”. Open VS Code settings (File > Preferences > Settings) and click the “Open Settings (JSON)” button in the top right corner. It will open “settings.json” file, add below lines and save the file to define the integrated shell for windows (write the full path to “slash” folder) and shellArgs to enable running bash as a login shell.

// MobaXterm Bash
"terminal.integrated.shell.windows": "C:\\Users\\UserName\\Apps\\MobaXterm\\slash\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": ["--login"],

Save settings JSON file and restart VS Code to see MobaXterm terminal in VS Code.

We can also define environment variables in MobaXterm terminal using VS Code settings.

// Define Environment Variables
"terminal.integrated.env.windows": {
"dev_dir": "C:\\Users\\UserName\\Development"
},

The shell, shellArgs, env and cmd terminal settings all support resolving variables.

// Open the terminal in the currently opened file's directory
"terminal.integrated.cwd": "${fileDirname}"

Install Python

In order to get Python in Windows, I suggest to install Anaconda Individual Edition instead of installing Python software. Anaconda Individual Edition is a free, easy-to-install package manager, environment manager, and Python distribution with a collection of 1,500+ open source packages with free community support. Anaconda is platform-agnostic, so you can use it whether you are on Windows, macOS, or Linux.

Installing Anaconda Individual Edition is so simple, you download the installer (Anaconda3–2020.07-Windows-x86_64.exe) and follow the directions. If you follow the directions as they are, the Anaconda3 is not added to the Path environment variable.

If you want to use anaconda/conda/python commands in MobaXterm, you need to add several paths to your Path environment variable. Those paths are (instead of Anaconda3 the folder, may be Anaconda2 depending on the Anaconda version on your Windows PC):

C:\Users\UserName\Apps\Anaconda3
C:\Users\UserName\Apps\Anaconda3\Scripts
C:\Users\UserName\Apps\Anaconda3\Library\bin

If you already added them to the Windows Path while installing Anaconda, you can ignore these commands. In order to add these to Path environment variable, open Terminal (Terminal > New Terminal) in VSCode and enter the following command: Update the path according to your Anaconda installation path. This will create/edit your “.bash_profile” in your $HOME folder.

echo "export PATH=\"/home/UserName/Apps/Anaconda3:/home/UserName/Apps/Anaconda3/Scripts:/home/UserName/Apps/Anaconda3/Library/bin:$PATH\"" >> .bash_profile

Close the terminal and reopen, check the conda/python version using the below commands. If these commands are working, you are all set to use Python in terminal.

conda --version
python.exe --version
# Update conda
conda update -n base -c defaults conda

Conda Environment

It is always advisable to use different conda environments for different versions of Python and/or packages. You can create a separate conda environment for each Python version you want to use by running:

conda create --name mypy37env python=3.7

For more details on creating and managing conda environments, take a look at this user guide.

Activate Conda Environment

Due to folder path differences in Windows and MobaXterm and also the new line character differences in DOS and Unix, you may see some issues using conda commands like activate/deactivate. If you have these issues, then create a script file for every new environment by running below commands in a Terminal (replacing the environment name):

conda shell.posix activate mypy37env > /home/UserName/mypy37env.sh
dos2unix /home/UserName/mypy37env.sh
. /home/UserName/mypy37env.sh

First two commands in the above are required only once, so next time when you want to switch the environment just run the last command. If you would like to use this environment every time, you can add this to your bash_profile file as below.

echo ". /home/UserName/mypy37env.sh" >> ~/.bash_profile

After adding this to bash_profile file, you need to close the terminal and reopen to see your terminal started with this environment. You can confirm this with ‘conda info’ command.

Select Python Interpreter

We need to tell VS Code which Python Interpreter to use for any python file that is opened. Let’s create an empty file using File->New File and enter simple python print statement as below:

print("Welcome to Python in VS Code!")

When you save this file as fileName.py, VS Code shows an alert to select the interpreter. When you click the “Select Python Interpreter” button, it will show all the available Python Interpreters including the conda environments that you created in Command Palette. Select the “mypy37env” for this. The selection will be shown in bottom left corner of VS Code window. Then you will see another alert to install “pylint”, which is a tool that checks errors in your code, helps you to follow coding standard. Click Install and select “Install using Conda”, it will open a terminal and install the necessary packages in the selected conda environment.

Now, we need to tell VS Code NOT to activate the conda environment when a new terminal is opened as we already added activation script to bash_profile file above.

// Disable activating conda environment in new terminal
"python.terminal.activateEnvironment": false,

Git Version Control Setup

The final and most important step to finish the development environment setup is Git Setup. VS Code includes support for Git and source control right away. MobaXterm comes with Git installed, so all we need to tell VS Code to use that for Version Control. Open VS Code settings.json file, add below lines to define the git path and save the file. More details about using version control in VS Code are here.

// MobaXterm Git
"git.path": "C:\\Users\\UserName\\Apps\\MobaXterm\\slash\\bin\\git.exe",

We can connect to GitHub using SSH to push all the repositories from our computer to remote. This will help us to GitHub without supplying your username and personal access token at each visit. We will use MobaXterm Terminal to run all these commands.

This completes the process to setup VS Code for Python development.

Conclusions

VS Code and MobaXterm are very powerful tools on their own. Combining both in one environment gives us many opportunities to work in a single development environment.

Next article: Use VS Code for Remote Development

--

--