0

So I want to make myself a module that at some point needs the name of the python script that is importing my module, like argparse does to make the help argument.

ex.

python script.py -h

     USAGE: script.py [-h] [-arg1] [-arg2] . . .

How does argparse acces the name of the script that is importing?

2 Answers 2

1

sys.argv[0] is the name of the "main" script.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe you can use the built-in __file__ to retrieve the name of the file being run. If you need just the filename and not the path, use os.basename() to strip the path.

2 Comments

Seems like he asked for the path of the file importing the file in question. That doesn't necessarily have to be the file currently being run.
it doesn't give the name of the script that is importing the name both file and os.path.basename(file) give , basically, the name/ path of the module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.