Docker : python3 manage.py runserver error [duplicate] - python

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 :)

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

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.

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

Related

command not found: django-admin

I have downloaded anaconda and Django, but VS code shows
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?
so, I check out Get out of root conda environment and Problem with django after installing anaconda, installed django in virtual environment. Then follow https://docs.djangoproject.com/en/4.0/topics/install/. But I can't check out version by''' django-admin.py --version''' it shows command not found: django-admin
According to what you describe, I believe that you didn't activate the env or you don't have one in your directory, run this command py -m venv env && .\env\Scripts\activate then python -m pip install Django to create an env and activated and install Django if you have an env in your directory you need to activate using this .\env\Scripts\activate and then install Django finally run server Django using py manage.py runserver should works.
You're getting that error because you're calling django with the system's python and not the one inside your virtualenv.
Activate your virtualenv and then type:
python -m pip install django
That's it! Django will be recognized.
Also, when wanting to check django version, just simply do:
django-admin --version
without the .py piece
Remember to always call python inside your virtualenv. Let's say, like so:
python .\manage.py runserver

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.

Python (Windows Server) Virtualenv not recognizing Django is installed

My virtual environment refuses to recognize my install of Django (strangely)
I'm on Windows Server, installed Python 3.7 to a directory (C:\Python37) which I have C:\Python37;C:\Python37\Scripts in my windows Path so when using Powershell or GitBash I can use the python command.
if I run where python it shows the default install
I CD into my django project directory and run:
python virtualenv venv and the venv directory is created
Then I run source venv/Scripts/activate and it activates appropriately.
When I run where python it shows the exe inside the venv directory - which is expected and appropriate.
I run pip install -r requirements.txt and all my requirements install appropriately. I confirm they are installed with pip freeze (all installed correctly)
Once I do that I go to run python manage.py collectstatic (no migrations are required in this particular instance) I get an error message that Django isn't installed.
To check this, with my virtualenv still activated I enter the shell (python)
If I do import django it also says Django is not installed.
I cannot figure out what's happening here - the python version appears to be correct, the correct virtualenv is activated - but it's still not seeing the properly installed Django installation.
Thoughts? Ideas?
For what it's worth - here is the solution and explanation:
Everything works as expected as was outlined in the question - the reason that the actual python command wasn't working had to do with a line in the .bashrc file.
There was an alias in the .bashrc file to set python to the command winpty C:\Python37\python
So when the command python manage.py collectstatic was getting ran - it was looking at the Python executable in the Python37 directory and not the virtualenvironment Python.
This was solved by simply running the appropriate Python (e.g.)
C:/my_project/venv/Scripts/python manage.py collectstatic
This forced it to use the virtualenvironment python to run the command so everything worked as expected.

No module named django.core when running django-admin startproject myproject

when running django-admin startproject myproject on macOS I get the error
Traceback (most recent call last):
File "/usr/local/bin/django-admin", line 2, in
from django.core import management
ImportError: No module named django.core
I checked out this question but running import django won't produce any output in a python3 shell.
/usr/local/bin/django-admin is a symlink to /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/bin/django-admin.py.
I already put /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django in my PYTHONPATH as suggested in other questions.
Am I missing something?
Even if you make it work, it is not good practice to do what you're doing! Ideally, the only python-related binaries you would want in /usr/local/bin/ would be python, pip and virtualenv (or venv, pyvenv)...
I would suggest you to delete /Library/Frameworks/Python.framework/Versions/3.6 ONLY IF you installed it there. As far as I know, macOS only comes with python2.7 installed and not python3.6!
Then open a new shell and try this:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install python3
pip3 install virtualenv
cd ~/Desktop/
mkdir proj
cd proj
virtualenv -p python3 env
source env/bin/activate
pip install django
django-admin.py startproject testproj
skip the first step if you already have brew installed
Check your permissions in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ with an ls -la command. If you see anything owned by root, this probably needs to change. I suspect that since you installed some packages as root, the permissions are weird and it can't find the module. If this is the case, reinstall the package(s) as your own user using sudo.
Another thing you should check: as phd mentioned you need to make sure you're using the version of python you think you are. Check this by running which python to tell you the location of the one you're referencing, and python --version to tell you which version you're using. If it's not Python 3.6, then you installed Django for a different version of Python. In this case, simply install Django for version 3.6 and you'll be on your way.
For future reference, Python offers a module called venv to prevent version mishaps like this. More info can be found here.
It seems like you are trying to create or work on a django project without using a python virtual environment. I recommend reviewing the python 3 venv documenation (https://docs.python.org/3/library/venv.html). Then creating a virtual environment (venv) specifically for your web application. Once you you that project's venv setup you can install django into that venv.
On MacOS, use sudo before the command:
sudo django-admin startproject myproject

Installed Virtualenv and activating virtualenv doesn't work

I cloned my Django Project from Github Account and activated the virtualenv using famous command source nameofenv/bin/activate
And when I run python manage.py runserver
It gives me an error saying:
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 was thinking that every and each dependency I need, might be present inside virtualenv.
Well, no. By default, a newly created virtualenv comes empty, that is, with no third-party library. (Optionaly, you may allow a virtualenv to access libraries installed system-wide, but that's another story.)
Once the virtualenv is created, you need to install the dependencies you need.
(How could virtualenv know what dependencies you need?)
The procedure is to install the virtualenv, activate it, and then install the libraries needed for the project (in you case Django and perhaps others).
If you project has a requirements.txt, you may install every required dependency with the command:
pip install -r requirements.txt
If your project has a setup.py, you may also execute
pip install -e path/to/your/project/clone/.
to install the project in the virtualenv. This should install the dependencies.
Of course, if the only dependency is Django, you can just type
pip install django
on ubuntu version
#install python pip
sudo apt-get install python-pip
#install python virtualenv
sudo apt-get install python-virtualenv
# create virtual env
virtualenv myenv
#activate the virtualenv
. myenv/bin/activate
#install django inside virtualenv
pip install django
#create a new django project
django-admin.py startproject mysite
#enter to the folder of the new django project
cd mysite
#run the django project
python manage.py runserver
If you have several python on your machine, for example,python2.7, python3.4, python3.6, it is import to figure out which version the python really reference to, and more over, which version does pip reference to.
The same problem got in my way after I installed the let's encrypt when I run the following command.
(python3 manage.py runserver 0:8000 &)
I inspected the python version and found that python3, python3.4, python3.6, python3.4m were all available.
I just change python3 to python3.6 and solved the problem.
(python3.6 manage.py runserver 0:8000 &)
So, this is probably a version mismatching problem if it is OK for a long time and crashes down suddenly.
I'm guessing you also upload the virtual environment from your other pc. And you hope that only activating that will work, bzz.
It's not recommended to upload the virtualenv files to your git repository, as #Alain says it's a good practice to have a requirements.txt file containing the project dependencies. You can use pip freeze > requirements.txt (when the environment is activated) to generate the project requirements file.
By doing so, when you clone the repository from another computer, you need to create a new virtualenv by issuing the command:
virtualenv nameofenv
then activate it:
source nameofenv/bin/activate
and finally, use the requirements file to install the requirements for your project using:
pip install -r requirements.txt
I had installed Django 2 via pip3 install Django, but I was running python manage.py runserver instead of python3 manage.py runserver. Django 2 only works with python 3+.

Categories