How do I properly install Django? - python

I'm trying to setup a Django project and am a bit stumped on this part of setting everything up.
I've ran pip install django and tried running django by doing
python
import django
but I immediately get a message ImportError: no module named Django
I've tried going to the folder /usr/local/lib/python2.6/site-packages but there is nothing in that folder. If I go to /usr/local/lib/python2.6/dist-packages, all of my modules are there including django. Now should everything go in the site-packages folder? I'm really confused as to why django isn't running the way it should be. I tried reading the Django documentation but that assumes my installation correctly goes into .../site-packages

Are you sure that you are using "Virtualenv", if not than you should think about using virtualenv. I suspect you are not using it and doing a system install of Django.
So make sure that Django is getting installed properly. May be that you dont have the user rights to make a system wide install, so try to do sudo pip install django.
Hope it works out for you, if not try to be more descriptive of your problem.

Use pip install Django.
For some reason they used a capital 'D' in Django instead of the normal all lower case. Source:Django Documentation.
Also make sure that/usr/local/lib/python2.6/dist-packages is added to your PATH.
The normal behavior of pip is to install into the site-packages folder. Maybe try re-installing pip?

You should set up a virtualenv, especially if you're going to be making several Django applications that have different requirements. You also have to run pip with sudo so sudo pip install django. To learn how to make a virtualenv: http://www.virtualenv.org/en/latest/ .
You can also check your $PATH variable to see if /usr/local/lib/python2.6/dist-packages is there, if it's not you can add with by adding the following to your .bash_profile
PATH="${PATH}:/usr/local/lib/python2.6/dist-packages"
export PATH
Under virtualenv, you don't have to use sudo pip, instead cd (change directory) to your environment directory, and run pip install django and it will only install it for that project.

Related

Package management issue with Poetry virtual environment and Django

I spent a good deal of time the past two days trying to use poetry correctly. I'm not new to poetry and have used it for a while. But in this DJANGO project I just started with a buddy, after adding packages, some of them needed to be added again via 'python -m pip install '. Once I did that, the django project passed a check and I could also migrate tables. It really bothers me that I'm using a virtual management package that doesn't appear to always work. I like poetry and want to use it. Some of the packages I had to pip install are django and pymysql. I also use pyenv. I'm running python version 3.10.6. I did things such as deleting the .venv folder (mine gets created in the project directory) checking with pip freeze that these packages are not already installed, and then doing a poetry update to re-create .venv. Any suggestions or ideas? I really need to focus on my django project and not be farting around all the time with package issues so I would like to either fix this or at least have a better idea of what is going wrong so I can avoid it.

Not able to install django and use an existing django project

I am studying Django for the first time and everything was going well until the last day. Today when I tried opening my Django project, the terminal returns an error saying :-
Unable to create process using
'C:\Users\User1\AppData\Local\Programs\Python\Python310\python.exe manage.py runserver'
Thinking that it might be an issue with the virtual env, I tried checking my Python Interpreter to make sure I am currently in my venv.
Fortunately, I am currently using my venv but Pycharm indicates me to Install Python Packaging Tools.
I tried the link to install it but it notifies a Non Zero Exit Code(101) and Invalid Python SDK error.
Googling for the error asked me to check the path in Environment Variables where two are present -
C:\Users\User1\AppData\Local\Programs\Python\Python310\
C:\Users\User1\AppData\Local\Programs\Python\Python310\Scripts\
Recently I had added MingW Compiler to the path and that was it. I've never touched the path of Python.
I would like to mention that there are no errors in creating a new virtual envirnoment nor activating it. But things get worse when I try to pip install django. CMD tells that unable to create process.
Please help! I am actually stuck with my project.
Happy to say that I have fixed the issue.
Steps followed include :
Uninstalled Python,Deleted Path,Restarted my pc
Downloaded and Installed Python 3.10.2 from the official site.
Upgraded pip
Installed virtualenv using pip install virtualenv
Tested it creating env using - py -m venv "environment name"
pip install django works + started a project and the setup was successful.
First, try to run any python file (not in your project).
If it works, then the problem is with your django project or the env (try to create a new env and move your files to it).
At last, I think you should reinstall python; this will fix the problem I guess.

Pycharm doesn't have Django, or pyenv, or let me install python modules. Completely lost

