Run an existing python web application inside a virtalenv

310 views Asked by At

I am new to python development using virtualenv. I have installed python 2.7, pip, virtualenv, virtualenvwrapper in windows and I am using windows PS. I have referred lots of tutorials for setting this up. Most of them contained the same steps and almost all of them stopped short of explaining what to do after the virtualenv was created.

  1. How do I actually work in a virtualenv? suppose I want to create a new flask application after installing that package in my new env virtualenv (eg; testenv).

  2. If I already have an existing project and I want to put it inside a newly created virtual env, how do I do that? How should the folder structure be like?

  3. My understanding of virtual env is that it provides a sandbox for your application by isolating it and keeping all its dependencies to itself in that particular env (and not sharing and it with others). Have I understood it wrong?

Please help me clear this.

2

There are 2 answers

0
Blender On BEST ANSWER

How do I actually work in a virtualenv? suppose I want to create a new flask application after installing that package in my new env virtualenv (eg; testenv).

You open up Command Prompt and activate the virtualenv:

> \path\to\env\Scripts\activate

When you run python and pip, they run in the virtualenv. You have to do this for every Command Prompt window, since working in a virtualenv is really just running C:\path\to\env\bin\python instead of just python and C:\path\to\env\bin\pip instead of pip.

If I already have an existing project and I want to put it inside a newly created virtual env, how do I do that? How should the folder structure be like?

It doesn't matter. When you install Python packages, they get installed globally into C:\Python27\site-packages. With virtualenv, you can create isolated Python environments that have their own packages, so if you're working on two projects that require different versions of a package, they can coexist without any issues.

Some people make a folder for their virtualenvs (like C:\Users\you\Virtualenvs\my_website). You can also store it with your project (like C:\Users\you\Projects\my_website\venv). Once you activate it, the location doesn't matter. I use the latter.

My understanding of virtual env is that it provides a sandbox for your application by isolating it and keeping all its dependencies to itself in that particular env (and not sharing and it with others). Have I understood it wrong?

Nope. The only point that I would clarify is that the "sandbox" is only for Python's packages, it doesn't affect your application in any way.

0
hd1 On
  1. testenv/bin/pip and testenv/bin/python
  2. I'd check it in a local repository and check it out in the virtualenv.
  3. No, you have not.