PyCharm 2018 Python Unresolved Reference 'django' VirtualEnv - python

I have the newest PyCharm version (2018) and the latest Django version (2.1) and Python (3.6)
When I want to import something in PyCharm from Django:
from django.urls import url
I get an error message under django stating:
Unresolved reference 'django'
I created a virtual environment and I ran a project on it, the most famous one (polls) and it ran very well.
But now I noticed that I have this error, what is the solution?
Thank you in advance.

The current virtual environment you are using does not have django installed in it. Installing it for the current environment or using a project interpreter which have django installed should resolve the issue.
1. Installing Django on your current virtual environment via pycharm settings.
Go to Settings > Project > Project Interpreter and click the
green plus sign on the right hand side (install option).
Search for django in the search bar of the available packages dialog
and select it.
Select the install package option at the bottom.
2. Installing Django on your current virtual environment via pycharm terminal.
Click on the pycharm terminal and install django using
pip install Django
3. Changing the current project Interpreter.
Go to Settings > Project > Project Interpreter and click on the project interpreter drop down menu.
Choose a project interpreter which have django installed.

I had this problem specifically with a tutorial that uses an SRC folder. I learned that the SRC folder needs to be marked as a "sources root" in Pycharm. Once done, then the error flagging goes away.
Try Django Tutorial

I had this problem but none of the answers above and in other posts worked. I use the right interpreter and Django is well installed (in a venv).
I restored default settings with File > Manage IDE settings and this corrected the error.
I suppose the last update of PyCharm went wrong and this restarted everything well.
/!\ As #Vegas precised it in the comment, all configurations will disappear

Related

How to I fix the “ModuleNotFound” error when importing PyQt5? [duplicate]

I installed bottle on Python 3.4 with pip install. In the terminal, when I do:
$ python3.4
>>>import bottle # shows no import error
>>>
but when I do it in PyCharm, it says:
import bottle ImportError: No module named 'bottle'
in your PyCharm project:
press Ctrl+Alt+s to open the settings
on the left column, select Project Interpreter
on the top right there is a list of python binaries found on your system, pick the right one
eventually click the + button to install additional python modules
validate
In some cases no "No module ..." can appear even on local files. In such cases you just need to mark appropriate directories as "source directories":
The settings are changed for PyCharm 5+.
Go to File > Default Settings
In left sidebar, click Default Project > Project Interpreter
At bottom of window, click + to install or - to uninstall.
If we click +, a new window opens where we can decrease the results by entering the package name/keyword.
Install the package.
Go to File > Invalidate caches/restart and click Invalidate and Restart to apply changes and restart PyCharm.
Settings:
Install package:
I am using Ubuntu 16.04. For me, it was the incorrect interpreter, which was by default using the virtual interpreter from the project.
So, make sure you select the correct one, as the pip install will install the package to the system Python interpreter.
PyCharm 2019.3, my solution is below:
For me, none of the above worked, and curiously even within one file some imports worked, some didn't:
from folder1.folder2.folder3.my_python_file import this_function # worked
from folder1.folder2.folder3.my_python_file import that_function # didn't work
Follow the above advice, but if it doesn't fix it additionally, (in PyCharm) click File >> Repair IDE and confirm all the 6 steps one after another.
I had virtual env site package problem and this solved it:
In the case where you are able to import the module when using the CLI interpreter but not in PyCharm, make sure your project interpreter in PyCharm is set to an actual interpreter (eg. /usr/bin/python2.7) and not venv (~/PycharmProject/venv/...)
I had the same problem, I tried all fixes like installing from the project interpreter and installing from python console, nothing worked. What worked was just going to the project folder from the terminal and installing it from there.

pycharm: says package not installed but server runs fine from terminal without any error

I have a Django project. I have installed django-extensions from terminal and added django-extensions to installed apps.
In pycharm i have selected the proper venv path for python interpreter
Now in pycharm when I run the server it says django-extension not found. But I can run the server from terminal without any errors.
In pycharm > settings > python interpreter : django-extensions is not shown.
I checked pip list in terminal and it shows django-extension package. Also I can see django-extension folder in site-packages. I am not able to understand what's the problem.
In Pycharm most of the pacakges are shown but only few are not shown
if you are using virtualenv, you need to install it proper way switching off virtualenv by using deactivate
and then installing package to your project therefore pycharm will read it in the project interprter folder
You need to set your project interpreter in pycharm.
It will be at File > settings > project interpreter
In above path you need to select your virtual environment (if not showing then add by clicking on '+' option)and apply then click on OK.
Now it won't show that error message.
1.Check if you are using a good virtual environment
If you have good env click on add icon on right side and add package manual
Sorry, there was no mistake with pycharm.
I have copied a complete virtualenv with my django project.
Later i found that in the /bin/activate
VIRTUAL_ENV="/path/to/original/virtualenv"
is still pointing to the folder from which i copied the project along with the vistualenv.
It should be
VIRTUAL_ENV="/path/to/present/virtualenv"
The packages are getting installed in the original folder. So now everything is working fine

VS Code error when importing Django module

I'm working on a web app and I use Django as framework.
I'm using VS Code on a macOS.
I get an error when I try to import some Django module. This is a screenshot of my code in error.
The error message is the following:
[pylint] E0401:Unable to import 'django.conf.urls'
I too was facing this error while working with Python virtual environments. In my case, it was happening because I have installed Django on my virtual environment and my base environment didn't contain any module named Django.
Base(Global) environment
and when I use the same command inside my virtual environment (myDjangoEnv)
Fix:
Now what I understood is that pylint uses the python interpreter to analyze our code and mark the error while we write the code.
Since I have three python environments in my system, hence three different python interpreters are available to my VS Code.
So if VS code uses the Python interpreter from the base environment, it will not recognize the Django module (import Error). Therefore, you are required to change the Python interpreter to the one present in your virtual environment.
It sounds complicated but it is pretty simple:
Click on the bottom left of the screen to change python interpreter.
Select from the list of available Python interpreters. Make sure you select the appropriate interpreter with respect to the current project.
Follow steps mentioned in the image. For details, or if that doesn't work, read further!!!
If you can't see your interpreter (installed in the virtual environment) listed in the drop-down list OR selecting interpreters listed don't rectify the error.
Then, you have to give the path of your interpreter (installed in venv) to vs code. Because you might have installed Django only in your venv. Happens when you don't use anaconda to create venv.
Steps to rectify-
To check the path, activate venv and type which python in terminal, this will give path. Copy the path.
Click interpreter on lower left, to pull drop-down, as shown in pic above.
Click enter the interpreter path.
Paste path copied.
This will assign the right interpreter and rectify the error.
Locate your project's virtual environment. In my case, I am working on a Django project and my virtual environment is located on the path below:
C:/Users/abc/Desktop/Virutal36/myLab/Scripts/python.exe
Copy the address of your virtual environment.
On VS Code, Select File > Preferences> Settings to open your User Settings (or use the Ctrl+, shortcut).
Create or modify an entry for python.pythonPath with the full path to your virtual environment and you will be good to go. In my case it is:
C:/Users/abc/Desktop/Virutal36/myLab01/Scripts/python.exe
https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter
Ctrl+Shift+P
Type Python:select interpreter
Now we will get:
Choose Enter interpreter path:
Select Find...
Then
Check correct django version is properly installed and active?
In the active environment, calling this code in python interpreter shouldn't have errors.
from django.conf.urls import url
Check the VS studio python environment
https://code.visualstudio.com/docs/python/environments
You need to select the right environment. So, go to view in tool bar, then select command pallet(ctrl+shift+p), then type "python:select interpreter", then select the right virtual environment where you start you project.
In my case I solved it using the Select Interpreter option from VS Code's Command Palette (Shift + Command + P).
I chose the Python interpreter option which corresponds with the folder in which my virtual environment was and it solved the issue immediately.
Hope it helps :)
Are you using a virtual environment (mkvirtualenv)? In that case you need to make sure you install django and pylint etc., within your virtual environment too, using the following commands.
workon [yourEnvName]
pip install pylint
pip install django
pip install djangorestframework
and so on...for all the modules you want to use.
enter image description here
Choosing a global environment helped me to recover this issue
First check the requirements that you install djangorestframework, second if you work on virtual envs maybe the pylint that you use not check in your virtual env so you can install in local the package.
For me, I opened the nested folder of my Django project. So, Django couldn't find the modules of venv.
Just mentioning, I thought it might be helpful to someone.

Why doesn't Pydev find Django?

