Published on

Set up python development environment

Authors
  • avatar
    Name
    Jinjiu Liu
    Twitter

Content

Today I mainly set up the development environment on my mac, so this blog will explain how to set up multiple python with different versions and some simple git set up.

Multi Python Management by pyenv

For the context, I use zsh for the terminal and deploy multiple versions of python3 for software development.

  1. Install pyenv by brew

    brew install pyenv
    
  2. Install different python versions by pyenv

    pyenv versions                # see what python pyenv installs, for me, it 
                                # doesn't see the ones installed by brew
    pyenv install 3.10.19         # install the versions you need
    pyenv install 3.12.12         # install the versions you need
    pyenv global 3.12.12          # set default for your user
    
    cd directory_of_your_project  # go to your project using specific version
    pyenv local 3.10.19           # in a project dir, pins that version (writes .python-version)
    python -V                     # the version will be 3.10.19 when you are under this folder
    
  3. Make sure your pyenv is initialized, for example, if you see different python versions of python and python3 after setting the local version

    In ~/.zprofile (for PATH at login) add:

    eval "$(pyenv init --path)"
    

    In ~/.zshrc (for interactive shells) add:

    eval "$(pyenv init -)"
    

    Then do exec zsh

Git worktree set up

Git worktree is an amazing tool to maintain multiple versions of your project. For example, I have two versions to maintain for my project, master and 1.0.

  1. In my current repo, which the head is master, get the branches
cd ~/code/myproject
git status     # make sure it's clean
git fetch --all --prune
  1. Create a 1.0 worktree folder
  • If you already have 1.0 in the remote
    git worktree add ../myproject-1.0 origin/1.0
    
    or
    git worktree add ../myproject-1.0 1.0_branch_name
    
  • If you want to create a new branch 1.0 from master:
    git worktree add -b 1.0 ../myproject-1.0 master
    # (or pick another starting point: a tag, commit, or origin/master)
    

Now when you go to the folder myproject-1.0, you branch 1.0 is automatically checked out!