ImportError when using virtualenv in Django - python

My Question is pretty Simple.
Whenever I use virtualenv environment in my Django project it gives the following error when executed this command
python manage.py runserver
File "manage.py", line 8, in
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
But when i use root and give the same command it seems to work perfectly. Why does this Happen? I am in the virtualenv so it means i am the root person by default.How can i fix this error?
I had seen many forums but couldn't find related to mine.

This happens because you have installed django in the system-wide Python interpreter; possibly by doing something like sudo pip install django.
Once you create a virtual environment and activate it - it contains no packages. The concept of a virtual environment is that it allows you to install Python packages without affecting the global Python installation.
So once you activate a virtual environment, you have to install packages in that virtual environment; so you should pip install django (note: without sudo) once you activate the virtual environment; like this:
$ virtualenv sample_env
...
$ source sample_env/bin/activate
(sample_env) $ pip install django
A virtual environment is a virtual environment for Python; it does not control what the user is that is logged into the system.
Finally, as a general rule - you should not be using root for development purposes as doing so can easily compromise your system.

Yup i have same error while running Django. I also have enabled virtual environment. But I was still getting error.
Solution to this problem is using this command to install any python package
python -m pip install django
This will definitely solve your problem. As it solved mine.

Related

Module installs but doesn't import

I'm on an OS Catalina and I'm trying to install and run Mephisto (see https://github.com/facebookresearch/mephisto/blob/master/docs/quickstart.md). I created a python3 virtual environment and then went to the directory and ran
sudo pip3 install -e .
This seems to have run fine as I can now run mephisto and see the list of commands and options. However when I run mephisto register mturk it throws No module named 'mephisto.core.argparse_parser' because of an import statement in the python file. This seems like a general issue of a module installing but not importing the module properly, but would appreciate help in how to fix it. Is it because my $PYTHONPATH is currently empty?
Mephisto Lead here! This seems to have been a case of unfortunate timing, as we were in the midst of a refactor there and some code got pushed to master that should've received more scrutiny. We'll be moving to stable releases via PyPI in the near future to prevent things like this!
I created a python3 virtual environment and then went to the directory and ran
sudo pip3 install -e .
You should not have used sudo to install this library, if you meant to install it in a virtual environment. By using sudo the library probably got installed in the global environment (not in the virtual environment).
Typically:
create a virtual environment:
python3 -m venv path/to/venv
install tools and libraries in this environment with:
path/to/venv/bin/python -m pip install Mephisto
use python in the virtual environment:
path/to/venv/bin/python -c 'import mephisto'
use a tool in the virtual environment:
path/to/venv/bin/mephisto
Is it because my $PYTHONPATH is currently empty?
Forget PYTHONPATH. Basically one should never have to modify this environment variable (this is almost always ill-informed advice to get PYTHONPATH involved).
Check the __init__.py file is in the module's file directory. If not try creating an empty one.

How to start a new project in django using virtualenv

I just installed virtualenv and in it I installed django. However, when I go to the django-admin terminal in the bin file, I wrote
django-admin startproject mysite
I thought that would start a new project but it just returned
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
Here is how to start a new django project in a virtualenv:
1. Create a new virtualenv for your project:
virtualenv py_env --python=python3
--python=python3 is not mandatory. I'd recommend programming in python3.x but this is up to you. If you are unsure about what is the default python that will be used when omitting the --python option, type python -V in your terminal.
2. Activate the virtualenv:
source py_env/bin/activate
If you see a (py_env) at the beginning of the command line, then you know the virtualenv is activated. To deactivate, simply type deactivate.
3. Install the required packages:
pip install django
While this is not needed, I recommend using ipython, so you might want to run pip install ipython.
4. Create a new django project:
django-admin startproject mysite
Hope that helped and happy coding!
I think you should first make a virtual environment:
pip3 install --user pipenv
Make a virtual environment:
pipenv --python 3.6
Activate the environment:
pipenv shell
and after this do whatever you want i think it would work better now
Pipenv is best to use because it brings the best Packages of python so that there may not be further bugs while making the virtual environment. And django can be replicated properly.

Working with django-pagedown

I am following the instructions on the official Django-pagedown repo
pip install django-pagedown - installed without errors
Add pagedown to your INSTALLED_APPS - error thrown is "ModuleNotFoundError: No module named 'pagedown'
3.Collectstatic run smoothly.
When I check the modules currently in my virtual env using help('modules'), pagedown was not present, but in my global installation of python, it was present, so my question is, why can't my project work/use the present installed django-pagedown installation? what am I missing here?
python - 3.6.4
django version - (2, 0, 4, 'final', 0)
pip version - 10.0.1
os - windows
The package is actually installed in your computer, as you can see it presents in the global installation. Just because you didn't activate the virtual environment that you use.
Go to your project, activate the virtual environment (scripts\activate).
Make sure that you see the name of your virtualenv shown before the path like that (env_name)C:\Users\....
Then run pip install django-pagedown.
I figured out the problem and it was pip, somehow while running the command pip install django-pagedown in my IDE, (and running in my virtual env), it was installing pagedown globally and missing in my virtual env, so, I just changed IDEs and that's it. Thanks all for helping me out.
If the package is installed globally, and the virtualenv is setup such that it doesnt look into your global packages, then this issue can occur.
You need to either install the package using the pip command after activating the virtualenv or maybe run the project without the virtualenv ( in this case you will also need to have other dependencies to be installed globally)

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.

Django installation using Virtualenv gone wrong

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

Categories

Resources