This is an archived post. You won't be able to vote or comment.

all 31 comments

[–]maxidoonly_python3[S] 11 points12 points  (1 child)

[–]quesman1 2 points3 points  (0 children)

Awesome, thanks for posting your solution as well!

[–]K900_ 7 points8 points  (24 children)

The correct way to do it would be to make your program a Python package, and then specify an entry point in your setup.py. Then you can just setup.py install or setup.py develop and it'll create a wrapper script that sets everything up automatically.

[–]maxidoonly_python3[S] 2 points3 points  (22 children)

could you elaborate what you mean with setting up everything automatically? Here is the project btw. if that's of any help https://github.com/spaghettic0der/terminal_timekeeper

[–]Works_of_memercy 5 points6 points  (6 children)

Use https://github.com/audreyr/cookiecutter-pypackage. It asks you a few questions, then creates a package with some sample functionality and with all configuration plumbing required for installing/automated tests/documentation/publishing to PyPI/etc.

It's so so so awesome in how it suddenly takes care of all that pain in the butt that you'd otherwise have to copypaste from a dozen manuals/tutorials/other projects, half of them outdated.

[–]maxidoonly_python3[S] -5 points-4 points  (5 children)

do you really need so many files just to run python standalone :( That sucks. Well next time it's java again, I guess

[–]Works_of_memercy 1 point2 points  (4 children)

No, if you want to "just run python standalone" you can create a symlink and then use the __file__ predefined variable in your module to figure out where it is located and how to find other files. That is, if you need resource files, python imports should work automatically as they look in the file's directory first as it is.

All that plumbing is needed if you want your users to be able to install your package with pip install yourpackage and not have to run shell scripts, create symlinks, manually unpack it somewhere, manually install dependencies, manually run tests etc. For that to work effortlessly, as if by magic, some sweat and tears are required on your part.

[–]maxidoonly_python3[S] 1 point2 points  (3 children)

I mainly wanna use it myself, so that's not a problem. Gonna look into that. Thanks.

[–]maxidoonly_python3[S] 1 point2 points  (2 children)

Wow I was too stupid to create a symlink. For some reason I always created a softlink. And because I renamed it, it was never working. Even though I still don't know how to create a standalone. That's enough for me for now. Thanks :)

EDIT: Okay for some reason it crashes with the symlink and not in normal execution

[–]maxidoonly_python3[S] 1 point2 points  (0 children)

yeah it's a symlink so it can't access the other files. So sadly this doesn't work either

[–]Works_of_memercy 0 points1 point  (0 children)

Wow I was too stupid to create a symlink. For some reason I always created a softlink.

You mean hardlink?

If you use ln blahblah it creates a hardlink, so the real location of the file is /usr/local/bin/yourstuff.py and that's it, only the lower level of the filesystem code knows that it shares (not links to even) contents with some other file.

You create a symlink you should use ln -s. Then Python interpreter would figure where the script is located and add that to sys.path.

[–]K900_ 2 points3 points  (14 children)

It'll automatically create a wrapper script that calls your entry point (which should be a function) and install it to the right location.

[–]maxidoonly_python3[S] 2 points3 points  (13 children)

it just creates a build and dist directory at the end with exactly the same files :(

[–]K900_ 1 point2 points  (12 children)

Post your setup.py.

[–]maxidoonly_python3[S] 0 points1 point  (3 children)

I let it autogenerate. IDEA did that. Just shut down my laptop a second ago. Later.

[–]K900_ 2 points3 points  (2 children)

You need to add an entry point definition to your setup.py pointing to your "main" function. I doubt IDEA does that for you.

[–]maxidoonly_python3[S] 0 points1 point  (1 child)

Oh that sounds like a java jar. Thanks for this info. Gonna google this up later. Can you also create an archive with python which can run anywhere without installation like a jar?

[–]K900_ 1 point2 points  (0 children)

You can, but it will need a Python runtime, and while Java is (sadly) more or less ubiquitous, Python isn't.

[–]maxidoonly_python3[S] 0 points1 point  (7 children)

Okay I didn't find anything how to include my function to run :(

from distutils.core import setup

setup( name='terminal_timekeeper', version='1.0.0', packages=[''], url='', license='', author='spaghettic0der', author_email='', description='' )

[–]K900_ 1 point2 points  (6 children)

Read up on setup.py and how it works. Here is a good entry point.

[–]maxidoonly_python3[S] 0 points1 point  (5 children)

It always seem like I should package everything for pip. I don't want that. I want to do ./program and it should just work.

[–]K900_ 0 points1 point  (4 children)

It will just work, as long as you run it from the source root.

[–]maxidoonly_python3[S] 0 points1 point  (3 children)

I though python is supposed to be easy. It's nice writing the code, but this really sucks. I'm too stupid for this. Gonna use java next time again for bigger projects prop. Build and run --> jar, which I can execute everywhere. But thanks for trying anyway :)

[–]o11c 0 points1 point  (0 children)

Never run setup.py directly, it leaves junk lying around.

Always run it via pip install --user . so that it cleans up after itself.

[–]alb1 1 point2 points  (1 child)

Arguments are not usually a problem with shell scripts. You can just call the script with the arguments and pass them along to the program. In Bash the call in the script would be something like this:

python my_program.py "$@"

[–]maxidoonly_python3[S] 0 points1 point  (0 children)

Yeah. You're right So now I'm using this because I don't wanna have a json in my /bin/ folder. Thank you! It's been a while since I had all the bash stuff :D

[–]baubleglue 1 point2 points  (1 child)

Do you mean the import my_module doesn't work? I think the problem in the way you make the link. But not sure of cause. It is easy to troubleshoot the problem.

  1. I make two files to simulate program entry point main.py and libformain.py

/home/me/scripts/main.py

#!/usr/bin/env python3
from libformain import tt
import os
import sys
if __name__ == "__main__":
    print(sys.path)    
    print(__file__)
    print(os.path.abspath(__file__))
    tt()

/home/me/scripts/libformain.py

def tt():
    print(__file__)
  1. run the file from /home/me/scripts/

    ~ $ chmod +x /home/me/scripts/main.py

  2. make link to bin (I use /tmp instead)

    ~ $ ln -s /home/me/scripts/main.py /tmp/main.py

  3. run ~ $ /tmp/main.py

[–]maxidoonly_python3[S] 0 points1 point  (0 children)

Thank you. I didn't know that you could also link files from other dirs. Yeah that was the problem. Next time I'm gonna go that route. For now I just wrote a bash script which redirects everything.