0

enter image description here

I am working to set up a django project on ec2 with an Ubuntu 14.4 LTS instance. I want to write my code using python 3 and django. I've been advised that the best way to do this is to use a virtualenv. Following https://robinwinslow.co.uk/2013/12/26/python-3-4-virtual-environment/

I tried:

 ~$ pyvenv-3.4 djenv

Which appears to create a virtualenv (please see screenshot). Now I have 2 questions:

1) What folder should I place my django project. - I'm thinking within the djenv folder. In other words I'd run:

/home/ubuntu/djenv$ django-admin.py startproject testproject.

2) init a git repository. I'm assuming I'd to it it in the same location, i.e.

/home/ubuntu/djenv$ git init

from within

Does this seem correct or is there a better way to do this?

user1592380
  • 34,265
  • 92
  • 284
  • 515

1 Answers1

2

Your project source code should be entirely separate from your virtual env in the file system. If they are in the same place, as you suggest, then you will end up checking libraries into your git repository needlessly and that will take up extra space end up causing problems.

Once you have activated a virtualenv you can run Python and use all the libraries in it. You don't need any connection in the file system.

You should store a PIP file in your git repo somewhere that describes how to install the relevant dependencies into your virtualenv so you can re-create it on another machine.

On my machine my projects are in /home/me/projects/«project» and my virtualenvs are in /home/me/envs/«envname». I use virtualenvwrapper which makes things easy.

Create an environment

$ mkvirtualenv test
New python executable in test/bin/python
Installing Setuptools......done.
Installing Pip.........done.

Activate it

$ workon test

Python now refers to the one in my environment. It has its own site-packages etc.

$ which python
/Users/joe/Envs/test/bin/python

If we run it and look at the paths, they point to the virtualenv. This is where it looks for packages (lots removed from my path for simplicity).

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Users/joe/Envs/test/lib/python27.zip', '/Users/joe/Envs/test/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Users/joe/Envs/test/lib/python2.7/site-packages']
>>>
Joe
  • 46,419
  • 33
  • 155
  • 245
  • Hi Joe , Thanks for your help. I've just been looking at http://virtualenvwrapper.readthedocs.org/en/latest/. I'm confused about how to set a specific python interpreter for a virtualenv using your method. – user1592380 Feb 27 '15 at 17:44
  • When you're in a virtual environment, the appropriate python interpreter is chosen automatically (actually it's all done with symlinks, but it looks like the python interpreter is at a different path). That is the whole point of virtualenv. I'll add an example to my answer. – Joe Feb 27 '15 at 17:52
  • I don't think I'm explaining myself well. I've used virtualenvs before, so I understand the concept, but how do you tell virtualenvwrapper which python interpreter to use inside a virtualenv that it creates? – user1592380 Feb 27 '15 at 17:58
  • The virtualenv has a `python` executable which is just a symlink to the actual interpreter. If you run `mkvirtualenv --help` it shows the documentation for the `--python` flag, which allows you to specify which version it should link to. – Joe Feb 27 '15 at 18:02
  • Maybe this question is relevant: http://stackoverflow.com/questions/6401951/using-different-versions-of-python-with-virtualenvwrapper – Joe Feb 27 '15 at 18:05
  • Thanks Joe again. This helps me out a lot. I'll plug away at it and ask a new question if I run into problems. - Bill – user1592380 Feb 27 '15 at 18:08