How to start a Django project using virtualenv?

Asked

Viewed 1,652 times

3

I have searched and found several different ways and conventions to create a project in Django using virtual environment (virtualenv). Is there a pattern to this? Someone helps me with a correct step by step?

Python Usage 2.7 + Django 1.8

  • A pattern for what exactly? By the way, you’re missing the new version 3.5 of Python...

3 answers

1


Taking into account that you already have Pip installed, to use the isolated environments approach is very simple, even more so with virtualenvwrapper. virtualenvwrapper is, as the name implies, a wrapper for virtualenv, bringing commands that facilitate the use of its functions.

pip install virtualenvwrapper

If you haven’t installed virtualenv before, the above command will do this for you.

After that, to create a new environment becomes very simple:

mkvirtualenv nome_da_env

At the end, your env will be created, and if you have performed correctly to virtualenvwrapper configuration (if you use a Unix-like OS), your env will already be enabled. To disable just run the command:

deactivate

And to activate, or change environment:

workon nome_da_env

In short: install virtalenvwrapper, set it as quoted in the link above, and use these commands to make your life easier.

0

I usually use the Generator-Django to Yeoman.

To install it use the command below:

npm install -g generator-django

Create the virtualenv and activate the:

virtualenv myproject --no-site-packages
source myproject/bin/activate

Now create the project directory and run the Yeoman generator:

mkdir myproject && cd myproject
yo django

More details for your installation on project documentation.

  • As Yeoman is done in Node.js I recommend the installation of NVM. This will make things easier :D

0

I have searched and found several different ways and conventions for the creating a project in Django using a virtual environment (virtualenv).

Creating a project in Django has no relation to virtualenv, I don’t know if the question was poorly formulated, but creating the project depends only on having Django, having no dependency on the environment. Virtualenv is just a tool for creating isolated environments.

So, simply having Django installed on the machine or in the virtualenv environment, you can create your project using django-admin startproject meuprojeto, this is the default, installing any other package to do this is redundant.

Maybe your question is:

How to create an isolated environment and how to start the environment?

At Macosx I do it this way:

iMac:~ Orion$: pip install virtualenv, virtualenvwrapper

In the archive .profile add the references responsible for running the virtualenvwrapper (storing in the file you are sure that when restarting the computer will be reloaded the variables):

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

To create an environment (when creating it already activates the environment):

iMac:~ Orion$: mkvirtualenv ambiente_teste

To deactivate:

(ambiente_teste)iMac:~ Orion$: deactivate

To reactivate (using workon):

iMac:~ Orion$: workon ambiente_teste

And to create a project (check if you are creating the project in the desired directory):

(ambiente_teste)iMac:~ Orion$: django-admin startproject meuprojeto

Browser other questions tagged

You are not signed in. Login or sign up in order to post.