hello everyone I am having a problem the visual studio code autocomplete for django does not work for me, because for python it does it without problems but when working with django the framework options do not work, that is when I work with views based on classes does not generate the autocomplete, such as template_name, form_class, etc ..., likewise with the models it does not generate the help of max_length, and those other things of the framework, I have selected the interpreter but it does not work for me and also python: build workspace symbols and nothing. in advance I appreciate the help.
You can install and activate python & Microsoft AI extension in visual studio code.
install some of the plugins for local and global
Global in vscode extensions:
Python (Microsoft)
Djaneiro
code spell checker
local:
pip install —dev autopep8 pep8 pylint pylint_django
Press CTRL+SHIFT+P(CMD+SHIFT+P for MacOS) -> ">select interpreter" -> select your virtual envirnoment
https://i.ibb.co/TTfkHRF/select-vir.gif
open vscode/setting.json file and past the beneath config code:
{
....
"editor.formatOnSave": true,
"python.formatting.provider": "autopep8",
"python.linting.enabled": true,
"python.linting.lintOnSave": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--load-plugin=pylint_django"
]
}
Related
I use VS Code Version: 1.74.3 on MacOS 13.2. python -V returns Python 3.11.1.
I get the following error message: Import "django.shortcuts" could not be resolved from source Pylance(reportMissingModuleScource).
As you can see in the screenshot, the correct venv is activated and includes Django.
I also tried or checked:
Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10
https://stackoverflow.com/a/65802367/2321643 but
the suggested solution with reloading the windows did not help.
reinstallation the virtual environment within VSCode and installing Django again
re-selecting the venv.
deleting all python-specific settings in user/settings.json as well as .vcode/settings.json.
Reinstallation python and associated extensions.
Using Debug I can safely run my Django-application without any import issues. What do I need to do that Pylance does not issues these problems?
My issue was a bug, fixed and closed.
For reference: https://github.com/microsoft/pylance-release/issues/3881
Try clicking on where it says "3.11.1 ('.venv': venv)" (near the bottom right).
It should bring up a little dialog sort of thing at the top where you can verify that the Python path is correct.
Check that it is using the correct Python path. It should be something like venv/bin/python3 or venv/bin/python assuming venv is situated at the root of youur project.
That's my user settings in vscode
{
"python.pythonPath": "/Users/cristiano/miniconda3/envs/django-rest-2/bin/python",
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_django"
],
}
I installed the plugin via conda, same as the pylint
pylint 2.1.1 py36_0
pylint-django 0.11.1 py_1 conda-forge
pylint-plugin-utils 0.4 py_0 conda-forge
If i commented out the "python.linting.pylintArgs" section, pylint works with no problem. I ned to enable the plugin to avoid django-specific errros such as "Entity.objects.all()", but if I enable it, the lint stop working: it does not highlight standard errors o warning the previously was doing it.
I have same exact behaviour using vscode for win and mac. I tried also to use the .pylintrc file as described here but I have the same result: lint stop working.
Same behaviour using base conda env or a custom one.
This config for pylint is working for me:
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--disable=C0111", // missing docstring
"--load-plugins=pylint_django,pylint_celery",
],
I just got the same issue. Like #J0hnG4lt said, I had a problem with the python path. I didn't point to the path of the environment, which I have installed pylint_django. This config is work for me. Thanks #Manu.
"python.pythonPath": "/Users/mc976/Documents/Programming/Python/Practice/music_service/venv/bin/python3",
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--disable=C0111",
"--load-plugins",
"pylint_django"
]
Besides, I think you should check your environment to make sure that you have correctly installed pylint_django by using pip list.
My issue was more prosaic (but perhaps it will help other forehead slappers like myself). Run the PIP install in the correct virtualenv directory!
pip install pylint-django --upgrade
Also note that plugin errors cause Pylint to completely fail to load silently. Start with blank pylintArgs and slowly add them back one at a time to see where things go awry.
found a working answer for myself here: https://donjayamanne.github.io/pythonVSCodeDocs/docs/linting/
my settings.json file now reads:
{
"python.pythonPath": "C:\\ProgramData\\Anaconda3\\envs\\djangoSite2\\python.exe",
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": ["--disable=C0111","--load-plugins", "pylint_django"],
}
this then adds linting, but doesn't throw an error on fields that it can't find (like the Entity.objects.all() one), but has the disadvantage that if you then try and reference something that really doesn't exist it doesn't throw an error.
It now works on my Mac. This is my workspace's settings.json:
{
"python.linting.pylintEnabled": true,
"python.linting.pycodestyleEnabled": false,
"files.autoSave": "afterDelay",
"editor.fontSize": 14,
"editor.wordWrapColumn": 90,
"editor.autoClosingQuotes": "beforeWhitespace",
"python.pythonPath": "/Users/myname/anaconda3/envs/myproject/bin/python",
"python.linting.pylintArgs": [
"--disable=C0111", // missing docstring
"--load-plugins=pylint_django",
],
}
I had to be careful to have installed pylint-django into the correct python environment. For me, this meant running this command in the terminal:
$ /Users/myname/anaconda3/envs/myproject/bin/python -m install pip pylint pylint-django
python.pythonPath is deprecated. You should use python.defaultInterpreterPath instead
I ran into an error related to pylint not being able to parse JSON correctly. All I had to do was add an 's' to my config to make it plugins (plural) instead of plugin. Then everything started working.
"python.linting.pylintArgs": [
"--load-plugins=pylint_django",
],
So i'm studying python crashcourse and after finishing the basics I jumped to the django project.
I created a virtual env, installed django and created an app with manage.py startapp command.
I use Visual Studio Code, and the problem is when I go to models.py it gives an error that it's unable to find django.db module
unable to import error
Now I understand this is because of the file structure, which is as per my research the correct one, and I followed the book carefully, however i don't know how to fix this and I feel that i'm missing something.
This is my file structure for the virtual environment
file structure
Any idea how to fix it ? :)
Thanks in advance
Thank you Incognos !
To be more specific The solution was :
enter "code ." in the terminal and then a new VS opens.
Ctrl+Shift+P -> Select Interpreter
select the environment that starts with ./env or .\env
run Ctrl+Shift+` to activate it
VS is not seeing your virtualenv. You need to activate it for VS code so that it knows that django is installed in the virtualenv see https://code.visualstudio.com/docs/python/environments
it happens because pylint is installed outside your virtual environment. So install pylint inside virtual environment with this command (pip install pylint).
Note: If you're using Visual Studio Code - Insiders you'll use code-insiders . instead of the code . command.
Then, a new window will open and you can set your virtual environment interpreter there.
Reference: https://github.com/Microsoft/vscode/issues/6627
in my case, the problem was solved by opening VS-Code in the directory where virtualenv is located.
Click Ctrl-shift-p to access the Command pallete, then search for Python: Select Interpreter. Click on it and then select on the one that has been recommended by the editor.
I've just started developing a new application using Flask and PyCharm as my IDE.
I've encountered a problem which is driving me crazy.
Here is the code. I'll spare use all the import part and so on, I'll show only the routes that cause problems.
#app.route("/")
def home():
return render_template("home.html")
#app.route("/login/")
def login():
return "foo"
The first route works without a problem. However on the first line of the second route, PyCharm gives me an error message '"#" or "def" expected'. This breaks the autoindent and the auto completion features.
But the code runs without a problem.
Can anyone tell me how to fix this?
Thank you
This is because PyCharm is not able to recognize the flask package or version.
Once you add new project to PyCharm -
Go to File menu item
Click on Settings option
This will open-up the pop-up window.
In the left-hand-side link list - click on the project name
Click on the Project Interpreter link
In the right-hand-side pane select Flask and related packages that your project needs and add/install the same.
What worked for me was (I was missing pycharm's python environment dependencies for python)
If you don't already have a requirements file for your project.
Navigate to your project & this on your command line (as I used a venv virtual environment):
(venv)$ pip freeze > requirements.txt
Then Open your pycharm and open the requirements file, wait a second or two and a pop up notification will show up at the top showing you are missing dependencies and will provide you a click here to install.
After that I had no issues.
I've upgraded to PyCharm 2.5 and in the meantime upgraded django-templated-email to version 0.4.3.
Now, Django unit tests do not longer work through PyCharm, as I'm getting the error ImportError: cannot import name send_templated_mail in the test console output.
When running the same test in the console, this error does not appear. I have the feeling that it has something to do with send_templated_email being part of templated_email\__init__.py (I vaguely remember a similar issue once, but cannot for the life of me remember how I fixed it).
The settings in the test dialog are:
Target: <django app name>
Custom settings: <path to dev settings>
Environment variables: PYTHONPATH: <path as set in virtualenv>
Python interpreter: <virtualenv Python interpreter>
Interpreter options: None
Working directory: <path to Django project (where manage.py resides)>
The strange thing is that the code inspection inside the IDE does not complain and loads the templated_email module correctly through from templated_email import send_templated_mail.
I'm using Windows 7, and my Django app is running inside a virtualenv (which is correctly being recognized by PyCharm).
I would suggest upgrading to version 4.5.x if you haven't already. It fixes lots of bugs and has several improvements.
Also, if you think this is a bug in PyCharm, check out their bug tracking/filing site:
https://youtrack.jetbrains.com/issues