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

  • SEARCH
  • Home
  • 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 7843319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:31:18+00:00 2026-06-02T16:31:18+00:00

If you are running a frozen python script (frozen using py2exe) from a directory

  • 0

If you are running a frozen python script (frozen using py2exe) from a directory and drive different from where the script is present, what is the best way to determine the path of the executing script?

Few solutions I have tried

inspect.getfile(inspect.currentframe())

Problem: Does not return the full path. It only returns the script name.

os.path.abspath( __file__ )

Problem: Doesn’t work on Windows

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

Problem: Returns empty string.

os.path.abspath(inspect.getsourcefile(way3))

Will not work if the drive is different from the pwd

os.path.dirname(os.path.realpath(sys.argv[0]))

Will not work if the drive is different from the pwd

Here is a minimal not-working example

D:\>path
PATH=c:\Python27\;c:\Users\abhibhat\Desktop\ToBeRemoved\spam\dist\;c:\gnuwin32\bin

D:\>cat c:\Users\abhibhat\Desktop\ToBeRemoved\spam\eggs.py
import os, inspect, sys
def way1():
    return os.path.dirname(sys.argv[0])

def way2():
    return inspect.getfile(inspect.currentframe())

def way3():
    return os.path.dirname(os.path.realpath(sys.argv[0]))

def way4():
    try:
        return os.path.abspath( __file__ )
    except NameError:
        return "Not Found"
def way5():
    return os.path.abspath(inspect.getsourcefile(way3))

if __name__ == '__main__':
    print "Path to this script is",way1()
    print "Path to this script is",way2()
    print "Path to this script is",way3()
    print "Path to this script is",way4()
    print "Path to this script is",way5()

D:\>eggs
Path to this script is
Path to this script is eggs.py
Path to this script is D:\
Path to this script is Not Found

Related Questions:

  • How to know the path of the running script in Python?
  • How do I get the path and name of the file that is currently executing?
  • python, path of script [closed]

Note

@Fenikso’s solution will work if the script resides on the same drive where you are executing but when its on a different drive, it will not work

  • 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-06-02T16:31:19+00:00Added an answer on June 2, 2026 at 4:31 pm

    Another approach which works with cxFreeze when running from another drive even using PATH:

    import sys
    
    if hasattr(sys, 'frozen'):
        print(sys.executable)
    else:
        print(sys.argv[0])
    

    From Python:

    H:\Python\Examples\cxfreeze\pwdme.py
    

    From command line:

    D:\>h:\Python\Examples\cxfreeze\dist\pwdme.exe
    h:\Python\Examples\cxfreeze\dist\pwdme.exe
    

    Using PATH:

    D:\>pwdme.exe
    h:\Python\Examples\cxfreeze\dist\pwdme.exe
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented a python com server and generate an executable and dll using py2exe
Following on from my question on using frozen Capistrano a couple of days back
Running Python, I have an image and some data calculated for different ROIs (regions
-- Running within ASP.NET MVC alongside jQuery -- I'm using jQuery to render a
Running Mac OS X 10.5.8, with PHP 5.2.11 Pre-installed. Using Coda 1.6.10. I'm writing
Can you deploy a Rails3 app using Bundler's Gemfile WITHOUT running bundle install ...
We're using gems:unpack to ensure gem version consistency across environments. However, we're running into:
I was using a version of v2 of protobuf-net from a few weeks ago
I'm running an AJAX request from a JavaScript-powered (+jQuery) webpage every 5 seconds for
So I'm developing an iPhone game using cocos2d. I'm using pushScene to transition from

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.