Signup/Sign In

Install Python 3.x and Virtual Environment on macOS for Local Development

Posted in Programming   LAST UPDATED: SEPTEMBER 14, 2021

    Every Python developer who uses Mac OS comes across the problem of whether to use the pre-installed python 2.7 on Mac or setup Python 3.x version, to start developing on the latest python version.

    Although Python 2.x is still in use but only to support the already developed products, but if you are starting afresh then I would advice to choose Python 3.x, as sooner or later you will have to move to it, so why not today.

    Updating the Pre-installed Python 2.x in Mac OS

    You cannot do that. As the Mac OS wouldn't allow you to do so because it uses Python and cannot afford a change in it's version as that may lead to errors or failures at the OS level.

    How to install Python 3

    To install Python 3.x we will be using Homebrew. Homebrew is a software package manager which makes it very easy to install, update, cofigure and uninstall software packages required in OS X. If you do not have Homebrew installed on your Mac, follow our Homebrew Installation Guide to set it up on your system. Considering that you have successfully setup Homebrew, let's start with Python 3 installation.

    Step 1:

    As you can see in the Homebrew installation guide, you can search for available software packages using the brew search command.

    $ brew search python

    This command will return a list of all the available packages for python, you will also see python3 and other latest versions on python in the list, and that is what we need.

    So let's install it:

    $ brew install python3

    It may take a few minutes as a lot of packages and dependencies are downloaded, you can see what Homebrew is doing on the screen logs.

    Alongside Python 3, pip, setuptools and wheel will also be installed.

    pip is used to install and manage libraries and packages that we want in our python development environment. We can use pip as follows:

    $pip3 install package_name

    Just like we do for Homebrew, but pip will install only python libraries and software packages within the python environment.

    Step 2:

    To check the version of python installed, use the following command:

    $ python3 --version

    Step 3:

    To update the Python 3 installation, we can use Homebrew,

    $ brew update python3

    How to Create a Virtual Environment

    To setup virtual environment, we can use the venv module provided by Python 3. While in Python 2.x virtualenv is used. So we will be using the venv module.

    Setting up virtual environment allows us to setup independent environment for different projects, with different dependencies. We can setup as many python virtual environment as we want. Let's setup our very own virtual environment.

    Step 1:

    Choose a directory to put Python programming environment in, or create a new directory. I suggest, if you are starting up with python, create a new directory. Go to the directory in which you want to create a new directory for python environment.

    $ mkdir Environments
    
    $ cd Environments

    The mkdir is a command used to create directory and using cd command we can open that directory, and that is exactly what we have done.

    Step 2:

    Once we are inside the directory, run the following command to create an environment:

    $ python3.6 -m venv my_first_env

    This will create a new directory/folder for your virtual environment by the name my_first_env. It will contain it's own copy of python version in lib/ subdirectory along with a subdirectory site-packages/ to store third party modules that you will install in this virtual environment.

    It will also have a pyenv.cfg file with python installation configuration.

    Step 3:

    To start using this new environment, run the following command:

    $ source my_first_env/bin/activate

    As you run this command, you will see a prefix added to your terminal like this,

    (my_first_env) Username:~ username$

    This means that if we run any python script now, it will be run as per the current environment's settings and packages.

    NOTE: Within the virtual environment we can use command python and pip instead of python3 and pip3 respectively.

    Congratualtions! Setup of your virtual environment is complete.

    You may also like:

    About the author:
    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    Tags:PythonHowToMacOSLinux
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS