Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33890 closed Bug (invalid)

call_command('startproject', 'project_name', '.') raises CommandError 'project_name'

Reported by: piscvau Owned by: nobody
Component: Core (Management commands) Version: 4.0
Severity: Normal Keywords: call_command raises exception
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by piscvau)

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!.....

Change History (7)

comment:1 by Mariusz Felisiak, 2 years ago

Resolution: invalid
Status: newclosed

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.

comment:2 by piscvau, 2 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 2 years ago

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.

comment:4 by piscvau, 2 years ago

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.

comment:5 by Mariusz Felisiak, 2 years ago

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.

comment:6 by piscvau, 2 years ago

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

Version 0, edited 2 years ago by piscvau (next)

comment:7 by Mariusz Felisiak, 2 years ago

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 comment), it works perfectly fine for me. You should use one of support channels where folks will help you to debug your issue.

Note: See TracTickets for help on using tickets.
Back to Top