How to prepare your Python (2.7) sandbox in your Mac
We will likely encounter version conflicts if we’re working on multiple projects and just using the system Python. It’s a common and best practice to use a sandboxed environment (virtualenv) to prevent conflicting version requirements of packages and for us not to worry about messing up our system’s Python.
What I will be using:
- macOS Catalina (10.15.7)
- Python 2.7
- Terminal
Steps:
1. Open your Terminal.
2. Install pip.
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py | python
3. Install virtualenv using pip — we will create virtual environments under our user profile.
pip install --user virtualenv
4. Create a VirtualEnv (or any name you prefer) folder where we will put our virtual environments for each project.
mkdir VirtualEnv
5. Now, let’s create our first virtual environment and name it project1.
virtualenv VirtualEnv/project1
Upon creation of the virtual environment, it will initially have installed three (3) packages: pip, setuptools, and wheel.
6. To use the virtual environment, we have to activate it.
source VirtualEnv/project1/bin/activate
We now have a usable Python virtual environment. 🤝
7. To exit, all you have to do is deactivate.
deactivate
To show a little more, I created another virtual environment named project2 with more packages installed.
I am running them concurrently, and as you can see, you can install packages in a virtual environment without affecting the other.