Django installation using Virtualenv gone wrong - python

So i have been playing around with python (2.7.x) and django framework on my ubuntu (12.04). The way i setup django based project is by using virtualenv
Which i did it like this
** FROM TERMINAL **
1. sudo apt-get install python-setuptools
2. sudo easy_install virtualenv
Then i continue setting up my directory for my django project by using this command:
virtualenv --no-site-packages project-name
Soon after that, i activated my virtualenv:
source project-name/bin/activate
Then i continue with the Django framework installation using this:
sudo easy_install Django
and verify that Django framework is installed within my virtualenv by checking there is a file called django-admin.py under project-name/bin/ directory (which is exists).
However, the second time i tried to create another django-project (completely different one) following the same exact step as above, I don't have django-admin.py installed in the correct directory. It's get installed to /usr/lib/python...
And when i tried to run the app i get this message:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Does anyone know why is this happening?
Thanks.
* EDITED PART *
responding to sachitad's answer
i get the following message when i executed his/her suggestion
(project-name)blah#blah:~/Documents/python/project-name$ easy_install django
error: can't create or remove files in install directory
The following error occurred while trying to add or remove files in the
installation directory:
[Errno 13] Permission denied: '/home/blah/Documents/python/project-name/lib/python2.7/site-packages/test-easy-install-3775.write-test'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/home/blah/Documents/python/project-name/lib/python2.7/site-packages/
that's why, i initially thought i need to use the sudo command.

Problem:
sudo easy_install Django
Even if you have activated the virtualenv, while installing packages inside virtualenv, never ever use sudo. If you use sudo, it assumes you are installing on the system path(/usr/lib/local/..).
Thus,
easy_install django
OR
pip install django
should work.

As an addition to sachitad answer, I suggest you have a look at virtualenvwrapper which allow you to manage easily you virutal env with commands like :
mkvirtualenv your_project_name // create a virtual environment
workon your_project_name // select this virtual environment
pip install django // will install in this virtualenv

./home/user/path_to_virtual_env_project_/bin/pip install Django
you call pip which not in virtual env.
It seems activate not work.

Related

