Define your project environments
September 23, 2021 ยท View on GitHub
Requirements
Install
Python Virtual environment with conda
A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
It is always good practice to work in a virtual environment, isolated from your other Python projects.
In this course, we will use conda, both as
See also the conda cheat sheet.
Exercise 1
In your terminal
-
list the
condaenvironments currently available on your machine -
For the
baseenvironment- list the installed packages
- what were the channels used to install the different packages?
-
Create a conda environment called
myenvwith Python 3.8.2- install
numpy - install
matplotlibusingpip - list the installed packages
- export the environment to a cross-platform
myenv.ymlfile - delete the environment
myenv
- install
-
Recreate the conda environment
myenvfrom the previousmyenv.ymlfile- update the Python version to its latest version.
- install
scipyusing theconda-forgechannel - export the environment to a cross-platform
myenv2.ymlfile
Integrated Development Environment - VSCode
Visual Studio Code (VSCode) is recommended to ease your coding experience.
Important: Use the command palette ( CMD/CRTL + Maj + P ).
See also
Tip: You may also consider synchronizing your settings, in order to keep the same setup each time you use VSCode (even from a different machine).
Launch VSCode
From Anaconda Navigator
Launch Anaconda Navigator
-
(Windows) Open Start Menu (Windows key)
-
(MacOS) Open Spotlight (CMD + Space)
-
Search for and open
Anaconda-Navigator -
Click on VSCode->Launch
From the command line
# cd path-to-your-projet
code . # open VSCode and define a workspace from current directory
code README.md # open README.md in VSCode
See also
Per project configuration
The .vscode directory, placed at the root of your project workspace, contains a list of suggested extensions together with the corresponding settings, code snippets, etc. You can share the .vscode directory with your collaborator, e.g., using git/GitHub, to make sure you have some common setup.
Note: This configuration only applies in the current workspace and has precedence over global user settings, see also Settings and Snippets.
Extensions
All registered extensions are available on VSCode's Marketplace. Extensions can also be installed locally from your VSCode window, see the documentation.
The .vscode/extensions.json file contains a list of suggested extensions that will greatly simplify your coding workflow.
Exercise 2
- Install the extensions listed in .vscode/extensions.json
- Activate/Deactivate the
Multiline Commentssetting of theBetter Commentsextension
Settings
VSCode settings You can configure Visual Studio Code to your liking through its various settings. Nearly every part of VSCode's editor, user interface, and functional behavior has options you can modify.
To do so, you can either define settings
- Globally: Open the Command Palette (
CMD/CRTL + Maj + P) and type eitherOpen User Settings, orOpen Settings (JSON),
- Per project: see settings.json.
See also guilgautier/vscode-workflow repository.
Exercise 3
- Change the location of the VSCode sidebar left <-> right.
Snippets
Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements.
Many language-specific extensions already provide some useful snippets. While some of them exactly match your needs, some others might be missing or you may not remember how to trigger them.
For these reasons you may create your own snippets to increase your productivity. To do so, you can either define snippets
- Globally: Open the Command Palette (
CMD/CRTL + Maj + P) and typeConfigure User Snippetsand choose to create- language-specific snippets,
- generic snippets that can be triggered in different scopes.
- Per project: see workspace.code-snippets
See also guilgautier/vscode-workflow repository.
Exercise 4
-
Create your own
mygithubsnippet and make it available only in Markdown (.md) files. -
Create an
forenumPython snippet, that displays when triggered in Python (.py) filesfor idx, val in enumerate(values): print(idx, val)
Version control within VSCode
All, and more, of what we experimented with git from the command line (cf. notes/command_line.md) can be achieved within VSCode, using