I am trying to learn how to debug a django application with PyCharm. In the application we have several custom manage.py commands. If I run there via terminal (external or Pycharm's terminal) they run fine.
If I try to run them from PyCharm (Tool -> run manage.py task) so I can debug, I get the following error:
Unknown command: 'add_question'
Type 'manage.py help' for usage.
I started working on the Django Project before using PyCharm, so I created it via django-admin, and we are adding it to pycharm as OpenDirectory->Select the project.
Any help would be helpful, be it to manage to run the command from inside PyCharm, or to connect PyCharms terminal to the debugger so that I can debug when running from PyCharms terminal.
The solution is in another answer, though a little outdated. Quick update for PyCharm 2016.3:
Run > Run... > Edit Configurations... (Note: you can add configurations by doing both "Run..." and "Debug...")
Click green + (plus sign)
In the drop down menu, choose Django Server.
Check "Custom run command" and enter your command (the part that goes after manage.py in the console.
Make sure "Port" and "Host" are empty. If not—make them empty.
Change "Name" to your liking.
"Apply" and "Close"
Now you can run this configuration (Run > Run... > Your_configuration_name) and debug it (Run > Debug... > Your_configuration_name).
You can create a configuration for a custom command you made
Select Tools->Edit Configurations.
In the left column, you will find a list and a "+" button in the upper part. Click this one, then select "Django Server.
In the right part of the same window, you check "Custom run command", and write your custom command in the box
Write a name for this configuration and select OK.
Now just run/debug, selecting the configuration you just created.
for anyone still having trouble with this. Try setting up a python run configuration which is more flexible instead of django server configuration. Checkout this post
Related
Is it possible to run my Django project in DEBUG mode, when in PyCharm? If not in PyCharm, say in production, then of course the DEBUG mode should not be True. How can this be done most neatly?
The easiest way would probably be setting up your settings.py to read the DEBUG value from the environment variables, then tell PyCharm to run with that env var set.
import os
# ...
DEBUG = (True if os.environ.get('DEBUG') else False)
and set DEBUG=1 in PyCharm's Edit Configurations... window.
Edit your run configuration and set an environment variable called DJANGO_DEBUG to True. The easiest way to do this is to create a custom run configuration.
Run > Edit Configurations
Create a new configuration for django, by clicking on + and selecting django server:
Then, set a custom environment variable. You do this by clicking on the button next to PYTHONUNBUFFERED=1:
Next, add a new configuration variable by clicking the +
Click OK, and give your new configuration a name:
Click Apply then OK to dismiss the dialogues.
Edit your settings.py and add this:
import os
DEBUG = os.environ.get('DJANGO_DEBUG', False)
Now, simply select your new configuration from the Run menu when you want to run with debugging enabled.
The standard approach for this is to have multiple Django settings files:
settings/
base.py
production.py
development.py
testing.py
then you can run DJANGO_SETTINGS_MODULE=settings.development manage.py runserver.
PyCharm allows to pick the settings in run configurations too.
Use cookiecutter-duango to generate the project template when starting a new Django project. Your project can be configured for development with PyCharm and you get the both configurations, development (with DEBUG=True) and production (with DEBUG=False) automatically.
When I try to run my Django app via PyCharm inside of Vagrant, it sends my Windows Path over SSH which then provides an error in the shell:
> ssh://vagrant#127.0.0.1:2200/usr/bin/python -u C:/Projects/dev_project/dev/manage.py runserver 0.0.0.0:8000
bash: line 0: cd:
C:/Projects/dev_project/dev: No such
file or directory /usr/bin/python: can't open file
'C:/Projects/dev_project/dev/manage.py':
[Errno 2] No such file or directory
I created a Python Project in PyCharm (and instantiated a Django Project in a sub-folder) in order to incorporate Vagrant.
dev_project (PyCharm project root)
|--.vagrant
|--dev (Django project root)
|--dev
|--app
|--manage.py
|--Vagrantfile
In Settings
I enabled Django Support (providing the Windows Paths as there is no other option) in Settings > Languages & Frameworks > Django.
The Vagrant Python Interpreter is selected as the Project Interpreter (and Django Console & Python Console)
In Run Configurations
My host is 0.0.0.0 and port is 8000
My Python Interpreter is the Vagrant Environment. I am also adding the Content Roots & Source Roots to the Python Path.
The bizarre problem is it was working fine, and then I exited out and it broke again. Also, I do not have 'Working Directory' explicitly defined anywhere.
This blog article showed me my issue, as I did not have my Path Mappings setup within my Run Configuration.
From the Article (Configure Your Project to Use the Correct Interpreter):
Select Run->Edit Configurations…
Select the configuration on the right PyCharm created for us. In my case it was called “session_tracker”.
Change “Python interpreter” to our newly created one.
A new field will appear. Click the button next to “Path mappings”. A new window will appear to let you create your mappings.
Vagrant shares a local directory with the VM. My Vagrant file is configured so a folder is called “django_shared” in our [VagrantFolder] locally, and “django_shared” in the home directory on the VM are the same. You need to enter the full paths of each of those in the “Edit Path Mappings” window. - Click the + button to create a new mapping, then enter the values for the mapping on each side.
When no other option works I do the following:
Create seperate virtualenv in my local directory
Reconfigure PyCharm to use the interpreter in that venv
Restart PyCharm
Maybe not a very good solution, but works.
I am trying to run
manage.py to validate models (as: manage.py validate) in the Interactive Shell of a Django project (called djangonew) using Pydev
The PYTHONPATH is set to include /djangonew ... so import djangonew and then dir(djangonew) actually gives me a name as 'settings' in the subfolder /djangonew/djangonew
but at the command-line I am unable run manage.py (and even find it)
How do I solve this issue? Thanks much
right click on project, open menu django-> custom command
and type verify ( or other command after "manage.py")
After having an unexpected shutdown on my DEV machine, when going back to Pycharm project, I noticed the Django view file I was editing (which had 700+ lines) when that happened, it was completely empty. I managed to restore it from a backup; no loss there.
The problem comes up when trying to debug, it returns this error: "ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings." Process finished with exit code 137
The Pycharm settings Django Support (project root, settings & manage script) have the expected values as well.
If I run the project with the ./manage .py runserver command, everything is fine. I can even access the DB with manage.py dbshell. I looked at my settings file and everything seems OK. I also updated from version 3.0.1 to 3.1.1, and no difference.
I'm using Django 1.6.1 and postgresql 9.2.7. What can I do?
For Pycharm, just go to Run -> Edit Configurations, select your project on the right of the window from Debug Configuration, and you will see Environment variables on the right. Make sure you have set DJANGO_SETTINGS_MODULE=mysite.settings, if not just add one, it is as easy as fill a key value pair from the pop up dialog.
I'm using PyCharm Professional and the answers provided here didn't work for me.
I went to Build, Execution, Deployment -> Consule -> Django Console and then added DJANGO_SETTINGS_MODULE=my_app_name.settings to Environment Variables.
There is a PyCharm Django Console.
Am I allowed to run manage.py things in that console, too?
What syntax I do enter to e.g. perform
python manage.py syncdb
in the django console?
You don't actually run it the way you're thinking. To run the manage.py tasks in PyCharm, you do the following:
On the main menu, choose Tools | Run manage.py task, or press Alt R.
In the Enter manage.py task name dialog box, start typing the desired task name. As you type, the suggestion list shrinks to show the matching tasks only.
Note that on typing an asterisk, PyCharm displays the complete list of available tasks.
Select the desired task.
Press Enter to start the task.
This was found on PyCharm's website.