Title: arguments and default path not set in and
Type: Stage:
Components: None Versions: Python 2.5
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, gjb1002, haridsv, hvendelbo, mattheww, tim.golden
Priority: normal Keywords:

Created on 2008-05-26 16:17 by hvendelbo, last changed 2010-09-30 10:02 by tim.golden. This issue is now closed.

Messages (10)
msg67372 - (view) Author: Henrik Vendelbo (hvendelbo) * Date: 2008-05-26 16:17
If you want to use as a regular python script,
altering behaviour of calling the python exe, there is a significant

sys.argv isn't assigned until after completes, and the directory
of the script to execute is added to sys.path after completes as

If it cannot be introduced for backwards compatibility, there should at
least be a way to access the information
msg84642 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2009-03-30 20:40
This cannot change as sys.argv needs to be set after Python initializes
to strip out what arguments were meant for the interpreter and not the
script being run. All of this also needs to happen with site executing
first, so this cannot be changed.
msg87665 - (view) Author: Hari Krishna Dara (haridsv) Date: 2009-05-13 01:36
I hit exactly this same problem today trying to customize sys.path. We
conventionally use .pth file under site-packages to add our custom
library paths, but this is not convenient in development while switching
between branches/checkout directories, so I would like something like
this to work:

def updateADPath(dir):
    """ Determine the root of the AD code given the specific sub
directory and update sys.path. Returns 0 on success. """

# First try the current directory. If not found, try the main script's
if updateADPath(os.getcwd()):
    # If not absolute path, the script is in the current dir anyway,
which we have already tried.
    if len(sys.argv) > 0 and sys.argv[0] != '-c' and

It works quite well as long as I run when the CWD is with in the branch
directory, but if it is outside (such as when running from a windows
shortcut) there is no way to determine the root. Is there an alternative
means to access the raw command-line arguments, it would help me provide
a partial workaround for this problem.
msg117442 - (view) Author: Geoffrey Bache (gjb1002) Date: 2010-09-27 13:06
I also just ran into this. Is it likely that an enhancement request to provide access to the "raw" command line, as requested by the previous commenter, would be accepted? It's sometimes useful to have some idea about what kind of Python process is being started at this point.
msg117443 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2010-09-27 13:20
Merely from a Windows point-of-view, you could get the full
command line fairly easily:


import ctypes
pstring = ctypes.windll.kernel32.GetCommandLineW ()
print (ctypes.c_wchar_p (pstring).value)


msg117445 - (view) Author: Geoffrey Bache (gjb1002) Date: 2010-09-27 13:52
Interesting. Any idea if something similar is possible on Linux?
msg117446 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2010-09-27 14:05
I'm afraid I don't know; might be worth asking that on the main python mailing list.
msg117542 - (view) Author: Matthew Woodcraft (mattheww) Date: 2010-09-28 20:05
open("/proc/self/cmdline").read() should work on linux (note that the arguments are separated by NULs).
msg117712 - (view) Author: Geoffrey Bache (gjb1002) Date: 2010-09-30 09:04
Thanks for the tips, looks like we have the basis for a solid workaround here. Perhaps that could be encapsulated and added as sys.raw_argv or something in future?
msg117715 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2010-09-30 10:02
Suggest you raise a separate feature-request issue for that, Geoff, perhaps offering the two implementations described. Perhaps raise it it on python-ideas first to gauge reactions. I'm +0 myself since it's so easy to do anyway and I don't know how common a requirement it would be.
Date User Action Args
2010-09-30 10:02:54tim.goldensetmessages: + msg117715
2010-09-30 09:04:40gjb1002setmessages: + msg117712
2010-09-28 20:05:20matthewwsetnosy: + mattheww
messages: + msg117542
2010-09-27 14:05:37tim.goldensetmessages: + msg117446
2010-09-27 13:52:59gjb1002setmessages: + msg117445
2010-09-27 13:20:53tim.goldensetnosy: + tim.golden
messages: + msg117443
2010-09-27 13:06:13gjb1002setnosy: + gjb1002
messages: + msg117442
2009-05-13 01:36:44haridsvsetnosy: + haridsv
messages: + msg87665
2009-03-30 20:40:17brett.cannonsetstatus: open -> closed
resolution: wont fix
messages: + msg84642
2009-03-28 20:08:18brett.cannonsetpriority: normal
2009-02-11 04:46:34ajaksu2setassignee: brett.cannon
nosy: + brett.cannon
2008-05-26 16:17:15hvendelbocreate