Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to use virtualenv with Vagrant?

Tags:

I was used VirtualBox manual setups with virtualenvs inside them to run Django projects on my local machine. Recently I discovered Vagrant and decided to switch to it, because it seems very easy and useful.
But I can not figure - do I need still use virtualenv Vagrant VM, is it encouraged practice or forbidden?

like image 789
Gill Bates Avatar asked Aug 16 '13 10:08

Gill Bates


People also ask

Is virtualenv necessary?

There are also workflow tools that simplify this process, such as Pipenv and Poetry. They create virtual environments for you without perception and then install dependencies into them. They are used by a wide range of users.

Should I use virtualenv or VENV?

These are almost completely interchangeable, the difference being that virtualenv supports older python versions and has a few more minor unique features, while venv is in the standard library.

Why do we use virtualenv?

virtualenv allows you to avoid installing Python packages globally by making an isolated python environment. That means it will install packages just in your desire project folder.

How do I create a virtual environment in vagrant?

The simplest way to do this is via Port Forwarding, to connect and relay the data to be sent from a port of our host machine to a port of our virtual machine. Then, to take this new setting use vagrant up (or vagrant reload if the vm vagrant is already picked up).


2 Answers

As Devin stated, it is not necessary to use virtualenv when you deploy to a vagrant machine as long as you are the sole user of the machine. However, I would still enable the use of a virtualenv, setup.py, etc. even if you do not use it for development or deployment.

In my (not so) humble opinion, any Python project should:

  1. Include a .cvsignore, .gitignore, .hgignore, ... file that ignores the common Python intermediate files as well as virtualenv directories.
  2. A requirements.txt file that lists the required packages in a pip-compliant format
  3. Include a Makefile with the following targets:

    • environment: create the virtual environment using virtualenv or pyvenv
    • requirements: install required packages using pip and the requirements.txt file
    • develop: run setup.py develop using the virtual environment
    • test: run setup.py test
    • clean: remove intermediate files, coverage reports, etc.
    • maintainer-clean: remove the virtual environment

    The idea is to keep the Makefile as simple as possible. The dependencies should be set up so that you can clone the repository (or extract the source tarball) and run make test. It should create a virtual environment, install the requirements, and run the unit tests.

You can also include a Vagrantfile and a vagrant target in the Makefile that runs vagrant up. Add a vagrant destroy to the maintainer-clean target while you are at it.

This makes your project usable by anyone that is using vagrant or developing without it. If (when) you need to use deploy alongside another project in a vagrant or physical environment, including a clean setup.py and a Vagrantfile that describes your minimal environment makes it simple to install into a virtual environment or a shared vagrant machine.

like image 111
D.Shawley Avatar answered Oct 27 '22 06:10

D.Shawley


If you run one vagrant VM per project, then there is no direct reason to use virtualenv.

If other contributors do not use vagrant, but do use virtualenv, then you might want to use it and support it to make their lives easier.

like image 27
Devin Jeanpierre Avatar answered Oct 27 '22 07:10

Devin Jeanpierre