Why Django doesn't want to create startapp? - python

msinfo32#dEfaaPc2:~/Django/malybar$ python manage.py startapp testApp1
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
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?
What i should to do?

To begin using the virtual environment, it needs to be activated
source venv_path/bin/activate
this will help you
for more details this will help you

Simple way is to make a virtual environment first & activate it and then make your project and App in its enviroment.
Follow the Following Steps to go through a less error routine:
Python -m venv newms
source newms/bin/activate
pip install django
django-admin startproject projectName
cd projectName Then python manage.py startapp appname
Here you're basically creating virtual env named newms and activating it then install django and then you can go ahead and make your project and apps inside it.

Related

Creating a Python Virtual environement with the command python3 -m venv venv points to the wrong site packages directory

OS=Pop_OS 22.04 Python version 3.10.6
I am having problems with the path to the virtual environment when I use the python -m venv command. These are the commands I am using to test the problem:
cd
mkdir testenv
cd testenv
python3 -m venv venv
source venv/bin/activate
pip install django
django-admin startproject website .
python manage.py runserver
I kept it simple on purpose and on the virtual machine it works, on my workstation it gives the error.
On my workstation after creating the virtual environment, I use pip to install django and run start project. This has worked many times. After I updated something in my python 3.10.6 system installation, django and other pip installed packages fail to import.
So I installed a virtualbox version of my pop os and followed the same sequence of steps. Django on the virtual machine worked just like it always used to do on my workstation.
Searching the internet and stack overflow I got the advice about not moving the venv file once created. I had not moved the project folder I was working on, but still I created a testenv folder and created the virtual environment on my workstation. Django still failed to start.
Here is the error
Traceback (most recent call last):
File "/home/echeadle/testenv/manage.py", line 11, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/echeadle/testenv/manage.py", line 22, in <module>
main()
File "/home/echeadle/testenv/manage.py", line 13, in main
raise ImportError(
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?
The only difference i can see is when I run python -m site.
This is my workstation that is failing:
sys.path = [
'/home/echeadle/testenv',
'/usr/lib/python310.zip',
'/usr/lib/python3.10',
'/usr/lib/python3.10/lib-dynload',
'/usr/local/lib/python3.10/dist-packages',
'/usr/lib/python3/dist-packages',
]
USER_BASE: '/home/echeadle/.local' (exists)
USER_SITE: '/home/echeadle/.local/lib/python3.10/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
This is the result of the same command (python -m site) on the system that is working:
sys.path = [
'/home/echeadle/testenv',
'/usr/lib/python310.zip',
'/usr/lib/python3.10',
'/usr/lib/python3.10/lib-dynload',
'/home/echeadle/testenv/venv/lib/python3.10/site-packages',
]
USER_BASE: '/home/echeadle/.local' (exists)
USER_SITE: '/home/echeadle/.local/lib/python3.10/site-packages' (doesn't exist)
ENABLE_USER_SITE: False
On my workstation, I ran the shell, imported site and looked around, but I cannot find information about how the python -m venv command uses the site function. All I know is that on my workstation this is the only thing I have found that might be an issue.
See comments below, but a temporary fix is:
cd
mkdir testenv
cd testenv
export PYTHONPATH='home/echeadle/testenv/venv/lib/python3.10/site-packages'
python3 -m venv venv
source venv/bin/activate
pip install django
django-admin startproject website .
python manage.py runserver
If you export the PYTHONPATH before you create the virtual environment, the fix for the life of the venv is permanent.
I found the answer to the problem I submitted where python -m venv returns an incorrect path the virtual environment's site-packages directory, on jetbrains website.
The problem was, in my .bash_aliases I had alias python="/usr/bin/python3"
This was the problem. The explanation is found at the link below.
Fix for incorrect path to site-packages

When I am trying to use django-newsletter, I have error while trying to install according to docs [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.

ImportError: Couldn't import Django ... Did you forget to activate a virtual environment?

I know there are several questions/answers about this, but I can;t figure out what I should do.
I wanted to get started with Django and installed it with pip install and added Python37 and Python37-32 to my environmental variables, and I guess it worked, because I can run several Python commands in my shell.
But every time I try to
python manage.py runserver
it gives me an error.
I also set up my virtual environment and activated it, but I think there's a problem with Django. But because I installed it with pip install django I know it's there and I can use commands like django-admin startapp ... So I guess Django is working. I don't really know what PYTHONPATH means and where to find it. It would be pretty nice if anybody could take a look at my error.
Here you can see that Django is installed :
#
**C:\Users\Kampet\Desktop\Python-Django\mysite>pip install django Requirement already satisfied: django in c:\users\kampet\appdata\local\programs\ python\python37-32\lib\site-packages (2.2.4) Requirement already satisfied: pytz in c:\users\kampet\appdata\local\programs\py thon\python37-32\lib\site-packages (from django) (2019.2) Requirement already satisfied: sqlparse in c:\users\kampet\appdata\local\program s\python\python37-32\lib\site-packages (from django) (0.3.0)**
# And thats my error
**C:\Users\Kampet\Desktop\Python-Django\mysite>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 16, in main
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available o
n your PYTHONPATH environment variable? Did you forget to activate a virtual env
ironment?**
###################
Here is where my virtual environment is located.
Python-Django
-----------------mysite
-------------------------main
-------------------------mysite
-------------------------manage.py
-----------------venv
-------------------------Include
-------------------------Lib
-------------------------Scripts
-------------------------pyvenv.cfg
This is my manage.py:
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def 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)
if __name__ == '__main__':
main()
#
I don't know why it can't find the module "django" / django.core.management
I also can't find django.core.management anywhere in my files, but I reinstalled and upgraded django several times. I don't know if this helps you.
Thank you for your time.
On the Windows machine, you should activate venv by this command .\venv\Scripts\activate (please note, you should be in the folder where this venv is)
Then inside activated venv install Django pip install django and in the same terminal run the server python manage.py runserver
I had similar issue in my windows 10 system and solved using pipenv. Steps with commands given below.
cd/Go to the project folder
Setup the virtual environment pipenv install
Activate the virtual environment pipenv shell
Install django pip3 install django
Run the project pipenv run python manage.py runserver
I had similar issue with:
django installed globally
virtual environment setup and activated
django project setup under venv activated
When trying to run the project, it could not import django anyway.
Adding path to manage.py in variables did not help but as mentioned above, installation of django under venv activated actually resolved the issue.
Your case may be different but try this scenario:
Setup venv
Activate venv
Install django
Create django proj
Try to run django proj
try running it in anaconda prompt instead of cmd, that worked for me
This error occurs when you install pip packages as a normal user other than root user. pip install <package name> --user command actually creates a directory called as .local in the users home directory under which the packages are installed.
Fix:
Find the django package installed location sudo find / -name 'django'
Copy the path from find command output.
Create export variable in ~/.bashrc file with PYTHONPATH variable.
Save the file and exit.
Exit from the normal user prompt.
Login again as the normal user.
Run the command env to check the PYTHONPATH variable set in the output.
Run the command python manage.py runserver <ip address: port>
Make sure that you are working on the command prompt.
Use this command '
workon your_env_name
'
Install Python3, Make sure its on PATH, make sure PIP is installed, Install virtualenv with PIP, setup virtualenv, activate virtualenv, install django.
Or you can try to run the command pipenv shell before start the server with py manage.py runserver in vscode terminal.
In my case, I reinstalled python to a global folder on the C drive and by mistake wrote PYTHONPATH With:\Python and not C:\Python\Scripts in global variables, but in local variables it was written correctly
enter image description here
On Windows, you should activate env by this command .\env\Scripts\activate (please note, you should be in the folder where this env is)
Then inside activated env install Django: pip install django and in the same terminal run the server: python manage.py runserver

Run django-admin

I'm new to django and I would like to follow this tutorial: https://docs.djangoproject.com/en/2.1/intro/tutorial01/
Unfortunately, django-admin is not in my path.
When I try to run the django-admin.py script directly, I have the following error:
$ /usr/local/lib/python3.7/site-packages/django/bin/django-admin.py
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/bin/django-admin.py", line 2, in <module>
from django.core import management
ImportError: No module named django.core
Here is my configuration:
System: macOS 10.13
Python: 3.7.0 (installed via Homebrew
Django: 2.1.4
What am I doing wrong?
I think You don’t install django correctly. If you are highly new in django then follow these steps to setup your clean and new project environment
Create your project folder
mkdir myProject
Open this Project Folder
cd myProject
Create virtual environment
python3 -m venv env
Now, active this virtual environment
source env/bin/activate
It’s time to install the Django
pip install django
Create your first project
django-admin startproject myProject
Open this Project
cd myProject
Open your server to check it either it’s work successfully or not
python manage.py runserver
First check if django is installed by running python -c "import django". If that is successful check if django-admin command is in your python path. If you are using a virtual environment check under virtual environment/bin/. If there is no file such as django-admin then the first solution would be to try reinstalling django. Same case if you are working outside a virtual environment.
If none of the above solves it then check if you have both python 2 and 3. If you do check whether .py files are being associated to python 2 instead of python3.

Could'nt import Django

I started my django project without activating my virtualenv django-admin startproject my_project and django-admin startapp my_app. Everythiing went fine, till i closed my terminal, and stopped the serveer. I wanted to restart my server, but this message is still coming up. i tried to install the virtualenv still the same problem,
File "./manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
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?
Please what could be the problem and how to fix this ??
You should run pip install django at terminal after active the virtualenv.

Categories