What is a Virtual Environment?
- It’s a place on your system where you can install packages and isolate them from all other Python packages.
- This is useful for testing and development, as it allows you to create a clean environment without worrying about package conflicts or dependencies.
How Can I Create a Virtual Environment?
- To create a virtual environment, you can use the venv module that comes with Python by issuing the following command in an IDE terminal:
python
1python -m venv name_of_your_env- Note: If you’re using a Mac, you may need to use python3.
- This will create a new directory called name_of_your_env in your current working directory, which will contain the Python interpreter and any packages you install.
- The -m flag stands for module.
How Can I Activate a Virtual Environment (Windows Users)?
- To activate a virtual environment on Windows, you can issue the following command in the IDE terminal:
python
1name_of_your_env\Scripts\activate- When the environment is activated, you’ll see the name of the environment in parentheses, inside the terminal window.
- Then, you can install packages to the environment and use these packages in your code. Packages you install in name_of_your_env (e.g., pandas, plotly, and django) will be available only while the environment is active.
How Can I Activate a Virtual Environment (Mac Users)?
- To activate a virtual environment on a Mac, you can issue the following command in the IDE terminal:
python
1source name_of_your_env/bin/activateHow Can I Deactivate a Virtual Environment?
- To stop using a virtual environment, you can issue the command, deactivate:
python
1deactivate- The environment will also become inactive if you close the terminal it’s running in.