[Django] #33890: call_command('startproject', 'project_name', '.') raises CommandError 'project_name'

12 views
Skip to first unread message

Django

unread,
Aug 3, 2022, 2:01:55 PM8/3/22
to django-...@googlegroups.com
#33890: call_command('startproject', 'project_name', '.') raises CommandError
'project_name'
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: nobody
Type: Bug | Status: new
Component: Core | Version: 4.0
(Management commands) | Keywords: call_command raises
Severity: Normal | exception
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
call_command ('startproject', 'project_name', '.') shoud create a project
named 'project_name' in the current directory.

to reproduce the bug :
from django.core import management
from pathlib import Path
project_name = Path.cwd().stem
management.call_command('startproject', project_name, '.')

This raises the exception

django.core.management.base.CommandError: <project_name> conflicts with
the name of an existing Python module and cannot be used as a project
name. Please try another name.

This is because the initial directory of the python script is in
sys.module.

--
Ticket URL: <https://code.djangoproject.com/ticket/33890>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 3, 2022, 2:33:19 PM8/3/22
to django-...@googlegroups.com
#33890: call_command('startproject', 'project_name', '.') raises CommandError
'project_name'
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution: invalid
Keywords: call_command raises | Triage Stage:
exception | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

This is not a bug in Django but in your code. You cannot create project
with the name of an existing Python module (as is explained in the error
message). If you're having trouble understanding how Django works, see
TicketClosingReasons/UseSupportChannels for ways to get help.

--
Ticket URL: <https://code.djangoproject.com/ticket/33890#comment:1>

Django

unread,
Aug 3, 2022, 3:45:58 PM8/3/22
to django-...@googlegroups.com
#33890: call_command('startproject', 'project_name', '.') raises CommandError
'project_name'
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution: invalid
Keywords: call_command raises | Triage Stage:
exception | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by piscvau:

Old description:

> call_command ('startproject', 'project_name', '.') shoud create a project
> named 'project_name' in the current directory.
>
> to reproduce the bug :
> from django.core import management
> from pathlib import Path
> project_name = Path.cwd().stem
> management.call_command('startproject', project_name, '.')
>
> This raises the exception
>
> django.core.management.base.CommandError: <project_name> conflicts with
> the name of an existing Python module and cannot be used as a project
> name. Please try another name.
>
> This is because the initial directory of the python script is in
> sys.module.

New description:

call_command ('startproject', 'project_name', '.') shoud create a project
named 'project_name' in the current directory.

to reproduce the bug :
from django.core import management
from pathlib import Path
project_name = Path.cwd().stem
management.call_command('startproject', project_name, '.')

This raises the exception

django.core.management.base.CommandError: <project_name> conflicts with
the name of an existing Python module and cannot be used as a project
name. Please try another name.

This is because the initial directory of the python script is in
sys.module.

When called from the command line, in the exact same condition, the
command passes :
in directory project_name :
django-admin startproject project-name .

in django docs it is described as the right usage to create a project
project_name in the current directory!....

SO I do believe this is a bug!.....

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33890#comment:2>

Django

unread,
Aug 4, 2022, 1:35:21 AM8/4/22
to django-...@googlegroups.com
#33890: call_command('startproject', 'project_name', '.') raises CommandError
'project_name'
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution: invalid
Keywords: call_command raises | Triage Stage:
exception | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

> SO I do believe this is a bug!.....

There is no need to shout, and no, it's not the same, `django-admin
startproject project-name .` tries to create a project called `project-
name`, on the other hand, in your Python shell example you tried to create
a project called `Path.cwd().stem` which conflicts with the name of an
existing Python module. Again, please try to use support channels.

--
Ticket URL: <https://code.djangoproject.com/ticket/33890#comment:3>

Django

unread,
Aug 4, 2022, 4:06:24 AM8/4/22
to django-...@googlegroups.com
#33890: call_command('startproject', 'project_name', '.') raises CommandError
'project_name'
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution: invalid
Keywords: call_command raises | Triage Stage:
exception | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by piscvau):

I do not shout I am just trying to raise a point which is the following :

if the current directory is <project-name> and you type in django-admin
startproject project-name . you create a project project-name in the
current directory project-name. This is the django docs recommended method
to create a project in the current directory.

The django docs also says : To call a management command from code, use
call_command.

and yet :
management.call_command (startproject, Path.cwd().stem, '.') raises an
exception.

The reason why the exception is raised is very straight forward.
But if you need to create a custom startproject command and create the
project in the current directory, applying the documentation leads you
immediately to this exception.
So if it is not a bug, it is at least a functional problem.

--
Ticket URL: <https://code.djangoproject.com/ticket/33890#comment:4>

Django

unread,
Aug 4, 2022, 4:13:06 AM8/4/22
to django-...@googlegroups.com
#33890: call_command('startproject', 'project_name', '.') raises CommandError
'project_name'
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution: invalid
Keywords: call_command raises | Triage Stage:
exception | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

I still don't understand where is the issue, when the current directory
name doesn't conflict with the name of an existing Python module it works
fine. For example
{{{
management.call_command(startproject, Path.cwd().stem, '.')
}}}
created a new project called `tickets_projects` when I was in the
`tickets_projects` directory.

--
Ticket URL: <https://code.djangoproject.com/ticket/33890#comment:5>

Django

unread,
Aug 4, 2022, 4:20:50 AM8/4/22
to django-...@googlegroups.com
#33890: call_command('startproject', 'project_name', '.') raises CommandError
'project_name'
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution: invalid
Keywords: call_command raises | Triage Stage:
exception | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by piscvau):

Thanks for your answer;
The issue is if you need to create a project, whose name is the name of
the current directory.

I am not sure about other consequences but a very simple fix could be :
in management.commands.templates validate_name, do not run the
import_module test if the name == Path.cwd().stem

--
Ticket URL: <https://code.djangoproject.com/ticket/33890#comment:6>

Django

unread,
Aug 4, 2022, 4:43:52 AM8/4/22
to django-...@googlegroups.com
#33890: call_command('startproject', 'project_name', '.') raises CommandError
'project_name'
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution: invalid
Keywords: call_command raises | Triage Stage:
exception | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

> The issue is if you need to create a project, whose name is the name of
the current directory.

But you can do this (see
[https://code.djangoproject.com/ticket/33890#comment:5 comment]), it works
perfectly fine for me. You should use one of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] where folks will help you to debug your issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/33890#comment:7>

Reply all
Reply to author
Forward
0 new messages