How to Create a Virtual Environment in Python
I am writing this mainly for personal reference after running into a Homebrew/Python/Path conflict on the first week of a new role. I’m not a computer scientist, and will probably not be in this lifetime, but the best practices for setting up Python for Data Science are still staggeringly confusing. Consequences of incorrect setups can range from “I literally can’t run Pandas on a Jupyter Notebook even though I installed it” to (IMO the much more frustrating) “I’ve been working successfully with this setup for a few year but can’t install a specific package because I have three different sources of Python installed on my computer and need to spend an entire Friday searching StackOverflow with different permutations of words to figure out a solution. Also, nobody on my team can help me because we all have different setups of Python on our computers.”
This setup is intended to get a Data Scientist up and running on a new MacBook to do Jupyter Notebook data exploration with Python. My setup is a MacBook Pro 2021, Apple M1 Pro, macOS Monterey 12.1
Install Homebrew (Optional)
Homebrew can be a useful way to install programs on a Mac, but do not install Python via Homebrew - it can potentially delete virtual environments and will make life harder for you then it needs to be.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/evanagovino/.zshrc
eval "$(/opt/homebrew/bin/brew shellenv)"
I can run ‘brew help’ to confirm that I’ve successfully installed Homebrew and added it to my local path.
Install PyEnv and Add It to My Path
curl https://pyenv.run | bash
path+=('/Users/evanagovino/.pyenv/bin')
export PATH
Now I should be able to run “pyenv” in my terminal.
echo ‘eval "$(pyenv init --path)"‘ >> /Users/evanagovino/.zshrc
echo ‘eval "$(pyenv init -)"‘ >> /Users/evanagovino/.zshrc
echo ‘eval "$(pyenv virtualenv-init -)"‘ >> /Users/evanagovino/.zshrc
I need to also add the above commands to my .zshrc file so that I can set up a virtual environment when needed.
Install Python via PyEnv
pyenv install —list
pyenv install 3.8.13
The first command lists the available versions of Python. Note that for PyEnv for MacOS 11/12, 3.7.13, 3.8.13, 3.9.11 and 3.10.3 are confirmed to be supported and other versions may not be supported.
pyenv global 3.8.13
The global command will set the listed version of Python as the default in a new PyEnv environment.
Create and Activate a Virtual Environment
pyenv virtualenv test
pyenv activate test
The new environment should now be activated. To confirm that your virtual environment is active, you can run:
pyenv which python
which for me returns:
/Users/evanagovino/.pyenv/versions/test/bin/python
Setup Jupyter Notebook in Virtual Environment
Now that your virtual environment is activated, you can install Jupyter Notebook (and anything else) with pip:
pip install ipykernel
If Jupyter Notebook is already installed on your machine (via Homebrew or elsewhere), you need to do the following:
mkdir /Users/evanagovino/Library/Jupyter/kernels/test
And add a new file named kernel.json in that directory with:
{
"argv": [
"/Users/evanagovino/.pyenv/versions/newnew/bin/python",
"-m", "ipykernel_launcher",
"-f", "{connection_file}"
],
"display_name": "test",
"language": "python"
}
Now type:
jupyter notebook
into the terminal and you’ll see that you can now create a notebook in your new environment.
Install Packages Directly from Jupyter Notebook
Adding this because I was previously unaware! You can install packages directly from a Jupyter notebook with the sys.executable command.