Thank you for the warning! It is indeed good practice to use a virtual environment when using pip. Virtual environments provide a way to isolate Python environments and avoid conflicts with the system package manager.
To create a virtual environment, follow these steps:
-
Install the virtualenv package if you haven't already:
$ pip install virtualenv
-
Navigate to the directory where you want to create the virtual environment.
-
Create the virtual environment:
$ virtualenv env
This command creates a directory named
env
containing the isolated Python environment. -
Activate the virtual environment:
-
On Unix or Linux:
$ source env/bin/activate
-
On Windows:
$ .\env\Scripts\activate
After activation, your terminal prompt will change to indicate that you are working within the virtual environment.
-
-
Install packages using pip:
$ pip install package_name
-
To exit the virtual environment, use the
deactivate
command:$ deactivate
By using a virtual environment, you can ensure that the installation and management of Python packages are contained within the environment, avoiding system-level conflicts.