How to know the path the script the python run?

sys.arg[0] gives me the python script. For example 'python hello.py' returns hello.py for sys.arg[0]. But I need to know where the hello.py is located in full path.

How can I do that with python?


import sys print(sys.path[0])

From the docs:

As initialized upon program startup, the first item of this list, sys.path[0], is the directory containing the script that was used to invoke the Python interpreter.




You can use __file__, a variable that contains the full path to the module from which you access it. This doesn't necessarily have to end with the ".py" extension, but can also be ".pyc" (or None).

There is also documentation available on __file__.


  • executable python file sys not accepting '(' character
  • Getting PyGame to import in Python interpreter after MacPorts install
  • Python2.7 default float precision [duplicate]
  • Why does nailgun cache command line arguments?
  • Sharing Redis settings across routes
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • What do I do with this error when I run tests in rails?
  • MRO with multiple inheritance in python
  • MeeGo Handset Emulator not starting on Windows 7
  • import scipy.sparse failed
  • How Get arguments value using inline assembly in C without Glibc?
  • Memory error in python- how to use more memory
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • Differences in dis-assembled C code of GCC and Borland?
  • Zurb Foundation _global.scss meta styles for js?
  • How to get Eclipse Oxygen to run on Java 9
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Algorithm for a smudge tool?
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • Can Jackson SerializationFeature be overridden per field or class?
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Importing jscolor library in angular 2
  • Why winpcap requires both .lib and .dll to run?
  • QuartzCore.framework for Mono Develop
  • Return words with double consecutive letters
  • RestKit - RKRequestDelegate does not exist
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • A cron job substitute?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Java static initializers and reflection
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • How to push additional view controllers onto NavigationController but keep the TabBar?