Import Error: No module named django - python

I am using centos linux.
I had python 2.6 with django and now i upgraded to python 2.7.
Python 2.6 is located in /usr/lib/python2.6.
Python 2.7 is located in /usr/local/lib/python2.7.
They both have site-packages directory and they both contain django 1.2.
If i run python i get the 2.7 version.
My problem is that if try to import django i get
ImportError: No module named django
I am not sure where is my PYTHONPATH defined and if this is what i need to change.
anyone ?
i ended up making a symbolic link to the 2.6 site-packages directory.

I had the same error, and this fix my issue
python -m pip install django
:) Done!

To check your path, you can use the following code:
import sys
print(sys.path)
If you already know where django is installed, it should be easy to test if the desired directory is in your path with directory in sys.path.
Regarding where your PYTHONPATH is defined, note that it's an environment variable, so you can check its value (if defined) with: echo $PYTHONPATH

Under linux, you can set the PYTHONPATH environment variable in your .profile or .bashrc. You can either edit it directly from the terminal by changing to your home directory (cd ~), and then edit the file (nano .bashrc), or by opening the file with gtkedit or vim or whatever, and add:
PYTHONPATH=/usr/local/lib/python2.7/site-packages:/another/path/etc
If you want to test this before editing your profile, you can export this from the terminal as:
export PYTHONPATH=/local/lib/python2.7/site-packages
I'm assuming you're running this straight from the command line. If you're running it as a wsgi module in apache, you can add this to your syspath from your wsgi file as:
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')

try
pip freeze
this command show which packages are installed in your system
then run with root privilege
pip install django
then create a new project with command
django-admin.py startproject mysite
then start your project
cd path/to/mysite
./manage.py runserver
in file wsgi.py add this lines
import os
import sys
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)

Try printing sys.path to see what's in your path. Django need to be in one of the dirs listed. Example on Windows:
>>> import sys
>>> for p in sys.path: print p
C:\Python27\Lib\idlelib
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
>>>

django went missing with an upgrade to python 3.7
pip3 install django
fixed the problem.

python3 -m django --version1
for me it was that^

If you are using a environment use:
$ <environment_location>/<environment_name>/bin/python manage.py runserver

I also had same error but easily solved it .those who are using version 4 and above of Django and python 3.0 can do the following(for windows)
pip install virtualenv #installs virtual environment in pc
py -m venv myvenv #creates virtual environment named myvenv inside folder
myvenv/Scripts/activate # activates the virtual environment
You are now ready to go.
pip install django # pip install django
python -m django --version # checks the version of installed django

Related

Django framework startapp [duplicate]

