I have a project with the below structure:
projectname/projectname/__main__.py
I execute the program using python -m projectname.
If I want to install it locally in my system so that I can just call projectname, How can I achieve this?
You'll want to make a file setup.py in the top-level projectname that installs the package and adds some command (e.g. yeet) to your path. That command will call some function inside projectname/__main__.py:
from setuptools import setup
setup(
name='mypackagename',
version='0.0.1',
packages=['mypackagename'],
install_requires=[
'tensorflow>=2.0.0', # put your modules from requirements.txt here
],
entry_points={
'console_scripts': [
'yeet=projectname:function_to_run',
],
},
)
Related
I have a GUI Python app that I'm trying to distribute a desktop entry with. Normally, one would write a setup.py with setuptools that has this in it:
from setuptools import setup
setup(
name = 'myapp',
version = '0.0.1',
packages = ['myapp'],
data_files = [
('share/applications', ['myapp.desktop']),
],
)
This is deprecated, however, and my goal is to use only pyproject.toml in my repo with no setup.py or setup.cfg needed. I have been unable to find any information on how I would go about doing this.
Environment
PC1: Dev-machine, online, Python install dir: C:\Python310
PC2: Target machine, offline, Python install dir: C:\Program Files\Python310
Doing
Write source and run command in workdir pip install -t ./out ./ on PC1.
Copy files under out dir from PC1 to PC2.
Open term and invoke exe file on PC2.
Then I got message Fatal error in launcher: Unable to create process using '"C:\Python310\python.exe" "C:\Program Files\Python310\Scripts\my_app.exe" ': ??????????????????.
How can I build for PC2?
Folder structure
┗━ my_app
┣━ setup.py
┗━ my_app
┣━ __init__.py
┣━ __main__.py
┗━ main.py
File contents:
setup.py
from setuptools import setup, find_packages
setup(
name = 'my_app',
packages = find_packages(),
entry_points = {
'console_scripts': [
'my_app = my_app.main:main',
],
},
)
my_app/__main__.py
from .main import main
main()
my_app/main.py
def main():
print('hello world')
Constraints
without cx_freeze, pyinstaller, py2exe or similer third party packages
actual my_app requires external packages(ex: tqdm)
Run command python setup.py bdist and copy dist/my_app...zip content to target machine, its resolved my question.
https://docs.python.org/3/distutils/builtdist.html
I have a small python script (without modules) that I want to package via pex and execute. However, i could not find any option in the documentation (https://pex.readthedocs.io/en/latest/buildingpex.html) to package script without an entry point
I am using the following command
pex requests ./app -o app.pex --python-shebang '#!/usr/bin/env python3.9'
You can use setuptools with PEX.
Flow setup.py sample:
from setuptools import setup, find_packages
setup(
name='cli',
version='0.1.0',
packages=find_packages(),
include_package_data=True,
install_requires=[
'Click',
],
entry_points={
'console_scripts': [
'cli=pkgname.main:cli',
],
}
)
And run this in terminal:
$ python setup.py bdist_pex --bdist-all
I figured out that it is not possible hence i created a module and a main function.
Using pip, you can easily install a package in tar form as in:
pip install https://path/to/respository/ending/with/en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz
However, I can't seem to get python setup.py install to find this same remote repository.
In setup.py, I have:
from setuptools import setup
setup(name='blah',
version='0.1.0',
description='A library',
install_requires=[
'en_core_web_sm-2.1.0.tar.gz'
],
dependency_links=[
'https://path/to/respository/ending/with/en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz'
],
packages=['blah'])
My error message is:
No local packages or working download links found for en_core_web_sm-2.1.0.tar.gz
error: Could not find suitable distribution for Requirement.parse('en_core_web_sm-2.1.0.tar.gz')
How can I accomplish the same with setup.py that I can with pip?
Figured it out. This tip from the spacy docs:
https://spacy.io/usage/models#production
You need to add #egg=en_core_web_sm to the end of the dependency_link. Final file looks like this:
from setuptools import setup
setup(name='blah',
version='0.1.0',
description='A library',
install_requires=[
'en-core-web-sm'
],
dependency_links=[
'https://path/to/respository/ending/with/en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz#egg=en_core_web_sm'
],
packages=['blah'])
When trying to run develop or install task of setuptools I am getting the Relative module names not supported error.
The command run is $ python -m setup.py develop
My setup.py script is pretty simple with one entry point:
setup(
name='foo',
version='1.2.3',
# ...
include_package_data=True,
packages=find_packages(),
entry_points={
'console_scripts': [
'foo = somepkg.somemodule:mainfunc'
]
},
install_requires=['requests',],
setup_requires=['pytest-runner'],
tests_require=['pytest', 'betamax', 'flexmock']
)
The issue was solved by not running setup.py as a module, i.e. running
$ python setup.py develop
instead of
$ python -m setup.py develop