How to include additional files into a package? - python

How can I include files that are not written in python to the packaging?
Or to make it more concrete:
Assuming I have the following package structure, how do I include all the files of the templates directory?
- projectname
- __init__.py
- classfile.py
- templates
- file1.prototxt
- file2.caffemodel
I have the following setup.py
from distutils.core import setup
setup(
name = 'projectname',
packages = ['projectname'],
version = '0.1',
license='MIT',
description = 'my description',
author = 'FULL NAME',
author_email = 'example#gmail.com',
url = 'https://github.com/repo',
download_url = 'https://github.com/repo/.../v_01.tar.gz',
keywords = ['keywords'],
install_requires=[
'opencv-python',
'numpy'
],
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
],
)
As I read somewhere on stackoverflow, I tried adding the following to the setup.py file but it didn't make a difference.
package_data = {
'templates': ['*'],
}

Preamble. The word "package" is heavily overloaded. I'll use the word meaning "importable package", i.e. an directory with __init__.py.
I see two problems with your code. First, you use distutils which is an obsolete outdated package. It will be removed from Python. You should switched to setuptools long ago. So replace the line
from distutils.core import setup
with
from setuptools import setup
The 2nd problem is in
package_data = {
'templates': ['*'],
}
The keys in the dictionary are packages, not directory names. Your templates is not a package (no __init__.py) but a data directory. So the way to include it is
package_data = {
'projectname': ['templates/*'],
}

Potential duplicate of this question
include_package_data=True

Related

Python module not found after build/install

I have an internal tool that we are using at work to pull some data and generate a report. The tool was written in python. I made some changes to the tool, and decided to tidy up the folder structure which seemed a bit messy to me. I probably broke something, but I'm determined to fix it and figure out what the issue is.
The project is named ReportCardLite, and my folder structure is like this:
ReportCardLite root folder
-Folder 1
-Folder 2
-rclite
-----__init.py__
-----A.py
-----B.py
-----C.py
-setup.py
-setup.cfg
-__init__.py
Originally, the author was importing by using the package name, which was odd to me since all the script files were in the same directory. So, in A.py for example, he would say something like "from rclite.B import fun".
I decided to remove the "unnecessary" module name from before all the import statements. Of course, that broke it, but I quickly figured out that I could add a line in my settings.json file to look within the rclite folder for all modules. Now my scripts were importing from one another without the module name, and running fine from within the IDE terminal window.
I next needed to build an executable from this module. The original author had included a setup.py and a setup.cfg file, so I used that to build and install this. But when I run this new executable, I receive errors that modules cannot be found. If I change it back to how it originally was, namely the "rclite.A" qualifier, it runs fine. I've spent hours trying to understand what is going on here, and I'm just out of ideas and cannot find any relevant questions using Google.
Can someone kindly point out which configuration I need to change in order to not have to put "rclite" in front of the import statements? Thanks!
Here is the setup.py script.. didn't see anything that looked promising.
"""ReportCardLite Packaging
See:
https://code.amazon.com/packages/ReportCardLite/trees/mainline
"""
# Always prefer setuptools over distutils
from codecs import open
from os import path
from setuptools import setup, find_packages
from cx_Freeze import setup, Executable
here = path.abspath(path.dirname(__file__))
import sys
# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
setup(name='ReportCardLite',
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='0.9.0',
description='RC.Lite for SIM',
long_description=long_description,
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 3 - Alpha',
# Indicate who your project is intended for
# 'Intended Audience :: Developers',
# 'Topic :: Software Development :: Build Tools',
# Pick your license as you wish (should match "license" above)
# 'License :: OSI Approved :: MIT License',
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6'
],
# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
packages=find_packages(exclude=['test']),
#packages = ['rclite'],
# Alternatively, if you want to distribute just a my_module.py, uncomment
# this:
# py_modules=["my_module"],
# List run-time dependencies here. These will be installed by pip when
# your project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/requirements.html
install_requires=[
'isoweek',
'jinja2',
'jsonpickle',
'jsonplus',
'markdown',
'pkg-resources',
'premailer',
'python-dateutil',
'pyxdg',
'six'
],
options={
'build_exe': {
'packages': ['lxml', 'asyncio', 'markdown.extensions'],
'include_files': ['templates/', 'webapp/']
},
},
# executables = [Executable("rclite/aiohttp_app.py", base="Win32GUI")],
executables=[Executable(script="rclite/aiohttp_app.py", targetName="run_rclite.exe")],
# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
# for example:
# $ pip install -e .[dev,test]
extras_require={
'dev': ['wheel'],
'test': ['mock'],
},
# If there are data files included in your packages that need to be
# installed, specify them here. If using Python 2.6 or less, then these
# have to be included in MANIFEST.in as well.
package_data={
# 'sample': ['package_data.dat'],
},
# Although 'package_data' is the preferred approach, in some case you may
# need to place data files outside of your packages. See:
# http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa
# In this case, 'data_file' will be installed into '<sys.prefix>/my_data'
# data_files=[('my_data', ['data/data_file'])],
# To provide executable scripts, use entry points in preference to the
# "scripts" keyword. Entry points provide cross-platform support and allow
# pip to create the appropriate form of executable for the target platform.
entry_points={
'console_scripts': [
'rclite = rclite.__main__:main',
'rclite-gui = rclite.aiohttp_app.__main__'
],
},
)

