create account

Installing packages using pip and virtual environments by python-dev

View this thread on: hive.blogpeakd.comecency.com
· @python-dev ·
$0.67
Installing packages using pip and virtual environments
## Installing pip

### Windows 

The Python installers for Windows include pip. You should be able to access pip using:

py -m pip --version
pip 9.0.1 from c:\python36\lib\site-packages (Python 3.6.1)

You can make sure that pip is up-to-date by running:

py -m pip install --upgrade pip

### Linux and macOS
You can also install pip yourself to ensure you have the latest version. It’s recommended to use the system pip to bootstrap a user installation of pip:

python3 -m pip install --user --upgrade pip

Afterwards, you should have the newest pip installed in your user site:

python3 -m pip --version
pip 9.0.1 from $HOME/.local/lib/python3.6/site-packages (python 3.6)

## Installing virtualenv

Note

If you are using Python 3.3 or newer, the venv module is the preferred way to create and manage virtual environments. venv is included in the Python standard library and requires no additional installation. If you are using venv, you may skip this section.

virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.

### On macOS and Linux:

python3 -m pip install --user virtualenv

### On Windows:

py -m pip install --user virtualenv

## Creating a virtual environment

venv (for Python 3) and virtualenv (for Python 2) allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtual environment while developing Python applications.

To create a virtual environment, go to your project’s directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands.

### On macOS and Linux:

python3 -m venv env

### On Windows:

py -m venv env

The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.

venv will create a virtual Python installation in the env folder.

Note

You should exclude your virtual environment directory from your version control system using .gitignore or similar.

## Activating a virtual environment

Before you can start installing or using packages in your virtual environment you’ll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s PATH.

### On macOS and Linux:

source env/bin/activate

### On Windows:

.\env\Scripts\activate

You can confirm you’re in the virtual environment by checking the location of your Python interpreter, it should point to the env directory.

### On macOS and Linux:

which python
.../env/bin/python

### On Windows:

where python
.../env/bin/python.exe

As long as your virtual environment is activated pip will install packages into that specific environment and you’ll be able to import and use packages in your Python application.

## Leaving the virtual environment

If you want to switch projects or otherwise leave your virtual environment, simply run:

deactivate

If you want to re-enter the virtual environment just follow the same instructions above about activating a virtual environment. There’s no need to re-create the virtual environment.
👍  , , , , , , ,
properties (23)
authorpython-dev
permlinkinstalling-packages-using-pip-and-virtual-environments
categorypython-dev
json_metadata{"tags":["python-dev"],"app":"steemit/0.2","format":"markdown"}
created2020-02-25 22:16:24
last_update2020-02-25 22:16:24
depth0
children1
last_payout2020-03-03 22:16:24
cashout_time1969-12-31 23:59:59
total_payout_value0.335 HBD
curator_payout_value0.334 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,545
author_reputation5,457,648,648,750
root_title"Installing packages using pip and virtual environments"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id95,828,749
net_rshares3,314,620,055,329
author_curate_reward""
vote details (8)
@mytechtrail ·
The simplest virtual environment setup and use description I have seen. Makes it understandable and easy to use.
properties (22)
authormytechtrail
permlinkre-python-dev-q6ai5q
categorypython-dev
json_metadata{"tags":["python-dev"],"app":"steempeak/2020.02.4"}
created2020-02-26 03:39:54
last_update2020-02-26 03:39:54
depth1
children0
last_payout2020-03-04 03:39:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length112
author_reputation19,007,403,368,902
root_title"Installing packages using pip and virtual environments"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id95,834,462
net_rshares0