Django virtual environment - django

What is the proper way of adding an already existing Django project to a newly created virtual environment? Do I just move the project to the virtual environment root directory?

It doesn't matter where the directory is - the only important thing is that you activate the virtual environment every time you want to work on the project.
I personally prefer to have the project directory inside the virtual env directory, but that is not required.
One caveat: don't put the virtual env inside your project directory. That may cause problems with test discovery and with git.

Related

When & where to create a virtual environment for a new django project?

I created my first virtual environment for a django app that I created following a tutorial guide. Now I'm not sure, can I use this very same environment (same name), or do I need to create a new virtual environment when I create a new project?
I understand using virtual environments keeps your individual project dependencies and package versions isolated but my projects all have the same version of django and python. So far I'm not installing any other libraries and I'd imagine they would all just use the same version too. Is there something else I am missing here?
I just experimented with creating a new virtual environment from the current virtual env and I'm now in the newly created one once I activated it. Is it ok to create a new virtual environment from within another virtual environment? Is that any different from doing so outside a virtual environment?
If I were to use the same named virtual environment that I used for the old project, for this new project, what would the implications be?
As You stated in Your question venv (virtual environment) as in official python documentation allows You to create a snapshot of the dependencies that are used within a project so that You are not installing the dependencies in a "global" context which would mean Your main Python directory.
A virtual environment is a Python environment such that the Python
interpreter, libraries and scripts installed into it are isolated from
those installed in other virtual environments, and (by default) any
libraries installed in a “system” Python, i.e., one which is installed
as part of your operating system. A virtual environment is a directory
tree which contains Python executable files and other files which
indicate that it is a virtual environment.
So answering couple of the questions that were mentioned in Your post:
You don't need to create a virtual environment every time You start a new project.
You are not missing anything.
I am not quite sure about creating a virtual environment from within one. (check the directory structure of newly created virtual environment and confirm its contents)
The implications are that You have a shared virtual environment. Any changes to the environment itself are going to influence both projects.
I would highly recommend for You to create named venv for each project independently. In my mind that will limit the probability of errors happening if You make any changes to the environment such as package updates and so on.

Do i have to install django again when i create a new project directory?

I am setting up a new project. I have installed virtual environment. But Do I have to install django again in that directory??
Each project should have its own, separate virtual environment. Reusing the same virtual environment for multiple projects kind of defeats the whole point of virtual environments.
When you want to create a new project, you first create a new virtual environment for it.
A newly created virtual environment is empty, so you have to install Django into it before you can run django-admin startproject.
Everytime you want to run an existing project, e.g. with python manage.py runserver, you first have to activate the virtual environment.
No, you need not to install django again. You can invoke the virtual environment again.
If you have installed virtual environment in the project folder and have configured it that way, then you perhaps have to create a new virtual environment.
There are good tools available:
pyenv
virtualenvwrapper
A good reference point can be RealPython for managing different versions of django.
Yes, you should install Django with every project you start with.

How to start an existing Django project?

