Why does "eggsecutable" search for __main__ - python

I try to make a executable *.egg file. I can create this using the following method: I just put a __main__.py at the top-level of an .egg named .zip, and python will run that __main__.py
I have read that there is a more elegant way:
setup(
# other arguments here...
entry_points={
'setuptools.installation': [
'eggsecutable = my_package.some_module:main_func',
]
}
)
https://setuptools.readthedocs.io/en/latest/setuptools.html#eggsecutable-scripts
But if I create ( with run setup.py bdist_egg) and run the *.egg, it prints:
C:\Python27\python.exe: can't find '__main__' module in <eggpath>
So python doesn't find the entry point.
Is it possible make an executable egg without explicit __main__.py?
System:
Win 7
Python 2.7.9
setuptools 39.0.1 from c:\python27\lib\site-packages (Python 2.7))
UPDATE
I have tried both on Linux both with python3 and I got the same error.

It seems like the entry points documentation is misleading and you don't need them.
What you probably want something like this:
setup.py:
import setuptools
setuptools.setup(
name="example_pkg",
version="0.0.1",
# all the other parameters
# function to call on $ python my.egg
py_modules=['example_pkg.stuff:main']
)
example_pkg/stuff.py
def main():
print("egg test")
if __name__ == '__main__':
main()
create the egg: setup.py bdist_egg
run the egg: python dist\example_pkg-0.0.1-py3.6.egg
output: egg test
solution source: https://mail.python.org/pipermail/distutils-sig/2015-June/026524.html

Related

Why cant I import my package created using setuptools?

I am working on a project (CN_Analysis) for which I would like to create my own python package (cn_tools) using setuptools. My goal is to have it accessible everywhere in my project folder. However, when I try to import it from a subfolder (e.g. CN_Analysis/Notebooks), I get
(.virtualenvironment) ...:~/Workspace/CN_Analysis/Notebooks$ python3
import cn_tools
ModuleNotFoundError: No module named 'cn_tools'
The directory structure is as follows:
CN_Analysis
├──README.md\
├──requirements.txt\
├──.gitignore\
├──setup.py\
├──.virtualenvironment/\
├──Notebooks/\
├──Data/\
├──cn_tools/\
| ├──__init__.py\
| ├──my_tools.py
The contents of setup.py are:
from setuptools import setup, find_packages
setup(name = 'cn_tools',
version = '0.1',
description = 'This package contains helpful functions for the processing data obtained from Karambola.',
packages=find_packages(where='cn_tools'),
package_dir={'': 'cn_tools'})
Additional information:
The basic routine is
source .virtualenvironment/bin/activate
(.virtualenvironment) python3 setup.py develop
Results in
Installed /home/ansgar/Workspace/CN_Analysis/cn_tools\
Processing dependencies for cn-tools==0.1\
Finished processing dependencies for cn-tools==0.1
Check for python3
(.virtualenvironment) which python3
/home/my_name/Workspace/CN_Analysis/.virtualenvironment/bin/python3
And if I call sys.path from python after I navigated to a subfolder (e.g Notebooks/), it returns
['',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/my_name/Workspace/CN_Analysis/.virtualenvironment/lib/python3.8/site-packages',
'/home/my_name/Workspace/CN_Analysis/cn_tools']
Does someone know why I cannot import cn_tools?
It works if I just use
packages=find_packages()
instead of
packages=find_packages(where='cn_tools')
in the setup.py file.

Python module cli bash: command not found

I'm trying to create a cli tool using python but whenever I try to run the command in terminal i get the error 'bash: command not found'. The python modules installs without any errors. I'm on macos and using python version: 3.9.4. I think this may be a PATH issue but currently unsure.
Here is the setup.py:
from setuptools import setup
setup(
name = 'mycommand',
version = '0.1.0',
packages = ['mycommand'],
entry_points = {
'console_scripts': [
'mycommand = mycommand.__main__:main'
]
}
)
Here is the main.py:
import sys
def main():
print('in main')
args = sys.argv[1:]
print('count of args :: {}'.format(len(args)))
for arg in args:
print('passed argument :: {}'.format(arg))
if __name__ == '__main__':
main()
Hope somebody can help me with this.
This is the tutorial I have followed: https://trstringer.com/easy-and-nice-python-cli/
if your layout is like that:
setup.py
mycommand/main.py
then the entrypoint is simply mycommand.main:main:
mycommand/main.py -> mycommand.main
:main -> def main()
You need to use the scripts keyword argument.
First add a file called mycommand (note that you should not have any extension) in the same directory as main.py
#!/usr/bin/env python
from mycommand.main import main
main()
then edit your setup.py like this:
from setuptools import setup
setup(
name = 'mycommand',
version = '0.1.0',
packages = ['mycommand'],
scripts=['mycommand/mycommand'],
)
Then when you install this package you will be able to call mycommand

