Subfolder in Python package not visible when installed - python

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(),
...
)

Related

How do you add a package install requirement on python to upload my package to PyPi?

So what i mean by the title is like this, I'm gonna create a python package and it needs pygame for my package to work, but I can't figure out how do you add that in setup.py
from setuptools import setup
setup(
...
install_reqs=[
"pygame",
],
...
)
I change install_reqs into???
install_requires.
See documentation of the setup function

Python module with requirements from test repo

For some internal development I created a package that depends on another package that is currently under development.
I would like to add in my setup.py requirement for the test version of that package, that is:
setuptools.setup(
...
install_requires=[...], #from pypi
??install_requires_dev??=[...], #from pypi-test
)
Is this possible?
in your setup.py script your going to want to set that package as an external dependancy
#module/setup.py
from setuptools import setup
setup(
name='foo',
version='1.0',
description='A useful module',
author='Man Foo',
author_email='foomail#foo.com',
packages=['foo'], #same as name
install_requires=['bar', 'greek'], #external packages as dependencies
)
replace bar or greek with your module name, this runs pip3 install module every time so if you update the external package this module will reinstall the package automatically when you run the module.
also if your scripts are in a scripts folder inside the module you will have to add a scripts attribute so your code would look more like this.
#module/setup.py
from setuptools import setup
setup(
name='foo',
version='1.0',
description='A useful module',
author='Man Foo',
author_email='foomail#foo.com',
packages=['foo'], #same as name
install_requires=['bar', 'greek'], #external packages as dependencies
scripts=[scripts/script1, scripts/script2]
)

Python packaging with setuptools does not include my source code when installing

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

How to add/install python libraries to my github project?

I'm building my first project on GitHub and my python src code uses an open-source, 3rd-party library that I have installed on my computer. However, I heard it is best practice to create a dep (dependencies) folder to store any additional libraries I would need. How do I actually install the libraries in the dep folder and use them from there instead of my main computer?
You have to create a requirements.txt file with each package on a separate line. e.g.
pandas==0.24.2
You also might want to add a setup.py to your python package. In the setup you have to use "install_requires" argument. Although install_requires will not install packages when installing your package but will let the user know which packages are needed. The user can refer to the requirements.txt to see the requirements.
You can check it here: https://packaging.python.org/discussions/install-requires-vs-requirements/
The following is an example of setup.py file:
from distutils.core import setup
from setuptools import find_packages
setup(
name='foobar',
version='0.0',
packages=find_packages(),
url='',
license='',
author='foo bar',
author_email='foobar#gmail.com',
description='A package for ...'
install_requires=['A','B']
)
Never heard about installing additional libraries in a dependencies folder.
Create a setup python file in your root folder if you don't already have it, in there you can define what packages (libraries as you call them) your project needs. This is a simple setup file for example:
from setuptools import setup, find_packages
setup(
name = "yourpackage",
version = "1.2.0",
description = "Simple description",
packages = find_packages(),
install_requires = ['matplotlib'] # Example of external package
)
When installing a package that has this setup file it automatically also install every requirement in your VENV. And if you're using pycharm then it also warns you if there's a requirement that's not installed.

How to install a dependency from a submodule in Python?

I have a Python project with the following structure (irrelevant source files omitted for simplicity):
myproject/
mysubmodule/
setup.py
setup.py
The file myproject/setup.py uses distutils.core.setup to install the module myproject and the relevant sources. However, myproject requires mysubmodule to be installed (this is a git submodule). So what I am doing right now is:
myproject/$ cd mysubmodule
myproject/mysubmodule/$ python setup.py install
myproject/mysubmodule/$ cd ..
myproject/$ python setup.py install
This is too tedious for customers, especially if the project will be extended by further submodules in the future.
Is there a way to automate the installation of mysubmodule when calling myproject/setup.py?
setuptools.find_packages() is able to discover submodules
Your setup.py should look like
from setuptools import setup, find_packages
setup(
packages=find_packages(),
# ...
)
Create a package for mysubmodule with its own setup.py and let the top-level package depend on that package in its setup.py. This means you only need to make the packages / dependencies available and run python setup.py install on the top-level package.
The question then becomes how to ship the dependencies / packages to your customers but this can be solved by putting them in a directory and configuring setup.py to include that directory when searching for dependencies.
The alternative is to "vendor" mysubmodule which simply means including it all in one package (no further questions asked) and having one python setup.py install to install the main package. For example, pip vendors (includes) requests so it can use it without having to depend on that requests package.

Categories