简体   繁体   中英

Django, Pyenv and Git file structure on Ubuntu

在此处输入图片说明

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, ie

/home/ubuntu/djenv$ git init

from within

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

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']
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM