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
Related
I'm on MacOSX (12.0.1) and with Python 3.9. I want to create a simple python package for personal use. Upon creating the package using python setup.py install, almost everything works: I can import the package when using python, etc. However, I've tried to follow every tutorial online to create an associated executable script. I.e., a command that I can execute from the shell that contains some functionality from the package I made. However, nothing has worked.
My setup.py code:
from setuptools import setup
setup(name='my_package',
version='1.0.0',
description='heeheehoohoo',
author='Me',
author_email='me#me',
url='me.com',
packages=['my_package'],
entry_points={
'console_scripts': ['mypkg=my_package:run']},
install_requires=['cairosvg',
'selenium',
'PyPDF2',
],
include_package_data=True,
zip_safe=False
)
And under my_package/__init__.py I have:
from . mine import main
def run():
import argparse
parser = argparse.ArgumentParser(prog = 'eeeeeee', description = 'eeeeee')
parser.add_argument('eeeeee', help = 'eeeeeee')
args = parser.parse_args()
print(f'eeeee ...')
main(args.eeeeeee)
print(f'Success!')
Everything gets installed, yet for some reason when I try to execute $ mypkg, I get zsh: command not found: mypkg. From python, I can import the function and directly try to execute run(). And strangest of all, each tutorial I have seen that has done anything like this can execute the commands without a problem once they'd executed python setup.py install.
Thank you!
Setting pip to the respective version of python and using pip install . instead of python setup.py install did the trick. However, it's still strange that python setup.py install does not work...
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
All of the cx_Freeze examples are for one file (module). I need to make an executable for an entire python package. Why is that hard to make?
Here's my directory:
test1/
__init__
__main__
The way I run this from the command line is by using the following cmd
python -m test1
__init__ is empty and __main__ just have a simple print statement.
I am using python 3.5.1 but I can switch to python 3.4 if that would fix the problem
Here is my setup.py for win64
from cx_Freeze import setup, Executable
import sys
build_exe_options = {"packages": ['test1'],
"include_files": []
}
executables = [
Executable("__main__")
]
setup(
name = "Foo",
version = "0.1",
description = "Help please!",
author = "me",
options = {"build_exe": build_exe_options},
executables = executables
)
Update:
1- see the comment below for solution for this approach
2- switch to pyinstaller because it can produce one exe file not a folder
Freezing a whole package doesn' t make sense because to create an executable binary you will want a Python script that can be run standalone from the command line. A package normally isn't started out-of-the-box but would get imported by another module.
However you can always import a package in your script so when you freeze it the package gets included in your distribution.
So do something like this:
test1/
__init__
__main__
run_test.py
run_test.py now imports test1 and starts your function that does whatever you want.
import test1
run_the_whole_thing()
Note: You will need to change your Executable in the setup.py to run_test.py.
Ok so I'm trying to use py2app to generate a distribution for my project. I'm still not sure I get the hang of it tho. So my setup.py looks like this:
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
import setuptools
PACKAGES = ['sqlalchemy.dialects.sqlite']
MODULES = ['sqlite3']
APP = ['tvb/interfaces/web/run.py']
OPTIONS = {'argv_emulation': True,
'packages': PACKAGES ,
'includes' : MODULES }
DATA_FILES = []
setup(
app=APP,
data_files=DATA_FILES,
packages = setuptools.find_packages(),
include_package_data=True,
options={'py2app': OPTIONS},
setup_requires=['py2app', "pyopengl", "cherrypy", "sqlalchemy", "simplejson",
"formencode", "genshi", "quantities","numpy", "scipy",
"numexpr", "nibabel", "cfflib", "mdp", "apscheduler",
"scikits.learn"]
)
So my first question would be: What should I include in my MODULES for py2app here? Does py2app know to scan for the things in setup_requires and include them or do I need to add some entries for them in MODULES ?
Another problem is that I'm getting an: sqlalchemy.exc.ArgumentError: Could not determine dialect for 'sqlite' when trying to run my app. After lots of googling I only saw that for py2exe you need to include the sqlalchemy.dialects.sqlite as a package but it doesn't seem to work for me. Am I missing something here?
The last one is that I'm getting a: malformed object (load command 3 cmdsize not a multiple of 8) just before the python setup.py py2app. Is this normal?
Regards,
Bogdan
Well seems I got the whole thing wrong.
'includes' : ['sqlalchemy.dialects.sqlite']
Instead of packages, and that seems to have done the trick.
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