I am a new to Django and have 0 experience with this framework.
I cloned a project from git it came with requirements.txt file, but I am not sure how to run it.
Do I need to create virtual environment first and clone the project into the directory of the virtual environment, and then install the requirements?
Do I need to clone the project first into some folder and then create the virtual environment inside this folder and then install the requirements?
Do I need to use any special IDE to run the project? I tried opening the project in PyCharm, without creating a virtual environment first, and it asked me if I want to install the requirements.
I would be glad if someone could explain what is the correct way to run an already existing project.
Let's come to your doubts one by one :
Cloning a repo has nothing to do with Django. You are just making a copy of the code on your disk. Now the copy can be kept anywhere you like(say in ~/Desktop). Also, the directory of a virtual envt. just contains the code of modules you might directly import(like Django), and has nothing to do with the code of the django project. So cloning can be done before or after activating the virtual envt.
You need not create a virtual envt., but you should. A virtual envt. just ensures that you can have different versions of the same module for your different projects. It keeps things organised. So, for example you can create two different virtual envt.'s one with Django=2.0 and another with Django=1.9, to test your website for the two different Django versions.
requirements.txt contains all the modules you will be needing to run the django application. So you first create a virtual envt, activate it and then in the virtual envt., install all the modules you will need. Generally do pip install -r requirements.txt
Now all the required modules are installed, To run the website on a local server( which Django will create for you ), do python manage.py runserver and open in the browser.
No, you don't need an IDE to run the django project. Editing the code on any text editor and running the server from terminal works just fine.
P.S: If you are completely new to python, I would recommend using the conda python distribution. You can create new virtual envt. using conda create as well.
1.Grab a copy of the project.
git clone new_project.git
2.Create a virtual environment and install dependencies.
mkvirtualenv new_project
pip install -r requirements.txt
3.Duplicate new_project/new_project/local_settings_example.py and save as local_settings.py.
4.Enter your database settings in local_settings.py.
5.Initialize your database.
python ./manage.py syncdb
python ./manage.py migrate
6.If your app has a custom user model, you'll need to create a new superuser for the admin.
python ./manage.py createsuperuser
7.Run the development server to verify everything is working.
python ./manage.py runserver

What do I need to do in preparation for cloning a django repository?

Assuming the django project repository is on github and I have had no interaction with it previously.
So: I cd to a new directory on my computer.
I clone the repository.
If the django project is using postgresql, do I have to have postgresql installed on my local machine?
Do I have to be running in a virtual environment to use a specific interpreter?
Thanks Peter
Database
You can actually use another database on your local copy if you choose, although in general it's a good idea to use the same database locally.
If you're going to be using postgres locally, yes you'll need to install it and then create your local database. Once you have your local database setup, you'll need to change some config values of your DATABASES property in settings.
Packages
Your project will also have some dependencies which should be listed in a requirements.txt file at the root directory. If it is not, you'll need to find out which packages need to be installed via pip freeze in the production console.
Virtual Env
You should use a virtual environment, but it's not completely necessary to get your project up and running. Virtualenvs allow you to have different installs and runtimes for different projects.
Other
Every project is different, and there will most likely be some other things that pop up. However, this should get you going in the right direction.

Django: use my FORKED version of an app in some projects, not all of them

I am very lost on how to manage my django apps. In most projects i use standerd versions of apps, but now i find myself forking projects and working on them simultaneously. How can i use my forked version in some projects and the std version in others? For example:
I have all my django projects in a directory calld DJANGOPROJECTS with a structure like this:
DJANGO PROJECTS
-PROJECT A
-PROJECT B
-APP A (forked version)
-APP B (forked version)
SITEPACKAGES (on default python path)
-APP A
-APP B
I want to use FORKED APP A (forked version) in PROJECT A & APP A in PROJECT B. PROJECT B takes care of it's self. Now how do i make PROJECT A use FORKED APP A?
If I put APP A (forked version) in a virualenv for each project i would have to update all of them each time there was a change. If i keep it out of the virtualenv, when i make local changes to the forked app (without doing a git push) all the projects that use it will get the changes instantly.
I solve this problem by using symbolic links to apps in my virtual environments whenever I want them to use a shared version.
I just found a much better way. Setting up symbolic links is kind of a pain! It turns virtualenv has a built in method to do this.
Modify the path to the package you want to use for that environment in:
yourEnv/Lib/site-packages/packagename.egg.link file.
If you are using easy-install then modify the package path in:
yourEnv/Lib/site-packages/easy-install.pth
For example:
If you want PROJECT-A to use an app called APP-A located in your github directory. Find the files noted above and modify the path from:
c:\users\someuser\documents\github\PROJECT-A\src\APP-A
to:
c:\users\someuser\documents\github\APP-A
Now PROJECT-A will use the version of APP-A in your working repository rather then the one in your virtualenv. You can now work with the APP-A repository and the changes will automatically be integrated with PROJECT-A without having to push or pull changes.