Python¶
What is Python?¶
Python is a versatile, high-level programming language known for its simplicity, readability, and ease of use. It supports multiple programming paradigms, including object-oriented, procedural, and functional programming. Its extensive ecosystem of libraries and frameworks makes it ideal for tasks ranging from web development to data analysis, artificial intelligence, and automation. Python’s active community and abundant documentation help developers learn quickly and implement effective solutions. Its clear syntax promotes code readability, making it a popular choice for both beginners and experienced developers.
Note
In our lab, we recommend using Python 3.10.
If Python 3.10 is not available, you can install it with the following command:sudo add-apt-repository ppa:deadsnakes/ppa && sudo apt-get update && sudo apt-get install python3.10
Useful resources:
Integrated Development Environment (IDE)¶
We recommend using Visual Studio Code (VSCode) as the main editor for Python development. VSCode is a lightweight, open-source editor offering powerful features such as IntelliSense, built-in debugging, Git integration, and a vast extension marketplace. The Python extension enhances the development experience with tools like linting, auto-completion, and seamless integration with virtual environments. VSCode’s intuitive interface and numerous tools make it an excellent choice for all skill levels.
To install Visual Studio Code, download the .deb file from the official website and run:
sudo dpkg -i code_*.deb
In VSCode, install the following Python extensions via the Extensions icon in the sidebar:
autoDocstring
Black Formatter
Pylance
Python
Python Debugger
Optional:
reStructuredText
Github Actions
Recommended settings in VSCode:
In File > Preferences > Settings, click “Open Settings (JSON)” (icon at the top right) to open settings.json and add:
"[python]": {
"editor.rulers": [
100
],
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"formatting.blackArgs": [
"--line-length",
"100"
]
},
Virtual Environment¶
virtualenv allows you to create isolated Python environments to manage dependencies for each project.
Install virtualenvwrapper:
pip install virtualenvwrapper --break-system-packages
Create a folder for your environments:
mkdir ~/my_envsAdd to your
~/.bashrc:export WORKON_HOME=$HOME/my_envs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.10 source /usr/local/bin/virtualenvwrapper.sh
Create a virtual environment:
mkvirtualenv venv # or for a specific version: mkvirtualenv -p python3.10 venv
Activate the environment:
workon venvDeactivate the environment:
deactivate
Example: create an environment for onsetpy
# 1. Create the virtual environment
mkvirtualenv onsetpy
# 2. Activate the environment
workon onsetpy
# 3. Install onsetpy
git clone git@github.com:Onset-lab/onsetpy.git
cd onsetpy
pip install -e .
# 4. Run a script
python onset_create_epinsight_report -h
# 5. Deactivate the environment
deactivate