I'm following the Django tutorial https://docs.djangoproject.com/es/1.10/intro/tutorial01/
I've created a "mysite" dummy project (my very first one) and try to test it without altering it.
django-admin startproject mysite
cd mysite
python manage.py runserver
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
I'm getting a SyntaxError on a file that was generated by the system itself. And I seem unable to find anyone else who has gone through the same issue.
I'll add some data of my setup in case it may be of use
$ vpython --version
Python 2.7.12
$ pip --version
pip 9.0.1 from /home/frank/.local/lib/python2.7/site-packages (python 2.7)
$ python -m django --version
1.10.6
Adding contents of autogenerated manage.py
cat manage.py
#!/usr/bin/env python3
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
Make sure which python version you connect the django with (Make sure to activate the virtual env if you are using any).
When you install django using just
pip install django
then you have to run
python manage.py startapp <yourApp name>
else if you have used:
pip3 install django
then you have to run
python3 manage.py startapp <yourapp name>
Refer:
You can try with python3 manage.py runserver.
It works for me.
You should activate your virtual environment.
In terminal, source env/bin/activate. Depending on your shell, something like (env) should now be a part of the prompt.
And now runserver should work. No need to delete exc part!
Just activate your virtual environment.
For running Python version 3, you need to use python3 instead of python.
The final command will be:
python3 manage.py runserver
I was experiencing the same but this was solved by running with specific python 3.6 as below:
python3.6 manage.py runserver
Its a simple solution actually one i just ran into. Did you activate your virtual environment?
my terminal screenshot
It's best to create a virtual environment and run your Django code inside this virtual environment, this helps in not changing your existing environments. Here are the basic steps to start with the virtual environment and Django.
Create a new Directory and cd into it.
mkdir test , cd test
Install and Create a Virtual environment.
python3 -m pip install virtualenv virtualenv venv -p python3
Activate Virtual Environment: source venv/bin/activate
Install Django: pip install django
Start a new project: django-admin startproject myproject
cd to your project and Run Project:
cd myproject,
python manage.py runserver
You can see your project here: http://127.0.0.1:8000/
After testing with precise instructions (using python2 or python3 instead of just "python") I've constated that no matter what the tutorial says, this works ONLY with python3.
The solution is straightforward. the exception from manage.py
is because when running the command with python, Django is unable
to predict the exact python version,
say you may have 3.6, 3.5, 3.8 and maybe just one of this versions pip module was used to install Django
to resolve this either use:
./manage.py `enter code here`<command>
or using the exact python version(x.x) stands:
pythonx.x manage.py <command>
else the use of virtual environments can come in handy
because its relates any pip django module easily to python version
create env with pyenv or virtualenv
activate (e.g in virtualenv => virtualenv env)
run using python manage.py command
I solved same situation.
INSTALLED VERSION
python 3.6, django 2.1
SITUATION
I installed Node.js in Windows 10. After python manage.py runserver caused error.
ERROR
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
REASON
My python path changed to python-2.7 from python-3.6. (3.6 is correct in my PC.)
SOLUTION
Fix python path.
The following could be the possible reasons,
1. The virtual environment is not enabled
2. The virtual environment is enabled but the python version is different
To create virtual environment
$ virtualenv --python=python3 venv
To activate the virtual environment
$ source venv/bin/activate
You must activate virtual environment where you have installed django.
Then run this command
- python manage.py runserver
Also, the tutorial recommends that a virtual environment is used (see Django documentation: https://docs.djangoproject.com/en/2.0/topics/install/#installing-official-release"). You can do this with pipenv --three. Once you've installed django with pipenv install django and activated your virtual environment with pipenv shell, python will refer to python3 when executing python manage.py runserver.
Pipenv documentation:
https://pipenv.kennethreitz.org/
Activate your virtual environment then try collecting static files, that should work.
$ source venv/bin/activate
$ python manage.py collectstatic
You should start your Virtual Environment,
How to do it?
First with terminal cd into the directory containing manage.py
Then type $source <myvenv>/bin/activate
replace with you Virtual Environment name, without angular brackets.
Another issue can that your root directory and venv mis-match.
The structure should be something like this:
|-website
..facebook
..manage.py
..myvenv
..some other files
That is your virtual environment and manage.py should be in the same folder. Solution to that is to restart the project. If you are facing this error you must haven't coded anything yet, so restart.
I had the exact same error, but then I later found out that I forget to activate the conda environment which had django and other required packages installed.
Solution: Create a conda or virtual environment with django installed,
and activate it before you use the command:
$ python manage.py migrate
The django-admin maybe the wrong file.I met the same problem which I did not found on a different computer the same set-up flow.
After comparing two project, I found several difference at manage.py and settings.py, then I realized I created 2.0 django project but run it with python2.
runwhich django-adminin iterm
/Library/Frameworks/Python.framework/Versions/3.6/bin/django-admin
It looks like I got a django-admin in python3 which I didn't know why.So I tried to get the correct django-amin.
pip show django
then I got
Name: Django
Version: 1.11a1
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation#djangoproject.com
License: BSD
Location: /Library/Python/2.7/site-packages
Requires: pytz
In/Library/Python/2.7/site-packages, I found the django-admin
/Library/Python/2.7/site-packages/django/bin/django-admin.py
So I created project again by
/Library/Python/2.7/site-packages/django/bin/django-admin.py startproject myproject
then run
cd myproject
python manage.py runserver
succeeded🎉
We have to create a virtual environment inside the project, not outside the project..
Then it will solve..
I landed on the same exact exception because I forgot to activate the virtual environment.
I was also getting the same error.
Then I went back to the folder where the environment folder is there and I forgot to activate a Virtual environment so only I was getting this error.
Go to that folder and activate the virtual environment.
$ source env/bin/activate
I had this issue (Mac) and followed the instructions on the below page to install and activate the virtual environment
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
$ cd [ top-level-django-project-dir ]
$ python3 -m pip install --user virtualenv
$ python3 -m venv env
$ source env/bin/activate
Once I had installed and activated the virtual env I checked it
$ which python
Then I installed django into the virtual env
$ pip install django
And then I could run my app
$ python3 manage.py runserver
When I got to the next part of the tutorial
$ python manage.py startapp polls
I encountered another error:
File "manage.py", line 16
) from exc
^
SyntaxError: invalid syntax
I removed
from exc
and it then created the polls directory
Same issue occurred to me,But what I did was,
Just Replaced:
python manage.py runserver
with
python3 manage.py runserver
in the terminal(macOsX). Because I am using Python version 3.x
I encountered the same error when using pipenv. The issue was caused by not accessing Django correctly from within the virtual environment.
The correct steps using pipenv:
Activate virtual environment: pipenv shell
Install Django: pipenv install django
Create a project: django-admin startproject myproject
Navigate into project folder: cd myproject
Start Django with pipenv: pipenv run python manage.py runserver
Note: Pipenv will use the correct python version and pip within the virtual environment.
It seems you have more than one version of Python on your computer.
Try and remove one and leave the only version you used to develop your application.
If need be, you can upgrade your version, but ensure you have only one version of Python on your computer.
What am I wondering is though the django is installed to the container it may not be in the host machine where you are running the command. Then how will the command run. So since no above solutions worked for me.
I found out the running container and get into the running container using docker exec -it <container> bash then ran the command inside docker container. As we have the volumed container the changes done will also reflect locally. What ever command is to be run can be run inside the running container
For future readers,
I too had the same issue. Turns out installing Python directly from website as well as having another version from Anaconda caused this issue. I had to uninstall Python2.7 and only keep anaconda as the sole distribution.
Have you entered the virtual environment for django? Run python -m venv myvenv if you have not yet installed.
I had same problem and could solve it. It is related to the version of Django you've installed, some of them are not supported by python 2.7. If you have installed Django with pip, it means that you are installing the latest version of that which probably is not supported in python 2.7, You can get more information about it here. I would suggest to python 3 or specify the version of Django during installing (which is 1.11 for python 2.7).
I solved this problem to uninstall the multiple version of Python.
Check Django Official Documentation for Python compatibility.
"Python compatibility
Django 2.1 supports Python 3.5, 3.6, and 3.7. Django 2.0 is the last version to support Python 3.4."
manage.py file
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'work.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
If removing "from exc" from second last line of this code will generate another error due to multiple versions of Python.

