abspath returns different results when py_compile input path is different in Python?

lampahome pahome.chen at mirlab.org
Mon Feb 12 20:48:16 EST 2018


so py_compile.compile() doesn't normalize the filename, but it will pass
the input filename to co_filename?

2018-02-12 19:27 GMT+08:00 eryk sun <eryksun at gmail.com>:

> On Mon, Feb 12, 2018 at 9:40 AM, lampahome <pahome.chen at mirlab.org> wrote:
> > I want to know abspath of python script followed by steps below.
> >
> >    1. *built it to byte code by py_compile.*
> >    2. *execute it to check abspath.*
> >
> > But I got *2 results* when I execute it.I found the *results based on the
> > path of script* followed by py_compile.
> >
> > Here is my script test.py :
> >
> > import os
> > import inspect
> > print os.path.dirname(os.path.abspath(inspect.getfile(
> inspect.currentframe())))
> >
> > Build it with py_compile, then got 2 results when I enter *different path
> > of test.py*:
> >
> > *enter the folder and compile with only script name.*
> >
> > [~] cd /usr/local/bin/
> > [/usr/local/bin/] python -m py_compile test.py
> > [/usr/local/bin/] cd ~
> > [~] python /usr/local/bin/test.pyc
> > /home/UserXX
> >
> > *In other folder and compile with absolute script name.*
> >
> > [~] python -m py_compile /usr/local/bin/test.py
> > [~] python /usr/local/bin/test.pyc
> > /usr/local/bin
>
> A code object has a co_filename attribute for use in creating
> tracebacks. This path isn't necessarily fully qualified. Here are a
> couple of examples using the built-in compile() function:
>
>     >>> compile('42', 'test.py', 'exec').co_filename
>     'test.py'
>     >>> compile('42', '/usr/local/bin/test.py', 'exec').co_filename
>     '/usr/local/bin/test.py'
>
> py_compile.compile() does not normalize the filename.
>



More information about the Python-list mailing list