pyc file issue

68 views
Skip to first unread message

Todd Widup

unread,
May 26, 2017, 11:12:30 PM5/26/17
to python_in...@googlegroups.com
so I have a few tools I gave over to a friend..gave them the PYC files.

part of what one of the tools does is get the directory path it was executed from to pass along.

anyways, my friend that was running it, kept getting some errors about a path not existing...strange thing was, it was showing the path where I have the tools on my local machine, not where he has them.

so...do PYC files store info like that?  I always thought it was strictly a compiled version of the py file, with nothing else

--
Todd Widup
Creature TD / Technical Artist
todd....@gmail.com

Justin Israel

unread,
May 27, 2017, 1:02:44 AM5/27/17
to python_in...@googlegroups.com


On Sat, May 27, 2017, 3:12 PM Todd Widup <todd....@gmail.com> wrote:
so I have a few tools I gave over to a friend..gave them the PYC files.

part of what one of the tools does is get the directory path it was executed from to pass along.

anyways, my friend that was running it, kept getting some errors about a path not existing...strange thing was, it was showing the path where I have the tools on my local machine, not where he has them.

so...do PYC files store info like that?  I always thought it was strictly a compiled version of the py file, with nothing else


Do you have an example of what kind of code was returning a cached result from when the pyc was generated? 


--
Todd Widup
Creature TD / Technical Artist
todd....@gmail.com

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABBPk36XUNP2PqWnGaq5wgRdrQH%3DgmYf_v%2BmLQi171VbFS5-GA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Todd Widup

unread,
May 27, 2017, 12:42:20 PM5/27/17
to python_in...@googlegroups.com
def getExecutingfile():
    exFile=namedtuple("filePath","path, file")
    path=None
    name=None
    frame=sys._getframe(1)
    fil=frame.f_code.co_filename
    path,name=os.path.split(fil)

    return exFile(path,name)



thats the code I am running  in a util.py file that is called by core.py   in core.py is where it seems to get hard coded to the path I have things sourced from




On Fri, May 26, 2017 at 10:02 PM, Justin Israel <justin...@gmail.com> wrote:


On Sat, May 27, 2017, 3:12 PM Todd Widup <todd....@gmail.com> wrote:
so I have a few tools I gave over to a friend..gave them the PYC files.

part of what one of the tools does is get the directory path it was executed from to pass along.

anyways, my friend that was running it, kept getting some errors about a path not existing...strange thing was, it was showing the path where I have the tools on my local machine, not where he has them.

so...do PYC files store info like that?  I always thought it was strictly a compiled version of the py file, with nothing else


Do you have an example of what kind of code was returning a cached result from when the pyc was generated? 


--
Todd Widup
Creature TD / Technical Artist
todd....@gmail.com

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1NzFWJ5o1%3DAFqK1mc%2Bsf%3DETiw8-s_zt3pZt2rWbJKR2w%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
May 27, 2017, 6:17:10 PM5/27/17
to python_in...@googlegroups.com
On Sun, May 28, 2017 at 4:42 AM Todd Widup <todd....@gmail.com> wrote:
def getExecutingfile():
    exFile=namedtuple("filePath","path, file")
    path=None
    name=None
    frame=sys._getframe(1)
    fil=frame.f_code.co_filename
    path,name=os.path.split(fil)

    return exFile(path,name)



thats the code I am running  in a util.py file that is called by core.py   in core.py is where it seems to get hard coded to the path I have things sourced from

Given that a pyc file has all of its code objects compiled and serialized up front, and that its primary purpose is to optimize subsequent runs of the program as opposed to be used as a distribution format, I suppose it shouldn't be surprising that the filename has already been evaluated and marshalled as part of the code object. It is meant to make sense and be accurate when debugging is performed against the pyc file that lives in the original location relative to the source file. So we could say that while it should work to distribute pyc files to users of the same platform and python version, one could expect certain debugging aspects to not work properly.

Is the intent of this function to get the filename of the module that is the caller of getExecutingfile() (one stack frame back, for debugging purposes)? If so, does this approach work?

    sys._getframe(1).f_globals['__file__']

At least this approach isn't referring to the already-compiled code objects, and is looking at the globals for the frame.

Justin

 




On Fri, May 26, 2017 at 10:02 PM, Justin Israel <justin...@gmail.com> wrote:


On Sat, May 27, 2017, 3:12 PM Todd Widup <todd....@gmail.com> wrote:
so I have a few tools I gave over to a friend..gave them the PYC files.

part of what one of the tools does is get the directory path it was executed from to pass along.

anyways, my friend that was running it, kept getting some errors about a path not existing...strange thing was, it was showing the path where I have the tools on my local machine, not where he has them.

so...do PYC files store info like that?  I always thought it was strictly a compiled version of the py file, with nothing else


Do you have an example of what kind of code was returning a cached result from when the pyc was generated? 


--
Todd Widup
Creature TD / Technical Artist
todd....@gmail.com

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABBPk37XaXLxC6NeMXL0MoqF9NjR5U4zd0YWN64mfrv8G8_b8g%40mail.gmail.com.

Marcus Ottosson