Why django-admin.py executed in root not virtual environment?

I am working in ubuntu. I have installed the django==1.3 in root earlier. Now I created local virtual environment name as local_env using virtualenv. Then I activated the local_env using source command.But I didn't install the django in this local_env environment. When I try to create the django sample project using
django-admin.py startproject sampleproject
it is working perfectly. My question is,It didn't prevent the local_env from the root environment ?. I mean it didn't raise the error like django-admin.py command didn't found. Please put the comments if any doubts.
django-admin.py is in the OS path. When you try executing it inside your virtual environment, the python will look into it's VE's sys.path and if not found it will search through the OS path, found it from the root environment and so there were no errors shown.
According to the docs, the django-admin.py command is installed on your system path, so if the virtualenv doesn't find this command, it will probably look in the global sys path and find a match.
If you want to use a different version of django to the one installed on your sys path, you will need to install it in your vritualenv using a package manager like pip and this will then take precedent over the global django-admin.py
To make sure you're doing this right, load your the virtualenv using
dhanna#dhanna:~$ source local_env/bin/actviate
If successful, your prompt should have the virtualenv name at the beginning - eg
(local_env)dhanna#dhanna:~$
Note that if you activate the virtualenv in one shell, but run the python interpreter inside a separate shell, you will be using the global interpreter and therefore have the global django-admin.py module available.
Next you'll want to install the django module
(local_env)dhanna#dhanna:~$ pip install django
To check if django is installed inside your virtual env, you can use the package management tool pip and pass the freeze argument
(local_env)dhanna#dhanna:~$ pip freeze
Now you can use the virtualenv's version of django-admin.py
(local_env)dhanna#dhanna:~$ django-admin.py startproject sampleproject

Django - "no module named django.core.management"

I get the following error when trying to run Django from the command line.
File manage.py, line 8, in <module>
     from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Any ideas on how to solve this?
