question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non-rendered variable in the project-template name causes error

See original GitHub issue
  • Cookiecutter version: 1.7.2
  • Python version: 3.7.5
  • Operating System: OSX (10.14.6)

Description:

When using a non-rendered variable (starting with an underscore) for the directory-name, cookiecutter gives an error.

What I’ve run:

cookiecutter.json

{
    "game_name": "",
    "_game_slug": "{{ cookiecutter.game_name.lower().replace(' ', '-') }}",
    "game_slug": "{{ cookiecutter.game_name.lower().replace(' ', '-') }}",
}

When I set the template-folder-name to {{cookiecutter.game_slug}} (so I use the variable without the underscore) it will create the right directory:

terminal output

❯ cookiecutter boilerplate-project
game_name []: this is a test with CAPTIALS and spaces
game_slug [this-is-a-test-with-captials-and-spaces]:

👍

But when I replace use the non-rendered variable (_game_slug) in the template-folder-name, it gives an error. The reason why I want the variable to be non-rendered, is because I want to base the variable-value on the game_name. And I don’t want an user to be able to overwrite this slug.

I get the following error:

❯ cookiecutter boilerplate-project
game_name []: this is a test with spaces and CAPITALS
game_slug [this-is-a-test-with-spaces-and-capitals]:

Traceback (most recent call last):
  File "/usr/local/bin/cookiecutter", line 8, in <module>
    sys.exit(main())
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/site-packages/cookiecutter/cli.py", line 140, in main
    cookiecutter(
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/site-packages/cookiecutter/main.py", line 101, in cookiecutter
    result = generate_files(
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/site-packages/cookiecutter/generate.py", line 352, in generate_files
    generate_file(
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/site-packages/cookiecutter/generate.py", line 161, in generate_file
    shutil.copyfile(infile, outfile)
  File "/usr/local/Cellar/cookiecutter/1.7.2/libexec/lib/python3.8/shutil.py", line 261, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: "/Users/oskar/projects/gamedev/godot/test/{{ cookiecutter.game_name.lower().replace(' ', '-') }}/screens/.DS_Store"

And I have 2 folders where half of the files are in one and half are in the other:

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
njilescommented, Apr 23, 2021

@edesz This example still fails for me (cookiecutter version 1.7.2):

$ cookiecutter -v test
DEBUG cookiecutter.config: User config not found. Loading default config.
DEBUG cookiecutter.main: context_file is test/cookiecutter.json
DEBUG cookiecutter.generate: Context generated is OrderedDict([('cookiecutter', OrderedDict([('game_name', '')]))])
game_name []: this is a test with CAPTIALS and spaces
DEBUG cookiecutter.utils: Making sure path exists: /Users/nicholasiles/.cookiecutter_replay/
DEBUG cookiecutter.find: Searching test for the project template.
DEBUG cookiecutter.find: The project template appears to be test/{{cookiecutter.game_slug}}
DEBUG cookiecutter.generate: Generating project from test/{{cookiecutter.game_slug}}...
Unable to create project directory '{{cookiecutter.game_slug}}'
Error message: 'collections.OrderedDict object' has no attribute 'game_slug'
Context: {
    "cookiecutter": {
        "_template": "test",
        "game_name": "this is a test with CAPTIALS and spaces"
    }
}

However, if I rename the {{cookiecutter.game_slug}} directory to {{cookiecutter.game_name}} then everything runs ok, and this is a test with CAPTIALS and spaces/mytest.txt is generated and contains the correctly rendered contents that you describe:

$ cookiecutter -v test
DEBUG cookiecutter.config: User config not found. Loading default config.
DEBUG cookiecutter.main: context_file is test/cookiecutter.json
DEBUG cookiecutter.generate: Context generated is OrderedDict([('cookiecutter', OrderedDict([('game_name', '')]))])
game_name []: this is a test with CAPTIALS and spaces
DEBUG cookiecutter.utils: Making sure path exists: /Users/nicholasiles/.cookiecutter_replay/
DEBUG cookiecutter.find: Searching test for the project template.
DEBUG cookiecutter.find: The project template appears to be test/{{cookiecutter.game_name}}
DEBUG cookiecutter.generate: Generating project from test/{{cookiecutter.game_name}}...
DEBUG cookiecutter.generate: Rendered dir this is a test with CAPTIALS and spaces must exist in output_dir .
DEBUG cookiecutter.utils: Making sure path exists: this is a test with CAPTIALS and spaces
DEBUG cookiecutter.utils: Created directory at: this is a test with CAPTIALS and spaces
DEBUG cookiecutter.generate: Project directory is /Users/nicholasiles/this is a test with CAPTIALS and spaces
DEBUG cookiecutter.hooks: hooks_dir is /Users/nicholasiles/test/hooks
DEBUG cookiecutter.hooks: Running hook pre_gen_project
DEBUG cookiecutter.generate: Processing file mytest.txt
DEBUG cookiecutter.generate: Created file at /Users/nicholasiles/this is a test with CAPTIALS and spaces/mytest.txt
DEBUG cookiecutter.generate: Check mytest.txt to see if it's a binary
DEBUG cookiecutter.generate: Writing contents to file /Users/nicholasiles/this is a test with CAPTIALS and spaces/mytest.txt
DEBUG cookiecutter.hooks: hooks_dir is /Users/nicholasiles/test/hooks
DEBUG cookiecutter.hooks: No post_gen_project hook found

This outcome, plus the two terminal outputs I’ve posted here, makes me think that maybe the project template is being generated first (before cookiecutter.game_slug has been created), and then pre_gen_project.py is being run afterwards?

0reactions
njilescommented, May 7, 2021

@simobasso Thanks for the response - my comment was to remark that @edesz 's example doesn’t work, for me at least, rather than to request help for an issue that I was having.

However, for completeness, the version at 623e4b9 still fails with the same error:

$ cookiecutter --version
Cookiecutter 2.0.0 from /Users/nicholasiles/anaconda3/envs/cc-gh/lib/python3.9/site-packages (Python 3.9)

$ cookiecutter -v test
DEBUG cookiecutter.config: User config not found. Loading default config.
DEBUG cookiecutter.main: context_file is test/cookiecutter.json
DEBUG cookiecutter.generate: Context generated is OrderedDict([('cookiecutter', OrderedDict([('game_name', '')]))])
game_name []: this is a test with CAPTIALS and spaces
DEBUG cookiecutter.utils: Making sure path exists: /Users/nicholasiles/.cookiecutter_replay/
DEBUG cookiecutter.find: Searching test for the project template.
DEBUG cookiecutter.find: The project template appears to be test/{{cookiecutter.game_slug}}
DEBUG cookiecutter.generate: Generating project from test/{{cookiecutter.game_slug}}...
Unable to create project directory '{{cookiecutter.game_slug}}'
Error message: 'collections.OrderedDict object' has no attribute 'game_slug'
Context: {
    "cookiecutter": {
        "_template": "test",
        "game_name": "this is a test with CAPTIALS and spaces"
    }
}

so I still think that maybe the project template is being generated first (before cookiecutter.game_slug has been created), and then pre_gen_project.py is being run afterwards?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Project contains too many procedure, variable, and constant ...
A project's procedure, variable, constant, and parameter names are stored in a name table. This error has the following cause and solution:.
Read more >
ProjectTemplate.pdf - R Project
This function removes specific (or all by default) named objects from the global environment. If used within a ProjectTemplate project, then ...
Read more >
Source code for cookiecutter.generate
"""Functions for generating a project from a project template. ... for variable, overwrite in overwrite_context.items(): if variable not in context: # Do ...
Read more >
Project template not found - MicroStation Wiki
template into project folder. Error is caused by: Location of _USTN_PROJECTROOT does not contain "project.template" file. 1. Locate the ...
Read more >
Development guide for GitLab CI/CD templates
yml immediately causes a lint error because there are no such jobs named performance in the included template anymore. Therefore, users have to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found