Reputation: 229
This is probably a really basic question but I can't find the answer anywhere for some reason. I created a custom command which I can call from the command line with python manage.py custom_command
. I want to run it from elsewhere but don't know how to do so. I have added pages to my INSTALLED_APPS in settings.py. This question: Django custom command works on command line, but not call_command is very similar but I'm not sure what the answer means and I think it's unrelated. My file structure is :
├── custom_script
│ ├── script.py
│ ├── __init__.py
├── project
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── pages
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── __init__.py
│ ├── management
│ │ ├── commands
│ │ │ ├── __init__.py
│ │ │ └── custom_command.py
│ │ ├── __init__.py
│ ├── migrations
│ │ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
content of script.py
from django.core.management import call_command
call_command('custom_command', 'hi')
content of custom_command.py
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('message', type=str)
def handle(self, *args, **options):
print('it works')
I want to run python custom_script/script.py
which will call the custom_command
but keep getting: django.core.management.base.CommandError: Unknown command: 'custom_command'. I have isolated the problem to the fact that django can't see my command as when I run print(management.get_commands())
my custom command is not listed. Additionally, after looking through the django python code for management for a while I noticed this settings.configured
variable which upon checking is False which means it only passes in the default commands when management.get_commands
is run. How can I get this to become True? Technically, I could use a subprocess if I really wanted to but since there is already a call_command feature I figured I'd try and use it.
Upvotes: 1
Views: 1824
Reputation: 3149
If somebody is using poetry, pytest and pytest-django and configuring your pytest configuration using pyproject.toml
, you are probably getting "Command not found". That's because poetry new
uses 5.x for pytest for some reason while pytest docs clearly states that you have to use 6.x in order to use pyproject.toml
as config file.
Edit pytest
to ^6.0
in your pyproject.toml
and run poetry update
. Now it should work.
Upvotes: 1
Reputation: 229
Not sure if this will help anyone, but it turns out I was doing this the wrong way. Generally, I don't think my method above will work because you have to call a django command from outside the django project basically which means the settings will not be configured. My use case was running a django command in the background on a webserver using script.py as the file to run the command. If your use case is similar you should instead call the custom command directly from the command line with python manage.py custom_command, this worked for me at least.
Upvotes: 2