Effortless Python Development on MacOS: A Best Practices For Setting Up Your Environment with PyCharmEffortless Python Development on MacOS: A Best Practices For Setting Up Your Environment with PyCharm
Updated at Nov 19, 2023
14 Views

1. Install pyenv on MacOS

The official documentation for Pyenv provides comprehensive guidance. We chose to install Pyenv to streamline the management of Python versions effortlessly. Notably, the ability to transition to a new Python version is made remarkably simple through just a few straightforward commands.

Use Homebrew to install Pyenv. (Learn more about Homebrew.)

brew install pyenv

Configure Pyenv with Zsh. (Refer to this resource for instructions on installing Zsh on MacOS.)

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

2. Pyenv Commands

Prior to installing Python on MacOS, I’ll first check the Status of Python versions to identify the initial bug-fix release. In the example provided below, I’ll be using version 3.11 as my current Python version.

Check all installed versions.

pyenv versions

Check all available versions.

pyenv install -l

To install version 3.11, use the following command to check for the latest available version.

pyenv install -l | grep '3.11'

Here are the outputs. I exclusively utilize the original Python versions, those without any prefixes. Based on the displayed output, I will proceed to install version 3.11.6 following the instructions provided below.

  3.11.0
  3.11-dev
  3.11.1
  3.11.2
  3.11.3
  3.11.4
  3.11.5
  3.11.6
  miniconda3-3.11-23.5.0-3
  miniconda3-3.11-23.5.1-0
  miniconda3-3.11-23.5.2-0
  miniconda3-3.11-23.9.0-0
  miniconda3-4.3.11
  pypy2.7-7.3.11-src
  pypy2.7-7.3.11
  pypy3.8-7.3.11-src
  pypy3.8-7.3.11
  pypy3.9-7.3.11-src
  pypy3.9-7.3.11

Install Python version 3.11.6 by replacing <INSTALLED_VERSION> with 3.11.6.

pyenv install <INSTALLED_VERSION>
pyenv install 3.11.6

Set Python version 3.11.6 as the global version by replacing <INSTALLED_VERSION> with 3.11.6.

pyenv global <INSTALLED_VERSION>
pyenv global 3.11.6

Use the following command to check the current Python version.

python --version

2. Install PyCharm

Select either the Professional or Community version, do not install both.

Install PyCharm Professional. (Requires a valid license, check here for more information)

brew install --cask pycharm

Install PyCharm Community version. (Free)

brew install --cask pycharm-ce

3. Setup pyenv in PyCharm

Execute the command pyenv versions to locate the path. The path is “/Users/tinghsuan/.pyenv/version” here. We will need this information in the subsequent steps.

Open PyCharm, and add local interpreter.

Put the path here.

Select the python executable file in “<Installed_Version>/bin/python<Installed_Version>”.

After selecting the local interpreter, click OK to complete the installation, and you can now use Python in PyCharm.

4. Setup Virtualenv in PyCharm

Virtualenv assists in isolating packages. When installing packages across various projects, conflicts may arise. By setting up a virtualenv, we can install packages on a per-project basis, mitigating potential conflicts.

By selecting “Python 3.11,” as shown in the following image, you can choose “Add New Interpreter” to create a Virtualenv specifically for this project.

You can check the box for “Inherit global site-packages” if you wish to utilize global packages without additional installation in your virtual environment. Alternatively, you can stick with the default settings and click OK.

After configuring the virtual environment (venv), you will notice the venv folder appearing in the project. If you later decide to switch back to the global Python, you can select the “Python Interpreter” at the bottom right and make the necessary environment switch.

5. Use venv in command

Activate venv.

source venv/bin/activate

Deactivate venv.

deactivate

Reference

  1. Status of Python Versions