WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Intermediate · question 29 of 100

How do you create and manage virtual environments in Python? Explain the use of venv and pip.?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Virtual environments are a way to create isolated environments for Python projects, which allows you to manage dependencies and avoid conflicts between different projects. In Python, virtual environments can be created and managed using the venv module and pip package manager.

Here’s how to create a virtual environment using venv:

Open a command prompt or terminal and navigate to the directory where you want to create the virtual environment. Run the command python -m venv env to create a new virtual environment called env. This will create a new directory called env in your current directory, containing the virtual environment files. Activate the virtual environment by running the command source env/bin/activate on Linux or macOS, or env
Scripts
activate on Windows.

Once you have activated the virtual environment, you can install packages using pip. Here’s how to install a package in the virtual environment:

Activate the virtual environment by running the command source env/bin/activate on Linux or macOS, or env
Scripts
activate on Windows. Run the command pip install package-name to install the package.

Here’s an example of how to create and use a virtual environment for a project:

Create a new directory for your project and navigate to it in the command prompt or terminal. Create a new virtual environment called env by running the command python -m venv env. Activate the virtual environment by running the command source env/bin/activate on Linux or macOS, or env
Scripts
activate on Windows. Install the packages you need for your project using pip, for example pip install numpy pandas matplotlib. Develop your project as normal, using the packages installed in the virtual environment. When you’re done working on the project, deactivate the virtual environment by running the command deactivate.

In summary, virtual environments in Python allow you to create isolated environments for your projects, which allows you to manage dependencies and avoid conflicts between different projects. You can create and manage virtual environments using the venv module and pip package manager.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics