Discussion:
[Maya-Python] pyc file issue
Todd Widup
8 years ago
Permalink
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
***@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+***@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.
Justin Israel
8 years ago
Permalink
...
Do you have an example of what kind of code was returning a cached result
from when the pyc was generated?
...
--
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+***@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.
Todd Widup
8 years ago
Permalink
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
...
--
Todd Widup
Creature TD / Technical Artist
***@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+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABBPk37XaXLxC6NeMXL0MoqF9NjR5U4zd0YWN64mfrv8G8_b8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
8 years ago
Permalink
Post by Todd Widup
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
...
--
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+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1BXzHec6o54JRJpHT%3Dh410zuygGkqWEt%3DANh5ba5OUZA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Marcus Ottosson
8 years ago
Permalink
How about this?

def getExecutingfile():
return __file__

​
​
--
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+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAtghUXNDCX3Ww7uypxMDiYC_cTi2L-mcZjR7k247vPbg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
8 years ago
Permalink
Post by Marcus Ottosson
How about this?
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+***@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.
Todd Widup
8 years ago
Permalink
justin's correct
...
--
Todd Widup
Creature TD / Technical Artist
***@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+***@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.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
8 years ago
Permalink
Post by Todd Widup
justin's correct
Did that suggestion to use the globals end up working?
...
--
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+***@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.
Todd Widup
8 years ago
Permalink
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?
...
--
Todd Widup
Creature TD / Technical Artist
***@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+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABBPk35nZMmkH2Q8RCGLEywb7h1C%2BLbvhGWbvhcjZchj496z8A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
8 years ago
Permalink
Post by Todd Widup
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.
...
--
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+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3NYChB_DfVsf%2B9nZ9626qCxeNZqCZn2CVB2eoqa8AOkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Search results for '[Maya-Python] pyc file issue' (Questions and Answers)
4
replies
Choosing a language: questions on Python?
started 14 years ago
programming & design
Loading...