I'm trying to work with python in PyCharm, and set up a virtualenv for my project. What I imagine should be the way this works is that I set up a project to pull from a repository. It's a Django project, so I enable Django support. Setting up a pyenv is good hygiene, so I set the interpreter to use a pyenv. PyCharm supposedly includes pyenv, so I don't need to install it from my os, and I was under the impression that either Django came with it as well, or I could install it from PyCharm.
Instead of any of that working the way it seems like it should and has been described, pyenv doesn't appear to be installed:
$ pyenv
No command 'pyenv' found, did you mean:
Command 'p7env' from package 'libnss3-tools' (main)
Command 'pyvenv' from package 'python3-venv' (universe)
pyenv: command not found
$ virtualenv
The program 'virtualenv' is currently not installed. You can install it by typing:
sudo apt-get install virtualenv
Of course, the only reason I'm even caring about that is that going to Settings --> Project --> Project Interpreter to install new modules doesn't work, either with or without a Pycharm-created virtualenv. I made a post to the Jetbrains forums here, but I'm not expecting a response, as they seem almost completely abandoned.
Then I thought I'd try enabling Django support, so that I could at least get that module working. But instead it says that Django isn't installed; In the Run/Debug configuration, at the bottom it says Django is not importable, the django icon has an x on it, and of course, every import django statement is underlined in red, indicating it can't be imported. My versions are:
Ubuntu 15.10
PyCharm 5.0.4
Python 2.7.10
So why does everything seem to be broken? What am I missing? If I have to install outside of pycharm, I don't really care, I'm just trying to set everything up the right way, so that nothing steps on anything else, and everything works as intended. Pycharm seemed to "volunteer" to handle everything, and I'm just trying to make that work. Using a pyenv seems to be what everyone recommends, and I'm just trying to do that. Any help at all would be appreciated.
To install Django on your virtualenv using pyenv please use the following command line:
pyenv activate <virtualenv_name>
pip install django
Or (for example if pyenv fails to activate your virtualenv due to some reason) it is possible to just execute pip using the full path:
<path_to_virtualenv_folder>/bin/pip install django
Then select the Python interpreter from your virtualenv as a project interpreter in PyCharm.
Normally PyCharm should be able to install packages, if it doesn't work please submit an issue to PyCharm tracker.

How to install Django with pip inside a virtualenv in /var/www/html/project_name folder?

I'm using Ubuntu 15.04 with Python 2.7, pip 1.5.6 and virtualenv 1.11.6.
I will create a Django project inside /var/www/html/project_name (and work in that directory) for use it with Apache2.
I created a virtual environment named venv inside the project_name folder for syncing purposes.
With the virtual environment activated, I just cant't run pip install django because I get 'Permission denied' message. So I try the command sudo pip install django, but that will install Django globally.
So, running which pip, I get /var/www/html/project_name/venv/bin/pip.
But running sudo which pip, I get /usr/bin/pip.
Does someone get any idea about how it is possible to install Django (or any other package) inside the virtual environment?
PS: I know it's possible to run sudo venv/bin/pip install django, but it doesn't seem very useful.
Your trouble is presumably that you don't have write access to this /var/www/html/project_name directory. Your choices are:
Change the permissions, recursively, so that you do have permissions to write to that directory.
Run the following commands:
.
$ sudo su
# . venv/bin/activate
# pip install
Just to add what everyone's been saying and I sort of skimmed over: NEVER EVER put sensitive things (that includes your django project) under the document root. You can store it under something like /var/www/scripts or something, but don't put it under the document root.
Another way of deploying is using something like gunicorn as the "main" webserver and then just having whatever world visible webserver (like apache) reverse proxy to gunicorn. I've done this with nginx and it's fairly easy to setup, the only down side is then you have to setup something extra in your system's init scripts to start up gunicorn.

Installing certain packages using virtualenv

So, I want to start using virtualenv this year. I like the no-site-packages option, that is nice. However I was wondering how to install certain packages into each virtualenv. For example, lets say I want to install django into each virtualenv... is this possible, and if so, how? Does buildout address this?
Well it's not so much django, more like the django applications... I dont mind installing a version of django into each virtualenv... i was just wondering if there was some intermediate option to 'no-site-packages'
I know where you're coming from with the no-sites-option. I want to use pip freeze to generate requirements lists and don't want a lot of extra cruft in site-packages. I also need to use multiple versions of django as I have legacy projects I haven't upgraded (some old svn checkouts (pre1.0), some 1.0, and some new svn checkouts). Installing Django in the global site-packages isn't really an option.
Instead I have a django folder with releases and a couple of different svn versions and just symlink to the appropriate version in the local site-packages. For ease of use I link to the local site-packages at the same level as the environment and then link in the appropriate django directory and any other "system" style packages I need (usually just PIL). So:
$ virtualenv pyenv
$ ln -s ./pyenv/lib/python2.5/site-packages ./installed
$ ln -s /usr/lib/python2.5/site-packages/PIL ./installed
$ ln -s /opt/django/django1.0svn/trunk/django ./installed
Now the following works:
$ source pyenv/bin/activate
$ python
> import django
> import PIL
If you want django to be installed on EACH virtualenv, you might as well install it in the site-packages directory? Just a thought.
I'd suggest using virtualenv's bootstrapping support. This allows you to execute arbitrary Python after the virtualenv is created, such as installing new packages.
The other option (one I've used) is to easy_install Django after you've created the virtual environment. This is easily scripted. The penalty you pay is waiting for Django installation in each of your virtual environments.
I'm with Toby, though: Unless there's a compelling reason why you have to have a separate copy of Django in each virtual environment, you should just consider installing it in your main Python area, and allowing each virtual environment to use it from there.
I want to check out this project:
http://www.stereoplex.com/two-voices/fez-djangoskel-django-projects-and-apps-as-eggs
Might be my answer....

Categories