Make CLI with argparse and setuptools

I have a module sfind.py. How can I do this so that I can run the script by the file name without the py prefix?
python sfind instead python sfind.py
sfind.py
import argparse
def createParser():
some logic
return parser
def main(namespace):
some logic
if __name__ == '__main__':
parser = createParser()
namespace = parser.parse_args()
main(namespace)
I tried to do it with setuptools, maked new file setup.py in same directory.
setup.py
from setuptools import setup
setup(
name='sfind',
version='0.1',
packages=['sfind'],
entry_points={
'console_scripts': [
'sfind=sfind:__main__'
]
})
But error occurred
python: can't open file 'sfind': [Errno 2] No such file or directory
Can anybody help me do it right?
File structure:
-sfind
--__init__.py
--__main__.py
--sfind.py
--test.py
You don't need setuptools to call your module as python sfind. Setuptools will allow you to call by just sfind. To call it with python sfind, you have to make your sfind a module and you can do this by making a python directory sfind with __init__.py, __main__.py, and sfind.py in the directory. Then, you can call with just python sfind. If you just want to call it with sfind, you can use setup tools, but it is recommended you provide callable function (e.g. sfind=sfind.sfind:main) instead of module itself (__main__) as your entry_points. You can then do either python setup.py install or python setup.py sdist then pip install.

Cannot use entry_points

I am trying to learn about entry_points and how to use Python from the command line. My OS is Linux (Mint 15), and I tried unsuccessfully with both Python 2.7.4 and 3.3.1 -- with virtualenv.
foo/setup.py:
setup(
name='foo',
version='0.0.1',
description='foo',
url='http://www....',
author='Foo',
author_email='xxx#xxx.com',
install_requires = ['docopt', 'termcolor'],
packages = ['foo'],
entry_points = {
'console_script': [
'foo = foo.main:start'
],
},
)
foo\foo\main.py:
def start():
print 'test'
foo\foo\__init.py__: empty
I installed with: python setup.py develop
(learn27)user#machine /data/apps/learn27/rocks $ python setup.py develop
running develop
... more output here
But when trying to run "foo" from the command line, it simply says "command not found". I could not find any file named "foo" onthe file system.
As far as I understand, I was expecting the generated executables to be located in the bin folder here:
>>> print distutils.sysconfig.get_config_var('prefix')
/data/apps/virtenvs/learn27
Thanks in advance for any help!
The entry point is called console_scripts, plural, you misspelled it as console_script (singular).
See Automatic Script Creation in the setuptools documentation.
You may have to add it to your PATH

Python - install script to system

how can I make setup.py file for my own script? I have to make my script global.
(add it to /usr/bin) so I could run it from console just type: scriptName arguments.
OS: Linux.
EDIT:
Now my script is installable, but how can i make it global? So that i could run it from console just name typing.
EDIT: This answer deals only with installing executable scripts into /usr/bin. I assume you have basic knowledge on how setup.py files work.
Create your script and place it in your project like this:
yourprojectdir/
setup.py
scripts/
myscript.sh
In your setup.py file do this:
from setuptools import setup
# you may need setuptools instead of distutils
setup(
# basic stuff here
scripts = [
'scripts/myscript.sh'
]
)
Then type
python setup.py install
Basically that's it. There's a chance that your script will land not exactly in /usr/bin, but in some other directory. If this is the case, type
python setup.py install --help
and search for --install-scripts parameter and friends.
I know that this question is quite old, but just in case, I post how I solved the problem for myself, that was wanting to setup a package for PyPI, that, when installing it with pip, would install it as a system package, not just for Python.
setup(
# rest of setup
console_scripts={
'console_scripts': [
'<app> = <package>.<app>:main'
]
},
)
Details

Categories

Resources