It sounds like you do not have django installed. You should check the directory produced by this command:
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
To see if you have the django packages in there.
If there's no django folder inside of site-packages, then you do not have django installed (at least for that version of python).
It is possible you have more than one version of python installed and django is inside of another version. You can find out all the versions of python if you type python and then press TAB. Here are all the different python's I have.
$python
python python2-config python2.6 python2.7-config pythonw2.5
python-config python2.5 python2.6-config pythonw pythonw2.6
python2 python2.5-config python2.7 pythonw2 pythonw2.7
You can do the above command for each version of python and look inside the site-packages directory of each to see if any of them have django installed. For example:
python2.5 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python2.6 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
If you happen to find django inside of say python2.6, try your original command with
python2.6 manage.py ...
sudo pip install django --upgrade
did the trick for me.
I got the same error and I fixed it in this manner:
I had to activate my virtual environment using the following command
source python2.7/bin/activate
Most probably in your manage.py the first line starts with !/usr/bin/python which means you are using the system global python rather than the one in your virtual environment.
so replace
/usr/bin/python
with
~/projectpath/venv/bin/python
and you should be good.
well, I faced the same error today after installing virtualenv and django. For me it was that I had used sudo (sudo pip install django) for installing django, and I was trying to run the manage.py runserver without sudo. I just added sudo and it worked. :)
Are you using a Virtual Environment with Virtual Wrapper? Are you on a Mac?
If so try this:
Enter the following into your command line to start up the virtual environment and then work on it
1.)
source virtualenvwrapper.sh
or
source /usr/local/bin/virtualenvwrapper.sh
2.)
workon [environment name]
Note (from a newbie) - do not put brackets around your environment name
I am having the same problem while running the command-
python manage.py startapp < app_name >
but problem with me is that i was running that command out of virtual environment.So just activate your virtual environment first and run the command again -
I experience the same thing and this is what I do.
First my installation of
pip install -r requirements.txt
is not on my active environment. So I did is activate my environment then run again the
pip install -r requirements.txt
The problem occurs when django isn't installed on your computer. You also get this error because the django.core.management module is missing.
To solve this issue we have to install django using pip. Open a command line prompt -> cmd(on windows) and enter the following command:
pip install django
This command will install django on your computer. So consider installing pip first. Here's how to install pip on a Windows machine
Okay so it goes like this:
You have created a virtual environment and django module belongs to that environment only.Since virtualenv isolates itself from everything else,hence you are seeing this.
go through this for further assistance:
http://www.swegler.com/becky/blog/2011/08/27/python-django-mysql-on-windows-7-part-i-getting-started/
1.You can switch to the directory where your virtual environment is stored and then run the django module.
2.Alternatively you can install django globally to your python->site-packages by either running pip or easy_install
Command using pip: pip install django
then do this:
import django print (django.get_version()) (depending on which version of python you use.This for python 3+ series)
and then you can run this: python manage.py runserver and check on your web browser by typing :localhost:8000 and you should see django powered page.
Hope this helps.
In case this is helpful to others... I had this issue because my virtualenv defaulted to python2.7 and I was calling Django using Python3 while using Ubuntu.
to check which python my virtualenv was using:
$ which python3
>> /usr/bin/python3
created new virtualenv with python3 specified (using virtualenv wrapper https://virtualenvwrapper.readthedocs.org/en/latest/):
$ mkvirtualenv --python=/usr/bin/python3 ENV_NAME
the python path should now point to the virtualenv python:
$ which python3
>> /home/user/.virtualenvs/ENV_NAME/bin/python3
This also happens if you change the directory structure of your python project (I did this, and then puzzled over the change in behavior). If you do so, you'll need to change a line in your /bin/activate file. So, say your project was at
/User/me/CodeProjects/coolApp/
and your activate file is at
/User/me/CodeProjects/coolApp/venv/bin/activate
when you set up your project, then you changed your project to
/User/me/CodeProjects/v1-coolApp/
or something. You would then need to open
/User/me/CodeProjects/v1-coolApp/venv/bin/activate
find the line where it says
VIRTUAL_ENV="/User/me/CodeProjects/coolApp"
export VIRTUAL_ENV
and change it to
VIRTUAL_ENV="/User/me/CodeProjects/v1-coolApp"
before reactivating
In my case, I am using Ubuntu. The problem can be that I don't have the permission to write to that folder as a normal user. You can simply add the sudo before your command and it should work perfectly. In my case sudo python manage.py syncdb.
I had the same issue and the reason I was getting this message was because I was doing "manage.py runserver" whereas doing "python manage.py runserver" fixed it.
I had the same problem and following worked good, you should navigate main folder in your project than type:
source bin/activate
My case I used pyCharm 5 on mac. I also had this problem and after running this command my problem was solved
sudo pip install django --upgrade
had the same problem.run command 'python manage.py migrate' as root. works fine with root access (sudo python manage.py migrate )
You can try it like so : python3 manage.py migrate (make sur to be in the src/ directory)
You can also try with pip install -r requirements.txt (make sur you see the requirements.txt file when you type ls after the migrate
If after all it still won't work try pip install django
Hope it helps
I got the same problem trying to use the python manage.py runserver. In my case I just use sudo su. Use the terminal as a root and try it again an it works partially. So I use python manage.py migrate comand and it fix it.
You must choose your Project first before running the server , type this
workon your_project_name
then
python manage.py runserver
It is because of virtual enviornment configuration. You need to work on your virtual enviornmnet of Python. You should try on your command promt with,
workon virtual_enviornment_name
File and Directory ownership conflict will cause issues here. Make sure the ownership of the directories and files under the project are to the current user. (You can change them using the chown command with the -R option.) Try rerunning the command: this solved the problem for me when running through the "First Django App" sample:
python manage.py startapp polls

Command not found: django-admin.py

I am a complete beginner to Python/Django, but I want to dive right in and start experimenting. Thus I was following this guide on installing Python/Django http://devcenter.heroku.com/articles/django.
Everything is working fine until the step
django-admin.py startproject hellodjango
Where I get
command not found: django-admin.py
Now I have tried a few things, but none have really worked out. Is there someone kind enough to point me in the right direction?
P.S. Is there a great guide out there on running Python/Django locally on a Mac to run and test apps?
I'm on Mac OS X Lion, Python 2.7.
When that didn't work for me, I tried python -m django startproject mysite and it worked.
Actually, if you use Ubuntu, it's just django-admin not django-admin.py.
Resides in /usr/bin
Probably the same thing on Mac.
You're using a Windows tutorial.
It may also tell you
python manage.py runserver
and that is actually
python ./manage.py runserver
I tried as the method, it worked.
pip uninstall django
sudo pip install django
django-admin startproject example
It worked well.
Make sure you properly did the source bin/activate command. If you skip that, or do it in a different terminal window, or close the window then re-open it, you won't be in the virtualenv and you won't have access to the django-admin.py command in your environment.
If you´re on Windows, here´s what worked for me (using pylauncher):
$ py -m django startproject myproject
To solve this problem, you need:
Find the main folder of Django, and find the django-admin.py file
Typically, the file is in <YOUR_DJANGO_FOLDER>/bin/django-admin.py
Create a link for this file
ln -s /bin/django-admin.py /usr/local/bin/django-admin
Type django-admin in your command to check if it works
As Timmy said above, it could just be that django-admin.py is not on your system path.
See here - https://code.djangoproject.com/wiki/InstallationPitfalls - for 3 possible causes with solutions.
To Windows users
using django-admin instead of django-admin.py worked for me in Windows.
For me this one worked
python3 -m django startproject mysite
Then it doesn't tell you that it created file you must check by yourself in your home
hardrive/user/urhome
python3 -m django startproject mango
I have used follwong command to install (/usr/local/bin) MAC OS
pip install django
django-admin startproject mysite
The following guide in official site https://code.djangoproject.com/wiki/InstallationPitfalls
First, you should find location of django-admin.py by
which django-admin.py
Example: in my case
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/bin/django-admin.py
You use sudo ln -s to relocate django-admin-py to /usr/local/bin
sudo ln -s /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/bin/django-admin.py /usr/local/bin/django-admin.py
after that change permission of the django-admin to be executable
sudo chmod +x /usr/local/bin/django-admin.py
Now you can use
django-admin.py startproject mysite
to create your django project
If you want to change django-admin.py to django-admin to look like more compact you can use
sudo mv /usr/local/bin/django-admin.py /usr/local/bin/django-admin
Hope this help for you !
This simply worked for me.
Install django on your virtualenv:
pip install django
And then run:
django-admin startproject myprojectname
I was just having the same problem, I just did an upgrade and that's it, it worked, I hope this is useful for future problems in Linux:
pip install --upgrade django
There may be a chance that your path is not correct. Ubuntu has a .local/bin folder which pip uses to install module binaries and you need it in your path to use django or any shell commands installed using pip.
Open ~/.bashrc or ~/.zshrc
Add the following line and save it
export PATH="/home/animesh/.local/bin:$PATH"
restart the shell with source ~/.zshrc
To windows users out there: I have faced this problem several times and here are the checkpoints:
When there is problem initiating a new project, make sure:
1) python is working in the command line (type in python and see if you get the console)
2) specify the full path of django-admin.py in the command
3) check django-admin.py is in the system path
4) cd the command line path to where you want the new project
Screenshot of what finally worked for me (only the last command):
https://flic.kr/p/r9LJ67
(stackoverflow doesn't allow me to post pictures yet)
If you come across command not found: django-admin.py problem which means you don't installed django frame work. You should install the framework using pip.
pip install django
After that look at the directory if the related script exist or not.
Look into C:\Python27\Scriptsfolder to check for django-admin.py exist or not.
if you install django by pip
ensure you have installed django:
pip list or pip freeze
if there is django
then
get location of django:
pip show django
if location is '/Users/xxxxx/Library/Python/2.7/lib/python/site-packages' then
relocate django-admin-py to /usr/local/bin:
ln -s /Users/xxxxx/Library/Python/2.7/lib/python/site-packages/django/bin/django-admin.py /usr/local/bin/django-admin.py
Get site-package path with Python:
import site; site.getsitepackages()
And run django-admin.py directly:
python (your-site-package-addresss)/django/bin/django-admin.py startproject hellodjango
On Mac: If this works you can go and add django-admin.py to your path using symlink:
sudo ln -s (your-site-package-addresss)/django/bin/django-admin.py /usr/local/bin/django-admin
(could be that you have to reopen terminal or reinstall django to get the symlink working)
I changed my complete python path to desktop and tried and it's working well for me
In my case i simply forgot to run pip install django
On RHEL stock python config:
django-admin.py startproject mysite
I'm running macOS and I'm using pyenv instead of virtualenv. I'm not sure if they behave similarly, but I was having the same problem in which django-admin.py was not found.
After a while I've noticed that I had a warning after installing django:
pyenv: cannot rehash: /Users/msvolenski/.pyenv/shims/.pyenv-shim exists
Once I deleted this file and ran pyenv rehash it all started working perfectly.
Hope this helps!
For Windows Users first search for django-admin, right click on the file that has been found and open file location and keep it open.
Using Windows Powershell, cd into the the folder where you want to create your django project
when your in the right folder write the full path of where django-admin is located in my case I am using Anaconda 3 so the file location is
C:\Users\Sen\Anaconda3\Scripts
so in Windows PowerShell type C:\Users\Sen\Anaconda3\Scripts\django-admin.py startproject [name of project]
hope this helps!
I had the same issue when migrating to AWS Beanstalk it was installed and everything but i noticed the alias was not working but when i called the entire thing path and all it worked so i just rebooted the boxes and it worked i think the alias list is not updated automatically after you install.
It has to do with the PATH:
Put this in the .bash_profiel and the source it (for mac users only):
(change the location with the location of your installed python libraries)
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
If someone is facing the same problem, and is on MacOs, here is what I did, and it worked for me:
If you've installed python directly from the official website, uninstall it, and install it once again using brew:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install python3
This will also install the pip3 for you, so you don't have to install it by yourself.
I was facing the same issue.
The issue resolved after I upgraded the django version using
pip install --upgrade django
Then run
django-admin startproject mysite
Hope this helps!
There was a space in one of the names in the path to my project. I set up a new virtual environment in a new directory and did all the same things and it works.
Sometimes it is the simple things...
Since you're just starting out. It is very important to adopt best practices. You will face many dependency related issues with this approach of development. In this case it is always recommended to work with a virtual environment for each python project.
This will ensure fresh installation of project-specific dependencies that do not overlap with what the system you are running on already has.
If you have not already noticed, you will come across more issues such as python3 not working with earlier versions of django. pip will skip install as it checks and finds a version of django already installed. So this cannot be stressed enough, always use a virtual environment for local setups.
to do so:
cd [your project path]
virtualenv venv
you can active your environment by :
source ./venv/bin/active
install your requirements packages with pip :
pip install -r
or pip install
you can also install your requirements modules without activate the environment
./venv/bin/pip install
to run your python script use :
python <.py file>
and if you didn't activate your env use :
./venv/bin/python <.py file>

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