How to use virtualenv with Python? [closed]
Asked Answered
C

5

22

I am planning to install a virtual environment for Python in order to keep my Python packages separate. One of the motivations for this is also to have two versions of Python on my machine (Ubuntu 14.04) co-existing. I have the following wonders:

  1. In what order should Python, PIP and virtualenv be installed? Does it matter at all?
  2. Once done, how can I keep two python versions separate under virtualenv?
  3. Assume I am working on separate projects, is it recommended to keep each of the project in a separate folder created by virtualenv or not?

I would like to know experts opinion in order to do things in the right manner and wisely.

Charmainecharmane answered 26/1, 2016 at 15:13 Comment(2)
any, no, see below, yes.Bondmaid
Possible duplicate of Use different Python version with virtualenvBondmaid
A
10

Using virtualenv is common amongst Python programmers. These links will be more useful than my answers:

  • http://docs.python-guide.org/en/latest/dev/virtualenvs/
  • http://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/

    1. Yes, it does matter. Pip uses Python, but since Ubuntu comes pre-installed with a version of Python (In your case both 2 and 3 are installed), you shouldn't have to worry about this. But the order would be Python -> PIP -> virtualenv.

    2. Once you cd in a new, empty project folder, you can create the virtualenv with the Python version of your choice with virtualenv -p /path/to/python/version venv. You can find the path with which python2 or which python3.

    3. If I understand your question correctly - yes. The whole point of virtualenv is to keep each project in a separate folder with its own virtualenv set up. Even with a small project, you will just become more familiar with the concept of virtualenv (and maybe even containers like Docker).

Atrabilious answered 26/1, 2016 at 15:42 Comment(0)
W
22
  1. You install it in that order, see instructions below (there alternatives though). You can install virtualenv with APT before pip, but since you install pip, you don't need to.
  2. I don't really now, but I found this other thread that may help you: How to use pip3 with python 3.4?
  3. It is strongly recommended, dependencies may not be the same and it will help you keep things clean ( regarding version control for instance)

Now, how to install Python For Ubuntu 14.04 you will have python2.7 and python3 already installed, with "python" being an alias to python2.7 by default.

Pip you can install with :

sudo apt-get install python-pip python3-pip

I don't know how pip for py2 and pip for py3 coexist but they are available as separated packages.

VirtualEnv You can use pip to install virtualenv:

pip install virtualenv

Here I'm using pip for python2


Once a have all set I do the following:

mkdir -p project_name/source
cd project_name
virtualenv env

I usually keep source and env names constant in every project because I have some hooks around but I recommend you to replace the names, specially "env" because it's the key to know in which VirtualEnv you are working, since you will get something like this:

(env)yser@machine:/home/user/cool_projects/project_name$

I've also keep env out of source to simplify things with version control (no need for marking it for ignoring) but that is just me.

To activate virtualenv:

cd project_name
source env/bin/activate

Now you can pip install inside the VirtualEnv. To change projects exit your current VirtualEnv with:

deactivate

Hope it helps!

Wien answered 26/1, 2016 at 15:42 Comment(0)
A
10

Using virtualenv is common amongst Python programmers. These links will be more useful than my answers:

  • http://docs.python-guide.org/en/latest/dev/virtualenvs/
  • http://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/

    1. Yes, it does matter. Pip uses Python, but since Ubuntu comes pre-installed with a version of Python (In your case both 2 and 3 are installed), you shouldn't have to worry about this. But the order would be Python -> PIP -> virtualenv.

    2. Once you cd in a new, empty project folder, you can create the virtualenv with the Python version of your choice with virtualenv -p /path/to/python/version venv. You can find the path with which python2 or which python3.

    3. If I understand your question correctly - yes. The whole point of virtualenv is to keep each project in a separate folder with its own virtualenv set up. Even with a small project, you will just become more familiar with the concept of virtualenv (and maybe even containers like Docker).

Atrabilious answered 26/1, 2016 at 15:42 Comment(0)
F
9

yes it's better to use for each python project its virtualenv

1- to create python virtualenv in venv folder use:

>>> cd [your project path]
>>> virtualenv venv

2- you can active your environment by :

>>> source ./venv/bin/activate

3- install your requirements packages with pip :

>>> pip install -r <your requirements file>
>>> or pip install <python module>

you can also install your requirements modules without activate the environment

>>> ./venv/bin/pip install <python module>

4- to run your python script use :

>>> python <.py file>

and if you didn't activate your env use :

 >>> ./venv/bin/python <.py file>

if you want to manage you python env you have virtual wrapper

Falchion answered 26/1, 2016 at 15:46 Comment(2)
You have source ./venv/bin/active but i think it should just be source venv/bin/activeUsquebaugh
should be source ./venv/bin/activate not source ./venv/bin/activeCinderellacindi
Z
3

I suggest using pyenv. It sits on top of virtualenv and provides an easy way to install different Pythons (cpython, jpython, anaconda, ...) each with its own virtualenvs. Your system's Python is available with an alias system.

Zaragoza answered 26/1, 2016 at 15:20 Comment(3)
Thanks for the link and information njzk2. Here is what I have done up to now: 1. Installed Pip. 2. Installed virtualenv 3. To be sure how it is working tried making a new virtualenv using: virtualenv project1_env. What I notice is that under bin and lib folders I seee python version 2.7. Does it mean I can have only version 2.7 and if I want to use python ver 3 how can I handle it please? The next question I have is where in this folder, I need to save my python scripts? Put it differently, is 'project1_env' where I need to save my project files? Thanks for your hints in advanceCharmainecharmane
with pyenv, the suggested course of this answer, you can install whatever versions of python you please. You can choose what the default version to run is. But on top of that, you can create local versions. Meaning you can navigate to your project directory and set the local version there to be whatever version you wish.Geri
@Charmainecharmane Finally added the link to pyenv. James described it pretty well.Trapp
H
2

In what order should Python, PIP and virtualenv be installed? Does it matter at all?

1. Install python
2. Install pip
3. Install virtualenv (with `pip install virtualenv`)
4. Create new virtualenv

How can I keep two python versions separate under virtualenv?

Create separate python environments for different python versions with command:

    virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/>

Assume I am working on separate projects, is it recommended to keep each of the project in a separate folder created by virtualenv or not?

It's not recommended. However you could keep that projects under one virtualenv until versions of python packages installed for your project not conflicting between each other

Read Virtual Environment paragraph from The Hitchhiker’s Guide to Python! it would answer a lot of your questions!

Also you can take a look on How do I install python 2.7.2 on Ubuntu? and to How to Install Pip on Ubuntu for reference of how to install...

Hypochondria answered 26/1, 2016 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.