I'm trying to make a python program into a package:
This is my setup.py
from setuptools import setup, find_packages
setup(
name='scroll',
version='2020.6.14',
# package_dir={'': 'scroll'},
packages=find_packages(),
install_requires=[
'Click',
],
entry_points='''
[console_scripts]
scroll=scroll:scroll
''',
# .... all other stuff
)
This is the module structure,
SCROLL
- scroll/
|
+--scroll.py
- setup.py
- MANIFEST.in
- venv/
When I run python setup.py sdist, a tar.gz file is created and when extracted, it contains the source code at projects\SCROLL\dist\scroll-2020.6.14\dist_scroll-2020.6\scroll-2020.6.14\scroll
But when I install the archive using pip install ./dist/scroll-2020.6.14.tar.gz,
Running scroll produces an ModuleNotFoundError: No module named 'scroll'
This is because during installation, the source code is not copied to SCROLL\venv\lib\python3.8\site-packages\.
Copying the scroll folder manually to site-packages solves this error
I have tried using MANIFEST.in file with contents below but the code is still not copied to site-packages
include scroll
recursive-include scroll *.py
I solved this by packaging the module using flit. I just found out it can create scripts too after asking the question.
For the how-to:
Refer to https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation
Related
Given a Python package with the following structure.
Installed it with pip
pip install --upgrade git+git://github.com/balandongiv/driving_tools.git
The installed directory looks as below
As shown in figure above, the subfolder sub_file and the nickname_override.py are missing in the installation folder.
May I know what modification is required to amend this issue.
Modification to be made as per Balaitous
from setuptools import setup,find_packages
setup(name='ppackage',
version='0.0.111',
description='make life easier',
author='testx',
packages=['ppackage','ppackage.sub_file'],
)
In packages argument of setup fonction, all modules have to be explicitly mentioned. Module can be a python file or a folder containing __init__.py.
It is not recursive. Here you have two modules ppackage and ppackage.sub_folder.
See: https://docs.python.org/3/distutils/setupscript.html#listing-whole-packages
So you should have:
setup(
name=...,
packages=["ppackage", "ppackage.sub_folder"],
...
)
If you want to embed all modules in you package, you can use find_packages
from setuptools import find_packages
setup(
packages=find_packages(),
...
)
I am trying to serve static files from flask app. It is working fine in development environment from visual studio code.
I made .whl file from the project including static folder.
In setup.py
from setuptools import find_packages, setup
setup(
name='techportal',
#packages=['techportal'],
packages=find_packages(),
include_package_data=True,
install_requires=[
'flask',
],
)
In MANIFEST.IN
include myportal/static/*
Then I installed wheel and created wheel file
pip install wheel
python setup.py bdist_wheel
Installed it using pip
pip install ./myportal-0.1.whl
In linux production server I run app using:
waitress-serve --port=8080 'myportal:app'
But I am not able to open url using:
http://192.168.0.1:8080/static/index.html
I think you should use graft for folders and include for files, i.e,
graft myportal/static
global-exclude *.pyc
"global-exclude *.pyc" just to exclude .pyc files, you can ignore that line in your MANIFEST.in file.
I've restructured a project to the src directory structure. It looks like this:
root_dir/
src/
module1/
__init__.py
script1.py
script2.py
module2/
__init__.py
other_script1.py
other_script2.py
conftest.py
setup.py
tests/
conftest.py
some_tests/
conftest.py
test_some_parts.py
some_other_tests/
conftest.py
test_these_other_parts.py
My setup.py looks like this:
setup(
name='Project',
version=0.0,
author='Me',
install_requires=['pyodbc'],
tests_require=['pytest'],
setup_requires=['pytest-runner'],
test_suite='root_dir.Tests',
entry_points={
'console_scripts': ['load_data = module1.script1:main']
},
package_data={'Config': ['*.json']},
packages=find_packages('src'),
package_dir={'': 'src'})
I am running Anaconda3 on Windows 10. When I run python setup.py install, I am able to run the load_data script without any issue. However, from what I've been reading, it is preferable to use pip install . vice python setup.py install. When I pip install the package and attempt to run load_data, I get ModuleNotFoundError: No module named 'module1.script1'. I've attempted adding 'src' to the front of this, but this doesn't work either. I don't understand what the differences are or how to troubleshoot this.
When building the source distribution for the package not all files are included. Try creating a MANIFEST.in file with
recursive-include src/module1 *
I'm trying get the file VERSION in the root of my python package installed into so that I can read from this file once it's installed, however using MANIFEST.in, the file is placed in the top-level /usr/lib/python3.6/site-packages rather than inside of /usr/lib/python3.6/site-packages/mypackage.
I'm trying to do this so that I can display the packages version at runtime and also easily make use of it inside of the repo.
directory structure:
setup.py
MANIFEST.in
VERSION
mypackage/
- __init__.py
- __main__.py
- foo.py
MANIFEST.in:
include VERSION
setup.py:
#!/usr/bin/env python3
from setuptools import setup, find_packages
with open("VERSION", "r") as versionFile:
version = versionFile.read().strip()
setup(
name="mypackage",
version=version,
packages=find_packages(),
include_package_data=True)
mypackage/__main__.py:
...
verFile = pkg_resources.resource_string(__name__, "VERSION")
with open(verFile, "r") as fin:
version = str(fin.read().strip())
print(version)
...
How can I get the VERSION file to install inside of the package directory?
This is the tree structure of the module I'm writing the setup.py file for:
ls .
LICENSE
README.md
bin
examples
module
scratch
setup.py
tests
tox.ini
I configured my setup.py as follows:
from setuptools import setup, find_packages
setup(
name="package_name",
version="0.1",
packages=find_packages(),
install_requires=[
# [...]
],
extras_require={
# [...]
},
tests_require={
'pytest',
'doctest'
},
scripts=['bin/bootstrap'],
data_files=[
('license', ['LICENSE']),
],
# [...]
# could also include long_description, download_url, classifiers, etc.
)
If I install the package from my python environment (also a virtualenv)
pip install .
the LICENSE file gets correctly installed.
But running tox:
[tox]
envlist = py27, py35
[testenv]
deps =
pytest
git+https://github.com/djc/couchdb-python
docopt
commands = py.test \
{posargs}
I get this error:
running install_data
creating build/bdist.macosx-10.11-x86_64/wheel/leafline-0.1.data
creating build/bdist.macosx-10.11-x86_64/wheel/leafline-0.1.data/data
creating build/bdist.macosx-10.11-x86_64/wheel/leafline-0.1.data/data/license
error: can't copy 'LICENSE': doesn't exist or not a regular file
Removing the data_files part from the setup.py makes tox running correctly.
Your issue here is that setuptools is not able to find the 'LICENSE' file in the files that have been included for building the source distribution. You have 2 options, to tell setuptools to include that file (both have been pointed to here):
Add a MANIFEST.in file (like https://github.com/pypa/sampleproject/)
Use include_package_data=True in your setup.py file.
Using MANIFEST.in is often simpler and easier to verify due to https://pypi.org/project/check-manifest/, making it possible to use automation to verify that things are indeed correct (if you use a VCS like Git or SVN).
pip install . builds a wheel using python setup.py bdist_wheel which is installed by simply unpacking it appropriately, as defined in the Wheel Specification: https://www.python.org/dev/peps/pep-0427/
tox builds a source distribution using python setup.py sdist, which is then unpacked and installed using python setup.py install.
That might be a reason for the difference in behavior for you.
I have some resource files inside my packages which I use during the execution. To make setup store them in a package with python code, I use include_package_data=True and I access them using importlib.resources. You can use backport for an older Python version than 3.7 or another library.
Before each release I have a script which verifies, that all files I need are placed inside a bdist wheel to be sure that everything is on the place.