unread,
May 28, 2017, 4:18:54 AM5/28/17
to python_in...@googlegroups.com

How about this?

def getExecutingfile():
  return __file__

Justin Israel

unread,
May 28, 2017, 8:13:52 AM5/28/17
to python_in...@googlegroups.com
That just returns the file of the current module. The original code wants to inspect the calling frame which could be in another module. 

If this utility module is used in some other tool it might look like:

import foo.utils
print foo.utils.getExecutingFile()
# myModule.py

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Todd Widup

unread,
May 28, 2017, 6:22:51 PM5/28/17
to python_in...@googlegroups.com
justin's correct

On Sun, May 28, 2017 at 5:13 AM, Justin Israel <justin...@gmail.com> wrote:


On Sun, May 28, 2017, 8:18 PM Marcus Ottosson <konstr...@gmail.com> wrote:

How about this?

def getExecutingfile():
  return __file__

That just returns the file of the current module. The original code wants to inspect the calling frame which could be in another module. 

If this utility module is used in some other tool it might look like:

import foo.utils
print foo.utils.getExecutingFile()
# myModule.py

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0A07nAE3CLKWUi4%3DiXSDtJatEe9Bo3wPXNjEkyE0dp%2Bg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
May 28, 2017, 6:28:08 PM5/28/17
to python_in...@googlegroups.com


On Mon, May 29, 2017, 10:22 AM Todd Widup <todd....@gmail.com> wrote:
justin's correct

Did that suggestion to use the globals end up working? 


On Sun, May 28, 2017 at 5:13 AM, Justin Israel <justin...@gmail.com> wrote:


On Sun, May 28, 2017, 8:18 PM Marcus Ottosson <konstr...@gmail.com> wrote:

How about this?

def getExecutingfile():
  return __file__

That just returns the file of the current module. The original code wants to inspect the calling frame which could be in another module. 

If this utility module is used in some other tool it might look like:

import foo.utils
print foo.utils.getExecutingFile()
# myModule.py

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABBPk363H_y4AoUCj1ctv-RPQaNcCo0pocik6bN1Tj0%3DK9g7Bg%40mail.gmail.com.

Todd Widup

unread,
May 28, 2017, 6:35:47 PM5/28/17
to python_in...@googlegroups.com
hadnt tried it yet.  been working on another area of this project.  once I drop this update to them, ill add some stuff to use __file__ in and see if that works correctly or not.

hmmm, if I did this

in module Path

fil=__file__

then in module Bob

import Path as pth

test=pth.fil





if I path was a py and Bob was a PYC..you think it would compile like I expect?

On Sun, May 28, 2017 at 3:27 PM, Justin Israel <justin...@gmail.com> wrote:


On Mon, May 29, 2017, 10:22 AM Todd Widup <todd....@gmail.com> wrote:
justin's correct

Did that suggestion to use the globals end up working? 


On Sun, May 28, 2017 at 5:13 AM, Justin Israel <justin...@gmail.com> wrote:


On Sun, May 28, 2017, 8:18 PM Marcus Ottosson <konstr...@gmail.com> wrote:

How about this?

def getExecutingfile():
  return __file__

That just returns the file of the current module. The original code wants to inspect the calling frame which could be in another module. 

If this utility module is used in some other tool it might look like:

import foo.utils
print foo.utils.getExecutingFile()
# myModule.py

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
Todd Widup
Creature TD / Technical Artist
todd....@gmail.com

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1ESdNzO%2BnAzu%2BX_6zK1zK9afesPUdwoWK7EnuYLJ24aw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
May 28, 2017, 6:51:45 PM5/28/17
to python_in...@googlegroups.com
On Mon, May 29, 2017 at 10:35 AM Todd Widup <todd....@gmail.com> wrote:
hadnt tried it yet.  been working on another area of this project.  once I drop this update to them, ill add some stuff to use __file__ in and see if that works correctly or not.

hmmm, if I did this

in module Path

fil=__file__

then in module Bob

import Path as pth

test=pth.fil


Maybe I don't understand the question, but to me it looks like its the exact same thing as just referring to __file__ directly. All it is doing is creating another reference to the __file__ global variable.
 




if I path was a py and Bob was a PYC..you think it would compile like I expect?

On Sun, May 28, 2017 at 3:27 PM, Justin Israel <justin...@gmail.com> wrote:


On Mon, May 29, 2017, 10:22 AM Todd Widup <todd....@gmail.com> wrote:
justin's correct

Did that suggestion to use the globals end up working? 


On Sun, May 28, 2017 at 5:13 AM, Justin Israel <justin...@gmail.com> wrote:


On Sun, May 28, 2017, 8:18 PM Marcus Ottosson <konstr...@gmail.com> wrote:

How about this?

def getExecutingfile():
  return __file__

That just returns the file of the current module. The original code wants to inspect the calling frame which could be in another module. 

If this utility module is used in some other tool it might look like:

import foo.utils
print foo.utils.getExecutingFile()
# myModule.py

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
Todd Widup
Creature TD / Technical Artist
todd....@gmail.com

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABBPk35nZMmkH2Q8RCGLEywb7h1C%2BLbvhGWbvhcjZchj496z8A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages