If anyone is interested here is my original: dpaste.com/09VPSD6 and the new one suggesting center(): dpaste.com/2NEHDEB attempt at centring in command window
@piRSquared Don't apologise it's great! Especially if you are having a good time. It's just one of the problems I was facing not the only problem I was attempting to solve
I have a package that should be executable (i.e. it has a __main__.py), but I also want to provide a separate script that executes the package. The project structure is essentially like this:
My problem is, where should I put the code for the command line interface? If I put it in command_line_interface.py, I can't access it from __main__.py. If I put it in __main__.py, well, I don't know how to execute the package as __main__ from within python. How do I solve this?
I tried to add another command_line_interface.pyinside the package, but for some reason I couldn't correctly from . import * there. The __init__.py never ran.
btw, you can remove the "As Rawing mentioned in a comment" part if you'd like. I'd rather see an answer with minimal fluff than an answer in which I'm credited :)
@Rawing I have an opinion on crediting folks. It is my opinion and I'm not upset if others don't share it. That preamble out of the way. I think it is important to foster a culture that readily gives credit to those who offer advice, come up with ideas, or generally do useful stuff. I've seen too often others completely rip off ideas as if they are their own and that actually perturbs me. In my opinion, I'd leave your name and credit to you at the expense of purity.
Building comradery and community is worth the price of a little fluff. </end_soapbox>
@Rawing __main__.py should not know about the script outside the package to work correctly. from . import * will not work unless the parent package has already been imported
You'd put the code for the command line interface in a module inside project_package, then call runpy.run_module('project_package') in the script outside the package
@vaultah Do I really need that extra module? Is there something wrong with putting that code into __main__.py and starting it with runpy.run_module('project_package', run_name='__main__')?
In fact I don't even need to set run_name='__main__'; I can just remove the if __name__ == '__main__' check in __main__.py
@Rawing Right, obviously couchpalm. But is it really worth the effort anyway? Especially with the older questions. Probably the OP found the other way round, right?
I don't know, but I don't think there's much traffic on questions older than a few hours. You probably have to consider yourself lucky if you get more than an upvote and a green check mark from the OP.
@vaultah From a programming perspective it's not a hard task to not unpack the iterable while it's passed to a built-in function and destroy the main purpose behind the built-in functions which is giving the user more performance. That's the only thing that came in my mind when the OP asked about. And then I just told you that it's worth to take a look at the source to be 100% sure.
The least thing that you could learn from this short discussion could be a little more thinking out side the box and not just talking based on trained data like a neural net at its best. (just a friendly suggestion though)
Nope, I'm not continuing this discussion here. That behaviour can be fully described by example (as I did), but if you want to look in the source, check the CALL_FUNCTION_EX opcode. Thanks for your friendly suggestion, though. I don't know what made you think I was "talking based on trained data like a neural net at its best" but thanks 👍
@idjaw To be fair, generating a 100 deep nested tuple is fairly impressive stuff in itself... can't imagine someone fairly new to Python managing that by accident...
does anyone know of a good tutorial for creating a setup.py installer for a cli script? I.e. not just a package to be imported by others, but an actual script needing to be installed to system/user path to be used via cli?
i keep find weird tutorials that either assume main() can be called without passing any command-line parameters or that only work on linux and not also on windows
I know nothing about distributing python projects, but something you said sounds weird to me - why can't your main() be called without command-line arguments? Isn't that where you parse the command line arguments?
When I run mypy against this I get this error: "error: Incompatible return value type (got "TextIO", expected "Dict[str, Dict[str, Any]]")". I can't figure out why the return type is TextIO.
oops, sorry I forgot to add the file path in the example, but still same error.
I want my gui to remember certain information until the next time the pc reboots. I am not happy about having to create a text file in the same directory, as I would like it to be hidden. I thought about creating/altering some regedit data, but it might cause some troubles when the gui is ran on a mac.