Skip to content

Setup Guideline VSCode

Alexander Fottner edited this page Mar 11, 2021 · 7 revisions

Firstly make sure that you have installed the Python extension for VSCode.

Now follow the instructions written in the README. Make sure to choose VSCode as your code editor. If you accidentally chose the wrong setting, you must repeat the whole process again.

image

Now open VSCode and navigate to the freshly created folder structure (either open VSCode directly in the corresponding folder or browse to the directory).

Right click on the README.md file and select “Open Preview”. This will display the compiled Markdown and not the plain text. Either open a new Terminal inside VSCode or use the one which is already open and navigate to your project folder.

Create Virtual Environment

pip SetUp

If you chose to use “pip” as your package manager create a new virtual environment by running 

"python -m venv name_of_your_project" in your project directory.

As next step you have to ensure that VSCode uses the proper Python interpreter and environment (the one you just created). To ensure this there are two options. The first one is to open a .py file and click on the interpreter button at the bottom left. Then select the correct interpreter (should contain the name of your virtual/conda environment, in the picture a conda environment is used, for a virtual environment only the name is different) from the options displayed in the search bar.

image image

This will set the "python.pythonPath" setting in the settings.json file in the .vscode folder. The second option would be to set this manually by copying the same path into the settings.json.

image

Open a new Terminal in the virtual Environment In VSCode you can press Ctrl+Shift+P  and enter Terminal: Create New Integrated Terminal

image

Conda SetUp

In the case you chose conda, create the environment as described in the README. The name “guide” will be replaced by the name of your project.

conda env create --name <env_name> --file=environment-dev.yml

conda activate <env_name>

image

Install Dependecies

Check if the Virtual Environment is activated.

Then you can install all dependencies as described in the README.

pip install -r requirements.txt -r requirements-dev.txt

Test Setup

Test with Terminal

As next step you should run the tests to ensure everything went right. To do that you have two options. The first one is to run the tests in a terminal as described in the README:

python setup.py test

python setup.py testcov

image

Test Discovery with VSCode

The second one is to run them by using the Test Discovery and Test Interface of VSCode. For VSCode to be able to discover the tests and accordingly use these tools you have to make your code available as a package.

There are two options to achieve this.

The easiest one is to run  "pip install -e ." from your projects directory (same level as your setup.py) in your terminal. This installs your code in editable mode by running "python setup.py develop" and creates a link to your code in the site-packages folder, which automatically incorporates any changes you make. This means when calling “import your <project’s name>” within a script you always use the latest version of your code  The second option requires you to create a .pth file inside your site-packages folder which points to the “src” directory that holds your code (just open a plain text file, copy the corresponding path of the “src” directory and paste it in the text file, lastly save the file with a .pth ending). After completing one of those options you will be able to discover and run tests within VSCode. Now run the prebuilt tests to check if everything went right. If so, you can move on to the last part of this guideline, enabling the use of Jupyter.

Enabling Jupyter

Normaly if you try to open the already existing example.ipynb insite the notebooks folder VSCode propts with that you need to install ipykernel to run it inside VSCode.  Install it.

Then make sure the Kernel is using you virtual environment you created earlier by checking the Kernel in the upper right corner. Select you Virtual Environment and test to import from your moduels 

Outside VSCode you should take following steps:

If you want to use Jupyter Notebooks within your project, you have to firstly install the required Jupyter packages and add them to the specific requirements. To actually use a notebook you have two options.

First option: Open the Notebook within your project environment by running "jupyter notebook" in your internal terminal and browsing to your file. This ensures that your kernel uses the correct Python interpreter.

If you prefer to run all your Notebooks from a separate environment, you have to follow a different path.

Second option: Make your project specific virtual/conda environment available as a separate kernel by running (replace pip with conda if you use a virtual environment):

image

Please substitute your environment name for “guide”.

This enables you to use your virtual/conda environment in a notebook which has been opened from a different environment.

Congrats you’ve worked through the guideline of the Python project template by [at]!

We wish you happy developing! 😊