Can't create django app in Conda environment. - django

I have created a conda environment by typing the following conda create --name testenv python command from here.
Now one of my tutorials tells me to install Django which I happily did in the environment(That is the point of using environments right?, keeping dependencies straight) using pip3 install django.Post which I was told to do this django-admin startproject mysite Which didn't work. I am thinking because he was doing it in venv and I am in conda probably that's why(There is no venv folder in my test-app folder as well. Also I have already activated the environment). Every time I type the command django-admin startapp mysite I get this error.
Traceback (most recent call last):
File "/usr/bin/django-admin", line 18, in <module>
from django.core import management
ImportError: No module named 'django'
How do I fix this? I have already installed Django. What more am I supposed to do?

This question is old, but if anyone is wondering the exact steps to create a Django application using conda, here it is
conda create -n <nameoftheapplication> python=3.6
source activate <nameoftheapplication> Note: For Windows just put activate
pip install django
django-admin.py startproject <nameoftheapplication>
cd <nameoftheapplication>
ls
You should see manage.py and a folder called <nameoftheapplication> which contains settings files.

Firstly, open the anaconda prompt in windows then go to the project directory and enter the following command:
conda create -n python=3.6 anaconda
activate
pip install django
django-admin.py startproject
cd
python manage.py runserver
Then open web browser and enter the http://127.0.0.1:8000/ to see django page.

Related

django-admin pointing outside virtualenv

I've initialised a virtual environment using the virtualenvwrapper command mkvirtualenv -a <path to project> django_project.
I then installed django with pip install django. But then if i try to use django-admin i get:
Traceback (most recent call last):
File "/usr/local/bin/django-admin", line 7, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
Now pip list gives me
Package Version
---------- -------
Django 2.1.3
pip 18.1
pytz 2018.7
setuptools 40.6.2
wheel 0.32.3
python -m django --version gives
2.1.3
If I run which python it correctly points to my virtualenv, however which django-admin gives:
/usr/local/bin/django-admin
I'd think that it should point to my venv. Why would it point to a global django admin? How do I fix it so that it'll work for my future virtual environments?
I'm on MacOS using zsh and python 3.7.0.
Thank you!
Edit: Mistake in a command
Edit: I realised I don't have a system-wide installation of Django and so the django-admin and django-admin.py files in my /usr/local/bin must've been leftovers from an earlier installation. Hence I deleted them and that solved the problem. Without any further django-admin inside the venv point to the correct django installation (inside the venv).
However, I would still like to know why the command didn't point to the Django installed in the venv in the first place?
So Django has been installed at system level, while you verified that python command refer to your virtual environment. I bet this is an issue with pip. You may check that it is under <path to project>/bin and is correctly used when you perform
(django_project) $ pip install django
Try to run
which pip
with your venv enabled and disabled to see what pip is used in each case
I had the same problem, but after reboot it solved the issue for me.
Don't forget to check if the executable django-admin is inside the virtualenv's bin folder:
~/Documents/Django_python/django/bin $ ls
activate deactivate.nu pip3.10 wheel
activate.fish *django-admin* python wheel3
activate.nu pip python3 wheel3.10
activate.ps1 pip3 python3.10 wheel-3.10activate.csh
activate_this.py pip-3.10 sqlformat
(*) Highlight mine.

How to create a django project in ubuntu

I'm following a tutorial on Django and I'm suppose to create a folder in Ubuntu in terminal
$ django-admin startproject mysite
This above line shows
"Cannot find installed version of python-django or python3-django."
After installing all the required stuffs.
The recommended way of starting a django project in Ubuntu is using a virtualenv so first install it.
Then run
virtualenv -p python3 env
This will create an env named folder. Active the virtualenv by running
source ./env/bin/activate
Then install django with pip. now django will be installed in activated virtualenv
pip install django
and then create your project
django-admin startproject mysite

ImportError: Couldn't import Django