How to package my python program so the user can install it using setup.py

I have a single python file right now and I am asked to convert it into a python module where the user can install it using python setup.py install. I am not sure how to do that. I have followed some instructions online and created the setup.py file and the init.py file. The setup.py file looks like this:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="",
version="0.0.1",
author="",
author_email="",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
I am not sure if this setup.py file is correct.Also I don't know what I am supposed to do next. Can anyone help me and tell me what am I supposed to do? Is there tutorial that teaches this? I can't really find anything related. Also is my setup.py correct? Thanks!
There are several ways to do packaging. packaging Python Projects on python.org and setuptools docs are a good start.
Unfortunately, examples tend to focus on package distributions, not single modules. Instead of packages, use the py_modules keyword. Assuming your module is called "test.py", this setup.py will work
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="test",
version="0.0.1",
author="",
author_email="",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
py_modules = ["test"],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
If you think this will expand to multiple modules, then you can go back to using
setuptools.find_packages(). In this case, you want a subdirectory named after your desired package and put the init in there
some_random_project_file
+-- setup.py
README.md
LICENCE
+-- test
+-- __init__.py
test.py

Module imports with and without setuptools build

I want to make my python app distributable but when I run it from command line from project folder all works well. After the packaging, there is a problem with module import.
All modules exists in package tlen. In my app I use eg. from Sender import Sender where Sender is tlen/Sender.py module.
All works well when I running tlen/main.py.
Problem exists when I try do package by sudo python setup.py install and run command tlen. Then I receive:
File "/usr/lib/python3.7/site-packages/tlen-1.0-py3.7.egg/tlen/main.py", line 3, in <module>ModuleNotFoundError: Nomodule named 'Sender'
Whole project:
https://github.com/tloszabno/tl_en
My setup.py file:
setuptools.setup(
name='tlen',
version='1.0',
author='Tomasz Łoś',
author_email='tloszabno#gmail.com',
description='A tool to learn foreign language',
packages=["tlen"],
entry_points={
'console_scripts': [
'tlen = tlen.main:main'
]
},
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
)
What I doing wrong with imports?
Hah I've solved it!
Just added app.py file to main dir (.. of tlen) with content:
#!/usr/bin/env python
from tlen import main as app
def main():
app.main()
if __name__ == '__main__':
main()
then setup.py:
import setuptools
setuptools.setup(
name='tlen',
version='1.0',
author='Tomasz Łoś',
author_email='tloszabno#gmail.com',
description='A tool to learn foreign language',
packages=["tlen"],
entry_points={
'console_scripts': [
'tlen = app:main'
]
},
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
scripts=[
'app.py',
]
)

pip install ignoring child directories on install with django project

I am writing a Django application and am very new to the framework and python. I am following the tutorial here: https://docs.djangoproject.com/en/1.9/intro/reusable-apps/ and am currently at packaging and installing the application created from this tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial01/
This is my first Django application and my first time using Python.
Currently my setup.py file looks like this:
import os
from setuptools import find_packages, setup
with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
README = readme.read()
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
setup(
name= 'django-polls',
version='0.1',
packages=find_packages(),
include_package_date=True,
license='BSD License', #example license
description = 'A simple Django app to conduct Web-based polls.',
long_description = README,
url='https://www.example.com',
author='Your Name',
author_email='yourname#example.com',
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.9.6', # replace "X.Y" as appropriate
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License', # example license
'Operating System :: OS Independent',
'Programming Language :: Python',
# Replace these appropriately if you are stuck on Python 2.
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
)
my MANIFEST.in file looks like this:
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
recursive-include docs *
my file directory is as follows:
|polls
|__init.py__
|admin.py
|apps.py
|models.py
|tests.py
|urls.py
|views.py
|__pychache__
|# pycachestuff
|migrations
|# migrations stuff
|static
|polls
|images
|background.gif
|style.css
|templates
|polls
|detail.html
|index.html
|results.html
|docs
|
When I run the setup, the produced .zip works great, all the directories are included. However, when I run
pip install C:\Path\To\dist\django-polls-0.1.zip -t C:\Path\To\Package
It only installs what is immediately under the primary polls directory and migrations. It ignores the subdirectories static and templates. It also ignores docs, but the folder is empty and the tutorial has led me to believe empty directories are ignored.
Does anyone know how I can fix this? A few answers I have seen suggest using package_data or data_files, but I'm very new to python and I can't seem to get the syntax right (or those answers are wrong).
Thank you for your time

After installing app TemplateDoesNotExist error

In virtualenv defaulting python3:
First I removed my app myapp from project. And I created a new folder called django-myapp next main django project directory.
Inside django-myapp I copied the myapp to this folder and created all needed files for making python package.
And I ran python setup.py sdist. Until now everything is ok.
My template and css files are included in django-myapp-0.1.tar.gz file. But when I installed this app with pip install django-myapp/dist/django-myapp-0.1.tar.gz, the app is installed. But when I run server and go to main page TemplateDoesNotExist error happens.
My admin page works, and then I checked /env/alis5/lib/python3.4/site-packages/myapp/ there is no templates directory in it.
What is the problem. I followed django tutorial: https://docs.djangoproject.com/en/1.7/intro/reusable-apps/
here is my MANIFEST.in:
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
recursive-include docs *
and here is also my setup.py:
setup(
name='django-polls',
version='0.1',
packages=['polls'],
include_package_date=True,
license='BSD License', # example license
description='A simple Django app to conduct Web-based polls.',
long_description=README,
url='http://www.example.com/',
author='Maksat Yalkabov',
author_email='yalkabovmaksat#gmail.com',
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License', # example license
'Operating System :: OS Independent',
'Programming Language :: Python',
# Replace these appropriately if you are stuck on Python 2.
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
)
You said you followed the tutorial, but I have to ask, did you create the manifest file? You didn't mention it, and only Python is included by default.
Create myapp/MANIFEST.in and inside it add recursive-include myapp/templates *
That should ensure that python setup.py sdist creates the package with your templates directory recursively.
edit
Ok, so I've just tested this on a project of mine.
- proj
- setup.py
- MANIFEST.in
- injurytracker
- init.py
- templates
- etc
In setup.py I've got packages=['injurytracker'], to include my package, and in MANIFEST.in I've got recursive-include injurytracker/templates * And when I check the dist.gz I've got my templates included.
As you've mentioned include_package_data, setting that either way, doesn't effect the template directory inclusion.
In your setup.py also import find_packages from setuptools, and in your setup method add somthing like this;
packages=find_packages(exclude=["project", "project.*"]),
For me that picks out all my python modules & files that I expect, migrations included.
Do you have this in your settings.py?
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader', # <--
)

Categories

Resources