7 Practical Tips To Get You Started Correctly With VS Code (MacOS)

Jaelin Lee
6 min readMar 13, 2023

--

For about 2 years, I did all my Machine Learning / Data Science (ML/DS) work only on notebooks. Notebooks were great becasue I didn’t need to worry about installing Python or setting up environments on my local machine. I felt that Kaggle notebook was the easiest way to get started with EDA and ML Model building. Google Colab was the second eaisest as linking to the Google Drive seemed like an added complexity to the beginner’s eyes. Jupyter notebooks were fine, but the shortcut key storkes were more intuitive to me in Kaggle and Colab.

However, as time goes by, I kept hearing from ML/DS experts that if you are working professionally, you will need to learn to use Visual Studio Code (VS Code) or alike. And, notebooks are only suited for learning and development purposes, but not for production.

So, about 4 months ago, I set out my mission to figure this thing out. I completed 2 projects that were fully on VS Code with Git tracking where multiple ML Engineers were involved. After some struggle, I picked up some best practices along the way. Here, I am sharing my hand-picked 7 Practical Tips To Get You Started Coding With VS Code.

Assumptions:

  • You have already installed VS Code on your computer.
  • You have already installed Python on your computer.
  • You have already installed Conda or Miniconda on your computer

Topics Covered:

  • Terminal
  • Package Manager
  • Virtual Environment
  • Requirements.txt
  • VS Code Interpreter
  • Connect to GitHub
Move from notebooks to VS Code and GitHub

1. Terminal — How do I open VS Code from terminal?

This is useful when you’re working on a project and need to quickly open it in VS Code.

  • Launch VS Code
  • ⌘⇧P — Select <Install ‘code’ command in PATH>
  • Type “ code .” in Terminal to open the current folder in VS Code.
To enable using ‘code .’ command to open VS Code
# Navigate to your project directory in Terminal
# Type below to open your project directory in VS Code
code .

2. Package Manager — Which package manager do I use?

These are the package managers that are commonly used in macOS.

brew install is a package manager for macOS that installs command-line tools and other software that is not available through the macOS App Store.

brew install python

pip install is used to manage Python packages and dependencies. It requires the use of virtual environments to achieve environment isolation.

pip install numpy

conda install can easily create isolated environments that contain specific packages and their dependencies.

conda create --name myenv
conda activate myenv
conda install numpy
conda deactivate

# check for installed conda environement
conda info --envs
# install packages from reuqirements.txt
pip install -r requirements.txt
# check installed packages
conda list

apt-get is a Linux command for package management and is not used as the default package manager for macOS. If you see a code with ‘apt-get’, look for ‘brew install’ alternatives.

3. Virtual Environment — How do I create a virtual environment with a specific Python version?

This is important because different projects may require different versions of Python or different packages. Creating a virtual environment allows you to isolate these dependencies so that they don’t interfere with other projects on your system.

  • Upgrade pip
  • Install Xcode command line tool (for macOS)
  • Install pyenv, Python 3.8.16
pip install --upgrade pip
xcode-select --install
brew install xz
brew install pyenv
pyenv install 3.8
  • Update PATH environment variable to configure the system to use pyenv installed Python version,
PATH=$(pyenv root)/shims:$PATH
  • Now, we successfully associated Python 3.8.16 with our project.
which python
python --version

# If correctly done, it will return something like this
> /Users/jaelinlee/.pyenv/shims/python
> 3.8.16
  • Next, we can create a virtual environment called ‘billenv’ with Python 3.8.16 and activate it.
python3.8 -m venv billenv
source billenv/bin/activate

# Deactivating is easy
deactivate

4. Requirements.txt — How do I install dependencies from requirements.txt without package conflict?

If error occurs due to package conflict, while installing the dependencies from requirements.txt, remove version info. If conflict occurs, my rule of thumb is to specify the versions of core packages as priority. I comment out the versions of other packages and let the system decide the versions. For example:

Specify version for core packages:

  • opencv-python==4.7.0.68

Do not specify version for non-core packages (Comment out the “==xx.xx.xx” part if conflict occurs):

  • matplotlib #==3.6.3
  • matplotlib-inline #==0.1.6
  • pandas #==1.5.2
  • numpy #==1.24.0
pip3 install -r requirements.txt

This helps avoid conflicts between packages and ensures that your project runs smoothly.

5. VS Code Interpreter — Why does VS Code tell me ‘can’t find xxx package” when I already installed it on my virtual environment?

This is important because if you don’t select the correct interpreter, VS Code may not be able to find the packages that you installed in your virtual environment. By properly installing the virtual environment in your project and selecting the interpreter, you can avoid this issue.

First, you need to activate the virtual environment in VS Code Terminal. Then, you also need to select VS Code interpreter. Here again, our virtual environment is named ‘billenv’:

  • VS Code — Terminal — (source billenv/bin/activate)
  • VS Code — Select interpreter — (./billenv/bin/python)

For interpreter to be selected automatically in VS Code, you need to properly install the virtual environment in your project. You shouldn’t just copy a virtual environment folder from another project. Create a new virtual environment from scratch. And install dependencies using requirements.txt file (as mentioned above in #3,4).

My virtual environment is named ‘billenv’ (left). 3 locations to check to activate the virtual environment (right)

6. GitHub (clone to local) — How to clone GitHub repository to your local computer:

This is important if you want to work on a project that’s stored on GitHub, or if you want to contribute to an open-source project.

  • Clone GitHub repository by copy-paste GitHub CLI to your Terminal.
gh repo clone <xxx>
Copy GitHub CLI command from GitHub repository

7. GitHub (link to remote)— I already have project files on my computer. I want to upload it to GitHub. How do I set this up?

This is important if you want to share your project with others or if you want to back up your project on GitHub. By properly setting up a remote repository, you can easily push changes to GitHub and keep your project up-to-date.

  • Check remote origin address to see if your local project directory is already linked to a GitHub repository (git remote -v).
  • If not linked, initialze git. And, add HTTPS address of the GitHub repo that you want to link to your local project directory.
git remote -v
git init
git remote add origin https://github.com/<xxx/xxx.git>
Copy the HTTPS address from GitHub repository
  • Pull from main branch
  • Created current branch name is ‘master’
  • Upload your local files to ‘master’ branch
# Check current branch name (mine says 'master')
git branch

# Upload local files to current branch ('master')
git pull origin main
git add .
git commit -m "initial commit from local"
git push --set-upstream origin master

# Continue working on this master branch
  • Or, work on another branch
  • Pull from a branch (i.e. ‘dev’)
  • Start working on a branch by checking out
git pull origin dev
git checkout dev
Photo by Thai Nguyen on Unsplash

Conclusion:

We’ve leanred 7 practical tips for getting started with Visual Studio Code (VS Code) on MacOS.

These are the key things that I wish I knew when I first attempted to use VS Code. I would have been less scared and confused. Hope this helps you as well to finally try out VS Code and move out of notebook-only workflow for your ML/DS projects. This will be a stepping stone for you to further explore deploying your ML models to Flask, Docker, and productionalize your code.

--

--