I've already configured virtualenv in pycharm, when using the python manage.py command, this is error shown:
E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
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?
How should I fix it, I've installed django.
I think the best way to use django is with virtualenv it's safe and you can install many apps in virtualenv which does not affect any outer space of the system
vitualenv uses the default version of python which is same as in your system
to install virtualenv
sudo pip install virtualenv
or for python3
sudo pip3 install virtualenv
and then in your dir
mkdir ~/newproject
cd ~/newproject
Now, create a virtual environment within the project directory by typing
virtualenv newenv
To install packages into the isolated environment, you must activate it by typing:
source newenv/bin/activate
now install here with
pip install django
You can verify the installation by typing:
django-admin --version
To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:
deactivate
When you install Django on your computer all things go fine but when you install a Virtual environment it gets separated from all things. You will know it's importance when you will make a final project and deploy it to any cloud or hosting.
Just reinstall Django in the virtual environment and baam:
pip install Django
and then just run the command for testing:
python manage.py runsever
and you are all done.
You need to install Django, this error is giving because django is not installed.
pip install django
You need to use both commands:
pip install django and pip3 install django
that worked for me
Check that you have installed Django; by executing import django in python.
you mustn't see ModuleNotFoundError if everything's ok.
Check that you have installed virtualenv; by executing virtualenv --version.
you must see the version number if everything's ok.
Check that you have enabled virtualenv; there's got to be the name of your virtualenv in your command prompt starting line. enable it by
source bin/activate. also, remember to deactivate it every time your job is
finished with the virtualenv.
Check that your virtualenv includes django. a virtualenv by default
has no modules installed. you either have to install django in your
virtualenv (even if you have it in your machine already) or use
virtualenv --system-site-packages when creating a virtualenv to
include system site packages in the virtualenv.
Add django to your path. open python, import django, then run
django to see django's path. then add it to your ~/.bashrc (or
~/.zshrc if you're using zsh). more info in here
Install django-admin by running pip install django-admin
find your django parent dir path and add it to PYTHONPATH
In my case, my django parent dir path is /Library/Python/3.7/site-packages,add this line into ~/.bash_profile
export PYTHONPATH=/Library/Python/3.7/site-packages
else if you have PYTHONPATH already, just append it like this
export PYTHONPATH=${PYTHONPATH}:/Library/Python/3.7/site-packages
then
source ~/.bash_profile
I was having great difficulties with this but I have solved my issue. I am on Windows 10 using Vagrant ssh in my virtualenv environment, the box I have installed is ubuntu/xenial64, Django version 2.1, python==3.6.
When I was installing packages I was using pip3 but most importantly I was using sudo and the -H flag to install these packages. When I ran sudo pip3 freeze my packages would come up, but when I ran a plain pip3 freeze there would be no packages.
Then I tried the python3 manage.py startapp <YOUR APP NAME> and it did not work same error as you.
I finally thought to try sudo python3 manage.py startapp <YOUR APP NAME> it finally worked!
Hope this was help :)
I faced the same issue, and in my case it was because I had multiple python versions on my machine, in addition to the Anaconda ones.
In my case django didn't worked well with my anaconda python.
I knew that when I run import django on each python terminal for all versions I have.
As a summary here are the steps I made to get this solved:
Run the CMD as Admin
Create a project folder.
Create a new ENV for this new project INSIDE THE PROJECT Folder...
pip install virtualenv >> virtualenv new_env`
Activate it:
.\new_env\Scripts\activate`
After the env activation ⇒ Install Django:
python -m pip install Django
The python version you used here in step 5 will determine which python will to work with this installed Django.
If you are working on a machine where it doesn't have permissions to all the files and moreover you have two versions such as default 2.7 & latest 3.6 then while running the command use the python version with the command. If the latest python is installed with sudo then run the command with sudo.
exp:
sudo python3.6 manage.py runserver
after activating virtual env that error raises up on ubuntu.
and I solve this issue just by typing again :
pip3 install Django
inside the directory which is I want to create a new app.
You can use python3 to run file, if you don't want to use virtualenv.python3 manage.py runserver
To install python3 look at this page
Make sure you have Django installed by writing this command :
python -m django --version
if it's not installed you can install it by writing this command :
pip install django
I solved this problem in a completely different way.
Package installer = Conda (Miniconda)
List of available envs = base, djenv(Django environment created for keeping project related modules).
When I was using the command line to activate the djenv using conda activate djenv, the base environment was already activated. I did not notice that and when djenv was activated, (djenv) was being displayed at the beginning of the prompt on the command line. When i tired executing , python manage.py migrate, this happened.
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?
I deactivated the current environment, i.e conda deactivate. This deactivated djenv. Then, i deactivated the base environment.
After that, I again activated djenv. And the command worked like a charm!!
If someone is facing a similar issue, I hope you should consider trying this as well. Maybe it helps.
Instead of creating a new virtual environment, you just have to access to your initially created virtual environment when you started the project.
You just have to do the following in your command line:
1)pipenv shell to access the backend virtual environment that you have initially created.
2) Then, python manage.py runserver
Let me know if it works for you or not.
To create a virtual environment for your project, open a new command prompt, navigate to the folder where you want to create your project and then enter the following:
py -m venv project-name
This will create a folder called ‘project-name’ if it does not already exist and setup the virtual environment. To activate the environment, run:
project-name\Scripts\activate.bat**
The virtual environment will be activated and you’ll see “(project-name)” next to the command prompt to designate that. Each time you start a new command prompt, you’ll need to activate the environment again.
Install Django
Django can be installed easily using pip within your virtual environment.
In the command prompt, ensure your virtual environment is active, and execute the following command:
py -m pip install Django
In case you have virtual env activated, django installed, django-admin --version prints the valid version - check if there is no circular import in the file you are executing.
I faced the same problem when I was doing it on windows 10. The problem could be that the path is not defined for manage.py in the environment variables. I did the following steps and it worked out for me!
Go to Start menu and search for manage.py.
Right click on it and select "copy full path".
Go to your "My Computer" or "This PC".
Right click and select "Properties".
Select Advanced settings.
Select "Environment Variables."
In the lower window, find "Path", click on it and click edit.
Finally, click on "Add New".
Paste the copied path with CTRL-V.
Click OK and then restart you CMD with Administrator privileges.
I really hope it works!
Looks like you have not activated your virtualenv when using the runserver command.
Windows: <virtualenv dir>\Scripts\activate.bat
Linux: source <virtualenv dir>\bin\activate
You should see (name of virtualenv) as a prefix to your current directory:
(virtualenv) E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
windows :
(virtualenv dir)\Scripts\activate # this step to activate virtualenv
you should be in the dir of (project name)
python manage.py runserver
you need to go to the root directory
and run the below command
source bin/activate
Once the above command is executed, you will be able to create custom apps
I also face the same problem in windows 10 with anaconda
For me anaconda3\Scripts>activate
it's working good. What you have to do you just need to go to anaconda home
AppData\Local\Continuum\anaconda3\Scripts
and you need to open a cmd prompt and type activate.
It will activate the venv for you.
if you don't want to deactivate or activate the already installed venv just ensure you have set the pythonpath set
set pythonpath=C:\software\venv\include;C:\software\venv\lib;C:\software\venv\scripts;C:\software\venv\tcl;C:\software\venv\Lib\site-packages;
and then execute
"%pythonpath%" %venvpath%Scripts\mytestsite\manage.py runserver "%ipaddress%":8000
The problem is related to this error: Execution Policy Change
Start virtualenv by running the following command:
Command Line
C: \ Users \ Name \ yourdjangofilesname > myvenv \ Scripts \ activate
NOTE: On Windows 10, you may receive an error by Windows PowerShell that the implementation of these scenarios is disabled on this system. In this case, open another Windows PowerShell with the "Run as Administrator" option. After that, try typing the following commands before starting your virtual environment:
C:\WINDOWS\system32> set-executionpolicy remotesigned
Execution Policy Change:
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170.
Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): A
After selection Y(es), close the Powershell admin window, and then go back to the Powershell Window(where you got the error) and run the command again.
> myenv\Scripts\activate and then python manage.py runserver 8085 ,
(8085 or any number if you want to change its default port to work on otherwise you dont need to point out anything. )
I had same problem, I installed all dependencies with root access :
In your case:
sudo pip install django
In my case, I had all dependencies in requirements.txt, So:
sudo pip3 install -r requirements.txt
Just sync your pipenv environment with:
pipenv sync
I had this problem with Django 3.
On manage.py detail the execute_from_command_line import.
You should have:
from django.core.management import execute_from_command_line
Instead of
from django import execute_from_command_line
I had the same problem and my solution was not posted here:
How I got the error
My error came whenever I was installation the requirements.txt file with pip (let's say from cloning a git repository).
Solution
I manually installed each of the modules in the requirements.txt + any other module needed for the installation of those modules (e.g: I got errors and some modules where missing to install other modules so I had to add them too).
If there is anyone who faced with the same problem when using virtual environment and running on MacOS, just try
sudo python manage.py startapp <project_name>
instead of
python manage.py startapp <project_name>
It will solve the problem suprisingly!
I had to install django using the virtual environment pip3 executable directly:
cd [virtual environment folder]/bin
sudo ./pip3 install django
If you already installed Django / configured virtualenv and you still having the error:
ImportError: Couldn't import Django. Are you sure it's installed and
available on your PYTHONPATH environment variable?
Try to run the command pipenv shell before start the server with py manage.py runserver

ImprtError in Django

When I try to run my projects server i get this error:
File "manage.py", line 14, in <module>
import django
File "C:/.../.../.../..../..."
from django.utils.version import get_version
ImportError: No module named utils.version
Help, Please.
It is definitely a PATH issue. This may happen if you've installed Django as one user and trying to run your django web app as another one. The best way is to use virtualenv as xyres mentioned in the comments above.
Install virtualenv from cmd.exe, run it as an Administrator
pip install virtualenv
Create your own virtual environment
virtualenv .venv
Activate virtual environment on Windows
.venv/Scripts/activate.bat
Or if you're using Linux
source .venv/bin/activate
Now you can install dependencies with pip as a part of your virtual environment
pip install -r requirements.txt
pip install django
You can exit virtual environment at any moment by typing
deactivate

Running Django in Virtualenv on EC2 -- ImportError: No module named django.core.management

I developed a django application locally, in a git repo. I launched an EC2 instance for the project and I set up a virtualenv with (what I believe to be) the correct packages/dependencies. I then proceeded to clone my repo into the virtualenv. Right now, I'm having difficulty as I'm receiving the following errors:
I attempted to use python manage.py runserver example.com/8080 to test. I was sure to activate the virtualenv using source bin/activate, just like I did in my local virtualenv. When I call ... runserver I get the following error:
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
Here is what sudo pip freeze produces:
Warning: cannot find svn location for distribute==0.6.24dev-r0
Cheetah==2.4.4
Django==1.5.2
Fabric==1.8.0
GnuPGInterface==0.3.2
Landscape-Client==12.05
M2Crypto==0.21.1
PAM==0.4.2
PyYAML==3.10
South==0.8.2
Twisted-Core==11.1.0
Twisted-Names==11.1.0
Twisted-Web==11.1.0
apt-xapian-index==0.44
argparse==1.2.1
boto==2.2.2
chardet==2.0.1
cloud-init==0.6.3
command-not-found==0.2.44
configobj==4.7.2
## FIXME: could not find svn URL in dependency_links for this package:
distribute==0.6.24dev-r0
django-s3-folder-storage==0.1
django-storages==1.1.8
django-tastypie==0.10.0
ecdsa==0.9
euca2ools==2.0.0
gunicorn==18.0
httplib2==0.7.2
keyring==0.9.2
language-selector==0.1
launchpadlib==1.9.12
lazr.restfulclient==0.12.0
lazr.uri==1.0.3
medusa==0.5.4
meld3==0.6.5
oauth==1.0.1
paramiko==1.12.0
psycopg2==2.5.1
pyOpenSSL==0.12
pycrypto==2.4.1
pycurl==7.19.0
pyserial==2.5
python-apt==0.8.3ubuntu7.1
python-dateutil==2.1
python-debian==0.1.21ubuntu1
simplejson==2.3.2
six==1.4.1
supervisor==3.0a8
ufw==0.31.1-1
unattended-upgrades==0.1
virtualenv==1.10.1
wadllib==1.3.0
wsgiref==0.1.2
zope.interface==3.6.1
...and this is my ./manage.py file:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Any thoughts on how I can fix this error? I tried to change #!/usr/bin/env python to #!/var/www/paletto-env/bin python, trying to direct it to my actual python path, but it did not lend to any apparent change, so I changed it back.
Thanks for the help.
Looks like you're environment can't find Django, even though it's clearly in your pip freeze.
Try opening a Python (not Django) shell from your virtual environment and entering:
import django
django.VERSION
If you run into the same error, there's probably an issue with your Django install. Your virtual environment probably can't find it. You could try modifying the path settings, or just reinstall Django.
If you can successfully import Django, or if you tried reinstalling and it doesn't work, you may have a permission problem. Ensure that the user responsible for running the server has access to wherever your python libraries are stored from the virtualenv.
I had pretty much the exact same problem as you and this is how I solved it (Disclaimer: I'm not sure if this is the absolute correct way but it worked for me and everything seems correct).
Short Answer:
Try pip install (package) instead of sudo pip install (package)
Long Answer:
I gave the Django complete install doc (See here) a quick read through and came across a bit that basically said you dont need super user privileges when using pip in the virtualenv. I just assumed that either would be fine but I now realize that's probably not the case.
I suspect that because I entered sudo pip install django it installed it somewhere above my local bin in my virtualenv for the project. I suspect this because when I enter python manage.py runserver I get an error; however, when I enter sudo python manage.py runserver everything functions properly.
Also, typing sudo pip freeze reveals my larger library whereas pip freeze reveals my local library for my virtualenv.
I didn't want to type sudo (do stuff) before everything and I wanted a nice, clean and proper virtualenv so I just reinstalled django but to my local virtual env with pip install django and now django is in the proper virtualenv library, pip freeze returns the proper contents, and python manage.py runserver functions properly!
django-admin.py startproject by default creates the shebang in manage.py with #!/usr/bin/env python.
If this is not the path to your python executable, or if you use python3, just edit the manage.py to reflect this.
Reinstall all python packages angain after you have activated your virtualenv.
Maybe the Django has lost some plugin with pip, so we can install Django with Tarball:
Go https://www.djangoproject.com/download/1.6.5/tarball/
Download Django-*.tar.gz
install it.
$ tar zxvf Django-1.6.5.tar.gz
$ cd Django-1.6.5/
$ python setup.py install
more ... (https://stackoverflow.com/a/24323774/686105