I have Django installed. I also have django-admin.py in my python-2.7 folder. I can import django using my Python shell (DreamPie). But I can't create a new Django project in Pydev. When trying to create a new Pydev Django Project (choosing python2.7 as the interpreter) I receive the following error message:
I went to the link in the message and I have all the pre-requisite to getting started in Django: Django is installed and import django properly works (in the shell).
In Eclipse:
Window --> Preferences --> Interpreter - Python.
Make sure to choose Python2.7 as the interpreter.
Now, click New Folder. Browse to find the path to django (in this case: C:\Django-1.2.5\django).
OK --> Apply --> OK.
That's it. You can create a new Django project.
What helped me (adjust dir and file names according your installation):
I created copy of file Django-1.3-py2.7.egg-info from C:\Python27\Lib\site-packages\ in the same folder with extender *.egg.
Then, in Eclipse dialog
Window -> Preferences -> PyDev -> Interpreter-Python
I used buton New Egg/Zip(s) in order to add this newly created file to System PYTHONPATH list.
This way Eclipse found Django.
I refer to Fabio Zadroznys answer which should work for all of you.
Probably the PYTHONPATH configured in your interpreter is not correct...
I.e.: if you have a structure with
/my/path/django
/my/path/django/init.py
you have to make sure that
/my/path
is added to the PYTHONPATH (and /my/path/django is NOT!!!)
Another thing to note is that right after installing Django, you just have to do a refresh >in your interpreter so that it's recognized (go to the interpreter preferences page, press >'apply' and choose the interpreters to be re-analyzed -- there are already plans to make >this automatic in one of the upcoming releases, but right now you have to do that manually >because the interpreter is analyzed when you configure it and 'set in stone').
LINK:
pydev does not recognize django installation
You can follow any python version. But in your case Django is not installed globally.
Requirement is you need to have Django installed in system.
For checking the django is installed or not
open CMD / terminal (Assuming that Python is already installed)
$ python
$ import django
$ django.VERSION
You can see the output like this. Here I am using Django 3.1
(3, 1, 0, 'final', 0)
Then in the Preferences --> Interpreter - Python --> Click new and add the location where python.exe or Python application resides. Make sure you are naming the Interpreter appropriately for future reference.
Then restart the eclipse and go to New --> Other --> PyDev --> PyDev Django Project
Name your project
Select the Interpreter that you previously created from the drop down.
The above fixes your issue.
not working for me. the attached screensnap shows that django is importable from the pydev shell, and that the path to it is in pydev's SYSTEMPATH.
(since folks have mentioned virtualenv in other questions, i'll mention this is occuring within a virtualbox but i don't think that matters?)
It needs to be in the PATH or in the /usr/bin

pydev not recognizing python installation with django

I have python installed with django. I know the installation is there because I installed it following the directions and in the command line I can do "import python" and there is no crash.
When I try creating a django project in pydev, I get an error: "Django not found."
What could the problem be?
I had the same problem and this is what I did just after installing eclipse:
Preferences > Interpreter Python
Pressed Auto-config
Selected /Library/Python/x.x/site-packages, which was not selected
(some django folders were in there, persumably the installation)
Clicked apply / OK
Creating a Django project afterwards should be ok.
Good Luck!
go in the menu to window -> preference -> pydev -> Interpreter - Python ; and add the python interpreter there, if you installed the django in a virtual environment you should add the python interpreter of the virtual env ; after adding the interpreter by clicking on new you should now click on Apply , you should see now all the system libs from PYTHONPATH added there between them Django .
hope this will help
Jaffa,
You might have configured your python interpreter in Eclipse prior to installing Django in python.
So what you have to do now is in Eclipse remove the Python interpreter and add it again (now that it has Django installed) and all should work ok.
I was having this same exact problem today, and I just wanted to add some notes to help people out. You can follow the instructions of antonkeren above, but there's one thing to note that was not mentioned. After adding the django directory to the path you must QUIT ECLIPSE OR APTANA STUDIO. After you restart it and try to create a django project it will work.
I stumbled upon this only yesterday myself: Django must not be among the system libraries for this to function...
I had the same problem and had solved it by adding the python home directory to the PATH variable and reconfigure the python interpreter on pydev.
Use auto config in the preferences option and then click apply and then ok.
If everything is done as said above, and still showing "Django not found" while your are trying to create django project, let try to double click 'django-admin' python file that in scripts folder of python. And try to create django project
I think I've got the same problem, Eclipse 3.5.2 on Fedora. In my PyDev Python interpreter settings I've got the site-packages/django folder (site-packages/django/bin too, just in case), but when I try to create a django project I get an error dialogue saying "It seems that the selected interpreter does not have Django available". Looks like I'm going to have to use Eric4, which is a pity because I can't get that working at all on my Windows rig!
Unless I manage to understand the cryptic advice above, "Django must not be among the system libraries for this to function..."
Same problem I have faced today. I took help from these answer.
but in my case it didnt work.
There is small modification in #antonkeren answer.
1) Go to Preferences. Windows > preferences.
2) Search for Pydev
3) Select Python Interpreter
4) Pressed Quick Auto-config
5) now download www.djangoproject.org/downloads django tar and extract on any location.
6) now click on Folder in "Libraries tab" of "System PYTHONPATH" and select your extracted django project folder location.
7) Click on apply and ok.
Creating a Django project afterwards should be ok.
Cheers...!!

Categories