Does the pip option "--force-reinstall" recompiles a module? (I'm trying to reinstall cx_Oracle)

My setup has:
- oracle-instantclient (downgraded from 12 to 11)
- Virtual Environment with Python + Django + cx_Oracle
When I downgraded the oracle-instantclient, I tried to reinstall cx_oracle using:
pip install --upgrade --force-reinstall cx_Oracle
The problem is that cx_Oracle keeps complaining that I'm not using the previous installed version 12:
$ python -c "import cx_Oracle"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: libclntsh.so.12.1: cannot open shared object file: No such file or directory
So, some questions here: "--force-reinstall" is not supposed to make a full reinstall, including a module recompile (aka cx_Oracle)? Somebody has some suggestion about how to workaround this issue? I looked at the pip manual and just tells me that it reinstalls the package. Not very useful.
Update1:
I tried to follow the solution here, adding my cx_Oracle path to the LD_LIBRARY_PATH but it's still not working.
echo $LD_LIBRARY_PATH
/usr/lib/oracle/11.2/client64/lib:/home/myuser/myuserenv/lib/python3.5/site-packages/cx_Oracle-5.2.1.dist-info
Note that my root user has a different python while my virtualenv uses its own python installation.
[root#myserver]# which python
/usr/bin/python
From my virtual environment:
(myvirtualenv) [myuser#myserver]$ which python
~/myvirtualenv/bin/python
Is there some way of installing cx_Oracle manually using my root user but putting the files at the virtual environment somehow?
Update2:
I tried to download cx_Oracle and compile manually using the following command:
sudo -u myuser ~/myvirtualenv/bin/python setup.py build install
But now I'm seeing some permission errors:
(...)
error: could not create 'build/bdist.linux-x86_64/egg': Permission denied
Am I doing the right thing? I don't want to mess this environment. Thanks.
The answer to your question (although you seem to have had another problem) is as follows:
It depends on the module's structure. If its setup script calls the compiler, then yes it does. Because, you see, pip modules by default do not include compiled parts, it's up to the module author to provide them.
I could finally install. The problem is that I had to use the python of my virtualenv. A simple solution but that I completely missed.
Steps:
pip download cx_Oracle
tar -xvf cx_Oracle-5.2.1.tar.gz
~/myenv/bin/python setup.py -v build
sudo ~/myenv/bin/python setup.py install
Note that I had to use sudo in the last step. Without this I was having this permission error:
error: could not create 'build/bdist.linux-x86_64/egg': Permission denied

ImportError when using virtualenv in Django

My Question is pretty Simple.
Whenever I use virtualenv environment in my Django project it gives the following error when executed this command
python manage.py runserver
File "manage.py", line 8, in
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
But when i use root and give the same command it seems to work perfectly. Why does this Happen? I am in the virtualenv so it means i am the root person by default.How can i fix this error?
I had seen many forums but couldn't find related to mine.
This happens because you have installed django in the system-wide Python interpreter; possibly by doing something like sudo pip install django.
Once you create a virtual environment and activate it - it contains no packages. The concept of a virtual environment is that it allows you to install Python packages without affecting the global Python installation.
So once you activate a virtual environment, you have to install packages in that virtual environment; so you should pip install django (note: without sudo) once you activate the virtual environment; like this:
$ virtualenv sample_env
...
$ source sample_env/bin/activate
(sample_env) $ pip install django
A virtual environment is a virtual environment for Python; it does not control what the user is that is logged into the system.
Finally, as a general rule - you should not be using root for development purposes as doing so can easily compromise your system.
Yup i have same error while running Django. I also have enabled virtual environment. But I was still getting error.
Solution to this problem is using this command to install any python package
python -m pip install django
This will definitely solve your problem. As it solved mine.

Django installed in dist-packages

I am working on an Amazon Linux AMI (version 2013.09).
My goal, among other things, is to have Django up and running.
So here's what I do:
Install pip using yum (it installs pip 7.0.3 in Python2.7/dist-packages)
Install virtualenv using pip
Create a virtualenv (with --no-site-packages)
Install Django using pip inside the virtualenv (this installs Django in the virtenv's Python directory, inside dist-packages)
This seems fine, until I try to use manage.py. The following line:
python manage.py collectstatic --noinput -c -l > /dev/null
Invokes the following error:
OSError: [Errno 2] No such file or directory: '...my-env/lib/python2.7/site-packages/django/contrib/admin/static'
Which is true, because the entire Django infrastructure is in dist-packages, not site-packages.
What is the correct way of fixing this dependency?
Thanks!
UPDATE 28.06.15
The reason Django attempts to access site-packages is the 'STATIC_ROOT' definition in its settings.py file.
Thing is, I installed Django in the exact same way, using the same settings, a couple of years ago, and it worked perfectly.
So what's changed? Why has pip suddenly moved to dist-packages?
sometimes PYTHON_INSTALL_LAYOUT="amzn" gets set in a shell and then stuff annoyingly goes into dist-packages.
To disable this annoyance,
unset PYTHON_INSTALL_LAYOUT

No modules named core.management

First of all I know that there are a lot of posts like this in all Internet, I tried everything and it didn't work.
Also I have some theoretical questions about it.
I run my installation of Django in a Virtualenv so I type:
sudo apt-get install python-setuptools
virtualenv --no-site-packages djangito
source djangito/bin/activate
(djangito)cd djangito/
(djangito)easy_install Django // V.1.6
(djangito)django-admin.py startproject django_test
But when I try to run my runserver happen this:
(djangito)root#marcproves:~/djangito/django_test# python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
BUT
(djangito)root#marcproves:~/djangito/django_test# sudo python manage.py runserver
Validating models...
0 errors found
February 20, 2014 - 09:25:14
Django version 1.6, using settings 'django_test.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
It runs with sudo but....Is runing python from VirtualEnv with sudo python manage.py runserver ?
I know that virtualenv is a kind of stuff for work with independent packages but I don't know if I'm missing something important.
I need to understand why happen this
Thanks at all
Try to use pip instead of easy install. Every virtualenv has it installed by default (along with setuptools, which contain easy_install) and it is the recommended way of installing packages inside virtualenvs.
racech#CONFIG0001:~$ sudo apt-get install python-virtualenv
[...]
racech#CONFIG0001:~$ virtualenv test
New python executable in test/bin/python
Installing distribute.....done.
Installing pip...............done.
racech#CONFIG0001:~$ source test/bin/activate
(test)racech#CONFIG0001:~$ pip install django
[...]
Successfully installed django
Cleaning up...
(test)racech#CONFIG0001:~$ django-admin.py startproject testproject
(test)racech#CONFIG0001:~$ cd testproject/
(test)racech#CONFIG0001:~/testproject$ python manage.py runserver
Validating models...
0 errors found
February 20, 2014 - 10:12:51
Django version 1.6.2, using settings 'testproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
And as for the explanation it looks like easy_install installed Django not into your virtualenv, but to your system Python. You can check that by creating a new virtualenv with a different version of Python. Make a Virtualenv with undefault Python binary (3.x if you use 2.x and 2.x if you use 3.x as default) by running
virtualenv --python=/usr/bin/python3.2 myvirtualenv
and then check it by
python --version
sudo python --version
they should be different. Virtualenv replaces default Python environment only for the user which activated it. Therefore easy_install must have used system Python instead of virtualenv. Maybe you did run it with sudo as well?

Installing Django and Python Suds (without admin rights)

I am trying to setup my project environment which uses the following:
Python 2.5.2
Django 1.3
Python Suds
The server I am running it on already has Python (2.5.2) and Django (1.1) installed but I want to use a newer version of Django and dont have administrator rights to upgrade. How do I go about installing this again?
Should I have to install Python + Django + Suds in a seperate directory? How would I replace standard python paths to this new one?
Thanks!
You can use virtual_env, I have used to play with another (unrelated) python framework buildbot
I would install it Django + Suds in a new directory! then you can grant access to everyone in this dir! then If you are running apache you just have to add this new folder to PYTHONPATH!
Just for record, I have never tried that, but it should work!
try searching about PYTHONPATH in google too, here is one link that might help:
http://www.stereoplex.com/blog/understanding-imports-and-pythonpath
cheers
A Complete Guide To setup django in other path
Installing django through pip
Well many use pip package manager for their install purpose (not my favorite).
to install django through pip you do something like:
pip install django
it will install django in a path which is not accessible by non-root users.
So you must first add the installation place for it.
pip install django --install-option="--prefix=$SOME_PLACE_WE_HAVE_ACCESS_TO" django
This $SOME_PLACE_WE_HAVE_ACCESS_TO can be /home/user/ directory.
now login to python and do the import:
import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
What are we doing wrong????
PYTHONPATH
well as long as you have not installed django in PYTHONPATH python doesn't know where to import the module!!!
do this two steps:
BASH:
echo $PYTHONPATH
PYTHON:
import sys
print sys.path
well sys.path show the path of packages locations installed in python.
and $PYTHONPATH is empty...
The only thing you have to do is to add the path of django egg file to PYTHONPATH
for example in mine its:
/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg
to add it to PYTHONPATH do this:
BASH:
export PYTHONPATH={{EGG PATH}}
which {{EGG PATH}} is the location of your django egg.
WHAT ABOUT django-admin?
well you have to run it from the place that django has set it up in somewhere it has been installed called bin
for that you can add the path of that bin (Think it might be ~/bin or any_place_you_installed/bin) to $PATH...
just like PYTHONPATH we do:
export PATH=$PATH:~/bin
ATTENTION >> : after $PATH is essential!!! to know why do a: echo $PATH
ATTENTION >> ~/bin must be django bin directory so pay attention to that.
Installing django through source
OH My god That's my favorite.
there is nothing difference with the thing up there just insted pip use setup.py...
for that you must have setuptools installed... (I think pip will install that itself if pip raises error for setuptools you must do the whole thing I told up there for django for setuptools too.)
after you installed setuptools you must do this:
./setup.py install --prefix=$PATH_YOU_DESIRE
the rest is the same...
REFERENCES
1 : Installing package through pip in other location.
2 : How to add a PATH To $PATH.

Categories

Resources