• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

[Step-by-step] How to Create Virtual Environment in Python 3?

Aniruddha Chaudhari/24195/0
CodePython

What is Virtual Environment in Python? Why is it needed? What are the Python commands to create and activate Virtual Environment?

I’m explaining it here. At the end of this tutorial, you will get answers to all your questions related to the virtual environment in Python 3.

Why is Virtual Environment Required?

We work on multiple projects. Every project does not require the same environment. We don’t need all the Python libraries installed on our system for all our projects.

The virtual environment allows us to create a separate virtual environment. You can only install selected Python modules you want in your virtual environment. This makes your environment more clean and simple.

Like, if you are working on two projects- one is the Django web framework project and another is Data Science project. Obvious we don’t need the same environment and libraries/modules for both projects. We need a Django module for the first project whereas the Data Science project does not need that module. Numpy, Pandas are the most useful Python libraries for data science which is not required in the Django project.

Now, it is clear. You need a virtual environment before starting your new project.

Table of Contents

  • Virtual Environment in Python 3 [Commands]
    • Install virtualenv Python Module Using the pip tool
    • Create Virtual Environment using venv Command
    • Activate Virtual Environment
    • Check All Modules Installed in Virtual Environment
    • Deactivate Virtual Environment

Virtual Environment in Python 3 [Commands]

We are using a Python module called virtualenv for creating a virtual environment.

Following commands works with most of the widely used OS like Windows, Linux and macOS.

Install virtualenv Python Module Using the pip tool

The virtual environment feature does not come with the default with any of the Python versions like Python2 and Python3. You have to install an external Python module from the PyPi repository for creating a virtual environment.

Run the following command to install the virtual environment module on your system:

pip install virtualenv

Note: You need an internet connection to download and install the Python module. The above command will not work if you don’t have an internet connection.

It will prompt you a success message after installation.

Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/62/77/
6a86ef945ad39aae34aed4cc1ae4a2f941b9870917a974ed7c5b6f137188/
virtualenv-16.7.8-py2.py3-none-any.whl (3.4MB)
| 3.4MB 1.3MB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.8

By default, it will install the latest version of the Python module virtualenv. If you want to install a specific version of the Python module, you can specify that in the pip install command. You can learn more about it in the managing Python module tutorial.

You are all set to create your first virtual environment.

Create Virtual Environment using venv Command

Let’s say, you are creating a virtual environment for your new project called toolAlpha-django. You can give any valid name to your virtual environment.

py -m venv toolAlpha-django

It will create a folder with the name toolAlpha-django in your current directory path.

Your first virtual environment has been created. You have to activate this environment before working on your new project.

Activate Virtual Environment

Activate this virtual environment before installing any Python modules or before using it in your project.

.\toolAlpha-django\Scripts\activate

Just to make sure, check the Python environment if it is set properly or not using where python command for Windows. On Linux, you can check it with the command which python.

(toolAlpha-django) F:\~~\toolAlpha-django>where python

You can see the first path from the virtual environment that we have activated just now.

F:\~~\Scripts\python.exe
C:\~~\Python\Python38-32\python.exe
C:\~~\WindowsApps\python.exe

Your virtual environment is activated. You are free to use it. Start with installing important Python modules suitable for your project need.

You can always check all the installed Python modules in your project environment.

Check All Modules Installed in Virtual Environment

You can check all the modules you have installed in your virtual environment using the pip tool.

(toolAlpha-django) F:\~~\toolAlpha-django>pip freeze

The command pip freeze will list out all the modules present in your virtual environment.

Deactivate Virtual Environment

Before leaving the project or switching to another project virtual environment, you can deactivate your current virtual environment.

(toolAlpha-django) F:\~~\toolAlpha-django>deactivate

These are the simple commands you can use to create and use a virtual environment for your Python project.

These commands for creating a virtual environment in Python 3 are tested for Python 3 in the Windows environment. These commands also work in the Linux environment like Ubuntu, RedHat…

Happy Pythoning!

Python Interview Questions eBook

Python
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard