Scrapy, a fast high-level web crawling & scraping framework for Python.
Fix the issue
Adrián Chaves544c1f6e390c
diff --git a/scrapy/commands/startproject.py b/scrapy/commands/startproject.pyindex e5158d993..35b58090c 100644--- a/scrapy/commands/startproject.py+++ b/scrapy/commands/startproject.py@@ -1,7 +1,7 @@import reimport osimport string-from importlib import import_module+from importlib.util import find_specfrom os.path import join, exists, abspathfrom shutil import ignore_patterns, move, copy2, copystatfrom stat import S_IWUSR as OWNER_WRITE_PERMISSION@@ -43,10 +43,10 @@ class Command(ScrapyCommand):def _is_valid_name(self, project_name):def _module_exists(module_name):try:- import_module(module_name)- return True- except ImportError:+ spec = find_spec(module_name)+ except ModuleNotFoundError:return False+ return spec is not None and spec.loader is not Noneif not re.search(r'^[_a-zA-Z]\w*$', project_name):print('Error: Project names must begin with a letter and contain'diff --git a/tests/test_commands.py b/tests/test_commands.pyindex 2e5bd6c00..10a3aa16c 100644--- a/tests/test_commands.py+++ b/tests/test_commands.py@@ -92,7 +92,10 @@ class ProjectTest(unittest.TestCase):class StartprojectTest(ProjectTest):def test_startproject(self):- self.assertEqual(0, self.call('startproject', self.project_name))+ p, out, err = self.proc('startproject', self.project_name)+ print(out)+ print(err, file=sys.stderr)+ self.assertEqual(p.returncode, 0)assert exists(join(self.proj_path, 'scrapy.cfg'))assert exists(join(self.proj_path, 'testproject'))@@ -129,29 +132,22 @@ class StartprojectTest(ProjectTest):def test_existing_project_dir(self):project_dir = mkdtemp()- os.mkdir(os.path.join(project_dir, self.project_name))+ project_name = self.project_name + '_existing'+ project_path = os.path.join(project_dir, project_name)+ os.mkdir(project_path)- p, out, err = self.proc('startproject', self.project_name, cwd=project_dir)+ p, out, err = self.proc('startproject', project_name, cwd=project_dir)print(out)print(err, file=sys.stderr)self.assertEqual(p.returncode, 0)- assert exists(join(abspath(project_dir), 'scrapy.cfg'))- assert exists(join(abspath(project_dir), 'testproject'))- assert exists(join(join(abspath(project_dir), self.project_name), '__init__.py'))- assert exists(join(join(abspath(project_dir), self.project_name), 'items.py'))- assert exists(join(join(abspath(project_dir), self.project_name), 'pipelines.py'))- assert exists(join(join(abspath(project_dir), self.project_name), 'settings.py'))- assert exists(join(join(abspath(project_dir), self.project_name), 'spiders', '__init__.py'))-- self.assertEqual(0, self.call('startproject', self.project_name, project_dir + '2'))-- self.assertEqual(1, self.call('startproject', self.project_name, project_dir))- self.assertEqual(1, self.call('startproject', self.project_name + '2', project_dir))- self.assertEqual(1, self.call('startproject', 'wrong---project---name'))- self.assertEqual(1, self.call('startproject', 'sys'))- self.assertEqual(2, self.call('startproject'))- self.assertEqual(2, self.call('startproject', self.project_name, project_dir, 'another_params'))+ assert exists(join(abspath(project_path), 'scrapy.cfg'))+ assert exists(join(abspath(project_path), project_name))+ assert exists(join(join(abspath(project_path), project_name), '__init__.py'))+ assert exists(join(join(abspath(project_path), project_name), 'items.py'))+ assert exists(join(join(abspath(project_path), project_name), 'pipelines.py'))+ assert exists(join(join(abspath(project_path), project_name), 'settings.py'))+ assert exists(join(join(abspath(project_path), project_name), 'spiders', '__init__.py'))def get_permissions_dict(path, renamings=None, ignore=None):