Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 724221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:11:50+00:00 2026-05-14T06:11:50+00:00

Is there a universal approach in Python, to find out the path to the

  • 0

Is there a universal approach in Python, to find out the path to the file that is currently executing?

Failing approaches

path = os.path.abspath(os.path.dirname(sys.argv[0]))

This does not work if you are running from another Python script in another directory, for example by using execfile in 2.x.

path = os.path.abspath(os.path.dirname(__file__))

I found that this doesn’t work in the following cases:

  • py2exe doesn’t have a __file__ attribute, although there is a workaround
  • When the code is run from IDLE using execute(), in which case there is no __file__ attribute
  • On Mac OS X v10.6 (Snow Leopard), I get NameError: global name '__file__' is not defined

Test case

Directory tree

C:.
|   a.py
\---subdir
        b.py

Content of a.py

#! /usr/bin/env python
import os, sys

print "a.py: sys.argv[0]=", sys.argv[0]
print "a.py: __file__=", __file__
print "a.py: os.getcwd()=", os.getcwd()
print

execfile("subdir/b.py")

Content of subdir/b.py

#! /usr/bin/env python
import os, sys

print "b.py: sys.argv[0]=", sys.argv[0]
print "b.py: __file__=", __file__
print "b.py: os.getcwd()=", os.getcwd()
print

Output of python a.py (on Windows)

a.py: __file__= a.py
a.py: os.getcwd()= C:\zzz

b.py: sys.argv[0]= a.py
b.py: __file__= a.py
b.py: os.getcwd()= C:\zzz

Related (but these answers are incomplete)

  • Find path to currently running file
  • Path to current file depends on how I execute the program
  • How can I know the path of the running script in Python?
  • Change directory to the directory of a Python script
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:11:51+00:00Added an answer on May 14, 2026 at 6:11 am

    You can’t directly determine the location of the main script being executed. After all, sometimes the script didn’t come from a file at all. For example, it could come from the interactive interpreter or dynamically generated code stored only in memory.

    However, you can reliably determine the location of a module, since modules are always loaded from a file. If you create a module with the following code and put it in the same directory as your main script, then the main script can import the module and use that to locate itself.

    some_path/module_locator.py:

    def we_are_frozen():
        # All of the modules are built-in to the interpreter, e.g., by py2exe
        return hasattr(sys, "frozen")
    
    def module_path():
        encoding = sys.getfilesystemencoding()
        if we_are_frozen():
            return os.path.dirname(unicode(sys.executable, encoding))
        return os.path.dirname(unicode(__file__, encoding))
    

    some_path/main.py:

    import module_locator
    my_path = module_locator.module_path()
    

    If you have several main scripts in different directories, you may need more than one copy of module_locator.

    Of course, if your main script is loaded by some other tool that doesn’t let you import modules that are co-located with your script, then you’re out of luck. In cases like that, the information you’re after simply doesn’t exist anywhere in your program. Your best bet would be to file a bug with the authors of the tool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 387k
  • Answers 387k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Make sure you're delivering all content including external CSS, JS… May 15, 2026 at 12:11 am
  • Editorial Team
    Editorial Team added an answer $ python -c'from BeautifulSoup import BeautifulSoup > print BeautifulSoup("""<html>Preheat oven… May 15, 2026 at 12:11 am
  • Editorial Team
    Editorial Team added an answer Take a look at the BackgroundWorker May 15, 2026 at 12:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.