How to Install Python and Set Up a Virtual Environment

How to Install Python and Set Up a Virtual Environment

Python, a high-level, interpreted programming language, is gaining immense popularity in the field of programming. It's easy to read syntax and dynamic semantics makes it an ideal language for scripting and rapid application development in many areas. If you are new to Python and wondering how to install it and set it up, then you've landed in the right place. Through this guide, you'll learn about installing Python and setting up a virtual environment.

Before starting with the Python installation, it's essential to know Python demands an operating system to run. It's compatible with various operating systems like Windows, Mac, and Linux.

Installing Python


Go to the official website of Python, www.python.org, and navigate to the downloads tab. Here, you will see the latest versions of Python available for different platforms. Choose as per the operating system you are using. Once you download the executable file, follow these steps-

For Windows:

1. Run the downloaded executable (.exe) file.

2. On the first Python setup window, you can see the Python version and architecture. Keep the default settings, check the box at the bottom that says ‘Add Python3.x to Path,’ and see that the install launcher for ‘All Users’ is selected. Click 'Install Now.'

3. The installation would start, and within a few minutes, Python will be installed on your system. You can also observe that it has chosen the path of installation.

For macOS:

1. Open the downloaded python .dmg file.

2. A new finder window would appear with the Python logo, a link to Applications folder, and a package file Python.mpkg. Double click on the package file. It will open Python’s installation wizard.

3. In the wizard window, click on ‘Continue’ to begin the installation and follow through with the process.

For Linux/Unix

Python comes pre-installed with most Linux distributions. However, you may need to upgrade it to the latest version. Use the terminal and follow these commands for Ubuntu-based distributions.

1. sudo apt-get update

2. sudo apt-get install python3.6

Setting Up a Virtual Environment


A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python environments for them.

You can create a virtual environment using venv module in Python. The syntax is: python3 -m venv /path/to/new/virtual/environment

Navigate to the directory where you want to create the virtual environment and run above command. Replace the path with what you want to name your virtual environment.

Activating the Virtual Environment

After your virtual environment is set up, you need to activate it each time you want to use it.

For Windows:

If your virtual environment name is 'myenv,' you'd type the below line in command prompt.

myenv\Scripts\activate

For macOS/Linux:

source myenv/bin/activate

Once the virtual environment is activated, it would appear in the beginning in the terminal.

Python is no doubt, an excellent platform for creating and managing large scale projects. It simplifies many complex tasks and provides an easy-to-use interface for programmers. Now that you have installed Python and created a virtual environment, it's time to roll up your sleeves and start coding.