I am maintaining an internal python package with a number of utility functions. As part of this package, I included a project generator console_script
which calls cookiecutter using a custom template that is shipped with the package. This works fine when installing the package using pip
(basically, as an sdist).
I am trying to get people to use conda
as much as possible, so this is inconvenient as dependencies get resolved/installed using pip
, not conda
. Therefore, I am creating a conda package using the python setup.py bdist_conda
way that is included by the Anaconda install. This works well, afaict, for another internal package.
However, when I try to create the present package, the bdist_conda
creation byte-compiles all the python files in the package, so of course (but unintended) also the template files. This leads to a syntax error, because of the jinja strings, when running python setup.py bdist_conda
:
byte-compiling build/bdist.linux-x86_64/dumb/home/bilderbuchi/anaconda3/lib/python3.5/site-packages/mypkg/project_template/{{cookiecutter.project_name_simplified}}/{{cookiecutter.project_name_simplified}}.py to {{cookiecutter.project_name_simplified}}.cpython-35.pyc
File "/path/to/mypkg/project_template/{{cookiecutter.project_name_simplified}}/{{cookiecutter.project_name_simplified}}.py", line 4
{%- if cookiecutter.command_line_interface == 'argparse' %}
^
SyntaxError: invalid syntax
I also can reproduce with a plain python setup.py bdist
, so I can exclude conda from being the culprit here. I add the template files with a MANIFEST.in
containing recursive-include mypkg/project_template *
, and include_package_data = True
in the setuptools-based setup.py
.
Is there a (better) way to ship a cookiecutter template with some package? As I said, this works nicely when used with pip
/sdist, but fails with bdist
. Alternatively, is there a way to suppress bdist byte-compiling a given set of files?
The template cannot be placed online, which is why I want to include it in the package. I also considered placing the template, separately, elsewhere, but then users depend on that location (eg network drive) being available when they use the project generator. I looked at adding it as a zip file, but apart from this being super unconvenient from a version control point of view, apparently , users will be prompted to delete the existing (I guess unpacked to some user location) template on the next run, so I'd rather avoid that.
Edit: I also posted this in the cookiecutter issue tracker , but didn't get any feedback so far.
So, after a fruitless search, I circumvent this issue by changing all my .py
template files to .pytemplate
. This avoids bdist/bdist_conda discovering them. I added a post_gen script that renames all the files back to .py
after the project has been created from the template. Stupid workaround, but it works. :sigh:
I know that for bdist at least, compilation can be avoided by setting sys.dont_write_bytecode
, but this workaround is not available for bdist_conda
, see https://github.com/conda/conda-build/issues/686 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.