Python in VSCode
Python coding in VS Code
Steps:
- Install first python in windows
- Install extensions in VS Code
- pylint
- python package
- black formatter
- Set python path in system environment path variable
- Setup vertual environment
- As it will help you to have dependencies and packages to be installed only for the workspace folder rather than for global system and will help to keep the projects isolated with each other
- Command: python -m venv .venv
- Command to activate venv:
- .\.venv\Scripts\activate
- It will create a new folder in your existing python project folder and will install required python packages to run your code and you can install dependencies as well in it.
- Setup dependencies by using requirements.txt file
- Provide all packages name in txt file and then you can use single pip command to install them
- To check pip version you need to type
- Command: pip --version - but after navigating to desired location where pip is installed in python folder.
- Command: python -m pip --version - you can use this command to find python version from any folder don't need to navigate to right location.
- Command to install dependencies by using requirements.txt file.
- Global upgrade pip version - py -m pip install --upgrade pip
- venv upgrade pip version - pip install --upgrade pip
- global - py -m pip install -r requirements.txt
- local -
- .\.venv\Scripts\activate
- pip install -r .\requirements.txt
- Caution always be sure of the commands for pip and python to know which python interpreter you are using.
- Debug the code
- Use create launch.json file to set the settings for debugging as it will help to understand the mechanism and will provide granual control as well.
- To know further about debugger search for vscode debugger recipes
- You will get complete json object to define your debugger for python frameworks as well like FastAPI, Django and others.
- Unit testings for python
- Use test option in VSCode and it will ask for test folder for configuration which you have defined for testing code. Then you can use it nicely.
- You have define test methods and then you can debug them as well by providing break point in main code for which test cases are written.
- VS Code will maintain history of all test case runs and will help you to know the exact issue with code at the point of unit test.
Further reading materials for reference are:
- https://code.visualstudio.com/docs/languages/python
- https://code.visualstudio.com/docs/python/linting
- https://code.visualstudio.com/docs/python/testing
- https://code.visualstudio.com/docs/python/environments
- https://code.visualstudio.com/docs/python/jupyter-support-py
For market extensions:
- https://marketplace.visualstudio.com/items?itemName=ms-python.python
- https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
- https://marketplace.visualstudio.com/items?itemName=ms-python.pylint
- https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
Comments
Post a Comment