Creating and managing virtual environments and installing dependencies with pip, each explained with a short example.
30 cards · basic cards · AI-written, checked twice. Edit anything.
- What is a Python virtual environment?
Show answer
An isolated Python installation on your system that has its own set of installed packages and Python version, separate from the system Python.
- Why should you use a virtual environment?
Show answer
To avoid package version conflicts between projects. Each project can have different versions of the same package without interfering with each other.
- What is pip?
Show answer
Python's package installer. It downloads and installs Python packages from the Python Package Index (PyPI).
- How do you create a virtual environment named 'myenv' using venv?
Show answer
python -m venv myenv
- How do you activate a virtual environment on macOS or Linux?
Show answer
source myenv/bin/activate
- How do you activate a virtual environment on Windows?
Show answer
myenv\Scripts\activate
- How do you deactivate a virtual environment?
Show answer
deactivate
- What is the difference between venv and virtualenv?
Show answer
venv is Python's built-in module (Python 3.3+), while virtualenv is a third-party package that works on older Python versions and has more features.
- How do you install a package called 'requests' using pip?
Show answer
pip install requests
- How do you list all packages installed in your virtual environment?
Show answer
pip list
- How do you create a requirements.txt file from your current environment?
Show answer
pip freeze > requirements.txt
- How do you install all packages listed in a requirements.txt file?
Show answer
pip install -r requirements.txt
- How do you upgrade an installed package to the latest version?
Show answer
pip install --upgrade package_name
- How do you uninstall a package?
Show answer
pip uninstall package_name
- How do you check the version of an installed package?
Show answer
pip show package_name