andres
andres

Reputation: 131

install virtualenv using pip or pip3?

I used brew to install both python2 and python3

brew install python
brew install python3

I noticed that there are pip and pip3 so which pip should I use to create virtualenv

pip install virtualenv or pip3 install virtualenv

Upvotes: 11

Views: 24712

Answers (4)

Hassan Shahzad Aheer
Hassan Shahzad Aheer

Reputation: 75

Having had the same question in mind and after searching and seeking advice, I've found the following insights:

  • pip: Mainly associated with Python 2.x. It installs packages for the default Python version, which could be Python 2.7 on older systems.
  • pip3: Specifically for Python 3.x. Using pip3 ensures package installation for Python 3, regardless of the default version.

Reasons for separate commands:

  1. Compatibility: Python 2 and 3 differ significantly. Mixing packages can cause errors. Separate commands prevent conflicts.

  2. Clarity: Explicitly using pip3 indicates your intention to work with Python 3, especially when both versions are present.

Therefore, for all newly project we need to run

pip3 install virtualenv

Upvotes: 0

Douglas Denhartog
Douglas Denhartog

Reputation: 2054

Use pip install virtualenv to create a python env and pip3 install virtualenv to install a python3 env

The difference is required because if you use pip install virtualenv and need python3 packages, you will get all kinds of errors!

UPDATE (2020-03-12): With python3 you can also use

python3 -m venv {directory}

where {directory} is the path of/to your virtualenv.

Upvotes: 8

stelios
stelios

Reputation: 2845

pip install virtualenv

This doesn't create any virtual environment. That installs virtualenv program, that is used in order to create virtual environments.

What is the default python version that your virtual environment will have is specified as an argument, when actually making the env, like:

virtualenv -p python3 my_venv 

or

virtualenv -p python2 my_venv

regardless on how the virtualenv package was installed.

Furthermore checkout this

Upvotes: 4

TomKivy
TomKivy

Reputation: 408

Your second question:"how can I know if that virtualenv is the one created by pip or pip3?"

-> You can activate the virtual environment with source bin/activate(first cd in the folder of the environment) When you are sure you are in the virtual environment, type "python --version". You can also check which python is active in the environment by typing "which python". Hope this helps.

Upvotes: 2

Related Questions