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.

UnicodeDecodeError after follow your-first-cookiecutter instructions

See original GitHub issue
  • Cookiecutter version: 1.4.0
  • Template project url: -
  • Python version: 2.7.12 and 3.5.2
  • Operating System: OS X

Description:

I was follow: https://cookiecutter.readthedocs.io/en/latest/first_steps.html#your-first-cookiecutter A new directory created successfully but it produces errors like below. I think maybe it’s about Python version? I tried it on Python 2.7.6. It works fine.

What I’ve run:

Python2

$ cookiecutter HelloCookieCutter1
directory_name [Hello]:
file_name [Howdy]:
greeting_recipient [Julie]:
Traceback (most recent call last):
  File "/Users/mrteera/.virtualenvs/automate_micro/bin/cookiecutter", line 11, in <module>
    sys.exit(main())
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/click/core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/cookiecutter/cli.py", line 106, in main
    config_file=user_config
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/cookiecutter/main.py", line 144, in cookiecutter
    output_dir=output_dir
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/cookiecutter/generate.py", line 350, in generate_files
    generate_file(project_dir, infile, context, env)
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/cookiecutter/generate.py", line 168, in generate_file
    tmpl = env.get_template(infile_fwd_slashes)
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/jinja2/environment.py", line 812, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/jinja2/environment.py", line 774, in _load_template
    cache_key = self.loader.get_source(self, name)[1]
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/site-packages/jinja2/loaders.py", line 175, in get_source
    contents = f.read().decode(self.encoding)
  File "/Users/mrteera/.virtualenvs/automate_micro/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 0: invalid start byte

Python3

cookiecutter HelloCookieCutter1
directory_name [Hello]:
file_name [Howdy]:
greeting_recipient [Julie]:
Traceback (most recent call last):
  File "/usr/local/bin/cookiecutter", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/cookiecutter/cli.py", line 106, in main
    config_file=user_config
  File "/usr/local/lib/python3.5/site-packages/cookiecutter/main.py", line 144, in cookiecutter
    output_dir=output_dir
  File "/usr/local/lib/python3.5/site-packages/cookiecutter/generate.py", line 350, in generate_files
    generate_file(project_dir, infile, context, env)
  File "/usr/local/lib/python3.5/site-packages/cookiecutter/generate.py", line 168, in generate_file
    tmpl = env.get_template(infile_fwd_slashes)
  File "/usr/local/lib/python3.5/site-packages/jinja2/environment.py", line 812, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python3.5/site-packages/jinja2/environment.py", line 774, in _load_template
    cache_key = self.loader.get_source(self, name)[1]
  File "/usr/local/lib/python3.5/site-packages/jinja2/loaders.py", line 175, in get_source
    contents = f.read().decode(self.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
$ python Hello/Howdy.py
Hello, Julie!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
mrteeracommented, Sep 24, 2016

I have the same problem again, but I found that it’s not related to Cookiecutter. I just want to put the solution here in case someone also found this problem. The problem is in your directory, there are files contain non-UTF-8, so what I did is remove all those files that contain non-UTF-8 characters.

find . -type f | xargs -I {} bash -c "iconv -f utf-8 -t utf-16 {} &>/dev/null || echo {}" > utf8_fail

Credit: https://gist.github.com/rmetzler/2947828

0reactions
rtr1commented, Sep 16, 2022

For me this issue was due to the default encoding in the terminal’s redirection of stdout, using Powershell in VS Code on Windows. The default Powershell encoding is utf8NoBOM which is “Encodes in UTF-8 format without Byte Order Mark (BOM)”.

So when I did this from VS Code:

pip freeze > requirements.txt

The requirements.txt file begins with a value of 0xFF. If I manually tell it to use an encoding of utf8 or ASCII then cookiecutter works as expected.

pip freeze | Out-File -Encoding utf8 -FilePath requirements.txt pip freeze | Out-File -Encoding ASCII -FilePath requirements.txt

Read more comments on GitHub >

github_iconTop Results From Across the Web

UnicodeDecodeError after follow your-first-cookiecutter ...
I tried it on Python 2.7.6 . It works fine. What I've run: Python2. $ cookiecutter HelloCookieCutter1 directory_name [Hello]: file_name [Howdy]: ...
Read more >
error UnicodeDecodeError: 'utf-8' codec when reading CSV
I recently download the PISA 2012 Student database from PISA. I follow the instructions and successfully read it on SAS. Then I exported...
Read more >
UnicodeDecodeError - Python Wiki
The UnicodeDecodeError normally happens when decoding an str string from a certain coding. Since codings map only a limited number of str ...
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