- Run Scripts from Command Line - Mac Terminal/ Windows WSL
- Version Control Your Code - Git
- Edit and Debug Your Code - VS Code
- Manage Your Python Version - Mambaforge
- Install Mambaforge
- Useful Mambaforge Commands
- Manage Your Python Package Versions - Poetry
Mac's Terminal app is ideal for this because Mac has a Linux based OS and most apps are deployed to Linux machines in Production due to their cost effectiveness.
Since Window's is not a Linux based OS, you can set up a Linux virtual environment with WSL.
Back to Table of Contents WSL Article Reference
NOTE: If you already have a legacy version of WSL installed (i.e. WSL1 instead of WSL2), uninstall it so you can follow the install steps below
- Run Windows PowerShell as an administrator and select yes to the prompt asking if you want to allow the app to make changes to your device
- Run the command
wsl --install
- Restart your computer
- When you log back in, you will now see an Ubunutu terminal loaded
- Close out of the Ubuntu window, and open PowerShell
- Run the command
wsl -l -v
to list your installed Linux DIstributions and you should see something like the following - To launch your default Linux distribution from PowerShell, simply run the command
wsl
No matter the command line interface (CLI) used, it helps to format your CLI to work well with Git so that you know what branch you are working on and don't accidentally commit code to the wrong branch.
NOTE: Code below found in this Medium article
- Open your terminal of choice (i.e. Mac users default terminal/ Windows users WSL)
- Cd to your home directory
cd ~
- Create .bash_profile file if it doesn't already exist
touch .bash_profile
- Open .bash_profile
- Mac:
open .bash_profile
- WSL:
vim .bash_profile
- Mac:
- Add this line to the bottom of the file
source ~/.bash_prompt
- NOTE: In vim, type
i
to enter INSERT mode and then right click to paster the copied text. Pressesc
to exit INSERT mode. Type:wq
and press enter to save and close the file.
- Create .bash_prompt file
touch .bash_prompt
- Open .bash_prompt file
- Mac:
open .bash_prompt
- Windows:
notepad .bash_prompt
- Mac:
- Add these lines to your file
#!/usr/bin/env bash # GIT FUNCTIONS git_branch() { git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*$ \\(.*\\)/ (\\1)/' } # TERMINAL PROMPT PS1="\[\e[0;93m\]\u\[\e[m\]" # username PS1+=" " # space PS1+="\[\e[0;95m\]\W\[\e[m\]" # current directory PS1+=" " # space PS1+="\[\e[0;92m\]\$(git_branch)\[\e[m\]" # current branch PS1+=" " # space PS1+=">> " # end prompt export PS1; export CLICOLOR=1 export LSCOLORS=ExFxBxDxCxegedabagacad
- Relaunch you terminal and navigate to a git repo (i.e. this python-dev-setup repo). You will now see your terminal prompt formatted with your username, current folder and repo branch
- Install Apple Xcode developer tools
xcode-select --install
- Download Homebrew: https://brew.sh/
- Homebrew helps you easily manage installs on a Mac from command-line
- Install git using homebrew
brew install git
- From PowerShell, open your Ubuntu Linux distribution with
wsl
and run the commandapt install git
- NOTE: If you forgot your password (or were not prompted to set one upon initial WSL install), you can reset your password using the following steps:
- Exit the Linux distribution you are in with the command
exit
- Re-enter the Linux distribution as the root user
wsl -u root
- Enter the command
passwd {your username}
and type in the new password - Exit the Linux Distribution with
exit
- Exit the Linux distribution you are in with the command
- Switch to the super user (a.k.a root user)
sudo su
- Enter your password
- Install git
yum install git
- Switch back to a normal user
exit
In order to commit/ push code to Git you must configure your username and email.
git config --global user.name {your username}
git config --global user.email {your email}
To easily authenticate with Github/ Bitbucket (i.e. not need to enter your username/ password every time you pull/push to the repo) you can set up SSH authentication.
Setting up git authentication on a personal machine (i.e. one that only you will be using) is very simple. However, if multiple user may be using the machine (i.e. a Linux Machine shared by a development team) you will want to follow a few additional step so that commits can be distinguished by user.
- Create an SSH key pair with the command
ssh-keygen
and press enter to accept all the default - Copy the content of the id_rsa.pub file using the
cat {path to your file}
command- For example,
cat ~/.ssh/id_rsa.pub
- For example,
- See the Machine Type Agnostic Steps section for remaining steps
- Create an SSH key pair with the command
ssh-keygen
, adding your git profile to the end of the id_rsa file name instead of accepting all default - Navigate to where the ssh key pair was created using the
cd {path to ssh folder}
command- For example:
cd Users/jillvillany/.ssh
- For example:
- Create the config file
touch config vim config
- NOTE: Git username vairable is case sensitive
- If using Github, add the below to your config file:
Host github-{git username} HostName github.com IdentityFile ~/.ssh/id_rsa_{git username} IdentitiesOnly yes
- If using Github Enterprise (i.e. IBM), add the below to yuur config file:
Host github-{git username} HostName github.ibm.com IdentityFile ~/.ssh/id_rsa_{git username} IdentitiesOnly yes
- If Bitbucket:
Host bitbucket.org-{git username} HostName bitbucket.org User git IdentityFile ~/.ssh/id_rsa_{git username} IdentitiesOnly yes
- NOTE: If you have other users to add, create similar entries below in the file
- Copy the contents of the
id_rsa_{git username}.pub
file using thecat id_rsa_{git username}.pub
command
- Add SSH key in Github or Bitbucket
- Add the SSH key to the SSH agent
ssh-add ~/.ssh/id_rsa_{git username}
- Test your SSH connection
- If on a personal machine:
- Github:
ssh -T [email protected]
- Github Enterprise (i.e. IBM):
ssh -T [email protected]
- Bitbucket:
ssh -T [email protected]
- Github:
- If on a shared machine:
- Github:
ssh -T git@github-{your username}
- Github Enterprise (i.e. IBM):
ssh -T [email protected]{your username}
- Bitbucket:
ssh -T [email protected]{your username}
- Github:
- If on a personal machine:
-
Clone repo with SSH auth
- IMP NOTE: If you are an SSH config file so multiple git users can be on the same machine (i.e. as done in instructions above), you must modify the SSH URL git provides
- For example, in
[email protected]:jillvillany/python-dev-setup.git
replace ".com" with "-{username}" ->git@github-jillvillany:jillvillany/python-dev-setup.git
- For example, in
- On a new repo:
- Select SSH when you clone the code and copy the URL
- Enter the command
git clone {SSH URL}
- For example:
git clone [email protected]:jillvillany/python-dev-setup.git
- For example:
- On an existing git repo (i.e. if had cloned this repo using username and password auth)
- IMP NOTE: If you are an SSH config file so multiple git users can be on the same machine (i.e. as done in instructions above), you must modify the SSH URL git provides
-
Add new/ modified files, commit and push changes
git pull git add {relative file path} git commit -m "commit message" git push
- IMPORTANT: If multiple users are working in the directory, be sure to set the git config user.email before pushing so that commits belong to the user that made the change
git config user.email {your email}
-
Undo all local changes to the checked out branch
git stash
-
Undo local changes to a particular file
git checkout --{file name}
-
Create a New Branch
git checkout -b {branch name} {branch making new branch from} git push -u origin {branch name}
-
Abort a merge
git merge --abort
-
Revert to a previous commit
git revert {commit hash}
- NOTE: See this stack overflow post for more details
-
Archive & Delete a Branch
git tag archive/{branchname} {branchname} git push origin --tags git branch -d {branchname} git push -d origin {branch_name}
- NOTE: If need to delete a created tag
git tag -d
- NOTE: The process of archiving numerous branches can get tedious. See the archive_branch.sh script in this repo for an automated way to archive branches.
-
Restore an archived branch
git checkout -b {branch name} archive/{archived branch name}
-
Maintain a Forked Repo
- Sync your forked version of the repo with the most updated version of the main repo
git checkout master
- If not already done:
git remote add upstream https://github.ibm.com/ML-Pipelines/ml-pipelines.git
git fetch upstream
git rebase upstream/master
git push -u origin
- Sync your forked version of the repo with the most updated version of the main repo
For other useful commands delivered in an entertaining way see: https://ohshitgit.com/
-
Download Vscode: https://code.visualstudio.com/download
-
Install the extensions below:
- Python
- Jupyter
- Prettify JSON
- JSON Viewer
- Markdown Preview Enhanced
- Rainbow CSV
- vscode-pdf
- Remote-SSH
- Remote-WSL (only if using Windows WSL)
- Once you install tyhe Remote-WSL, navigate to the Remote Explorer tab and connect to your WSL distribution
- Navigate to open a folder as you normally would
When project files are hosted on a Linux Machine, you can easily edit/ debug them as you would local files by taking advantage of VS Code's Remote-SSH extension.
To use SSH to connect to a remote Linux machine:
- Open the Command Palette
- Mac:
shift + cmd + P
- Windows:
shift + ctrl + P
- Mac:
- Search for/ Select Remote-SSH: Add New SSH Host...
- Enter the ssh connection command
- Select the ssh file to update
- You will see a pop-up in the bottom right corner that the host has been added
- In the Remote Explorer tab, you will now see your new SSH Target. Click the plus folder icon to "Connect to Host in New Window"
- A new window will open and you will be prompted to enter your password
- Choose the folder in the remote machine you want to open and select "OK"
- You will be prompted for your password one more time
- Upon successful connection, you will see the remote machine's ip as well as the git branch you are currently on (if opened to a git repo) in the bottom left corner
You can now easily edit and debug your code as you would locally. However, there is one exception. If you are connecting to the remote machine with a normal user (i.e. non-root user) you will get access errors when trying to run a ipynb file.
This happens because, in a Unix/Linux system, you can’t bind to external facing ports without super user/root access.
If you do not have an environment controlled with Puppet, you can try editing the user's permissions to not need password authentication when entering root user mode. However, it is easiest to just test small code snippets in a noteboook file locally.
For more information, see https://code.visualstudio.com/docs/remote/ssh
- Install
wget
brew install wget
- Install Mambaforge (Type "yes" wherever asked)
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh" bash Mambaforge-$(uname)-$(uname -m).sh
- Install
wget
sudo apt install wget
- Install Mambaforge (Type "yes" wherever asked)
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh" bash Mambaforge-$(uname)-$(uname -m).sh source ~/.bashrc
- Create new conda python virtual environment
- Note: If it asks you to select y/n, enter "y"
conda create -n {name} python={version}
- Note: If it asks you to select y/n, enter "y"
- Activate conda environment
conda activate {name}
- Deactivate conda environment
conda deactivate
- Install
poetry
with the following command:curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
- Restart your terminal
- Enter
poetry
and you should see a list of commands returned
NOTE: For demo purposes, let's pretend this project is dependent on Python version 3.9.6 and pandas
-
Create a
pyproject.toml
file using the following template (See this repo's pyproject.toml):-
NOTE: We are assuming this repo represents a project dependent on Python version 3.9.6 and a pandas version greater than or equal to 1.4.1
[tool.poetry] name = "{repo/project name}" version = "1.0.0" description = "{free text description}" readme = "README.md" repository = "{git repo link}" documentation = "{site url if applicable}" authors = [ "{full name} <{email}>" ] [tool.poetry.dependencies] python = "=={Python version}" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api"
-
-
Create a conda environment that uses the Python version specified in the
pyproject.toml
file and active the environment -
Run command
poetry install
- Install the requirements in the poetry.lock file
poetry install
- Add a package to the pyproject.toml file and update lock file
poetry add {package name}
- NOTE1: This will install the latest package version. The pyproject.toml will show a version greater than or equal to the version is required and the poetry.lock file will update to specify the specific version installed.
- NOTE2: If you need to install a version other than the latest version, you can specify
poetry add {package name}=={version}
- Remove a package from the pyproject.toml file and update lock file
poetry remove {package name}