22

Why do python .pyc files contain the absolute path of their source code, instead of a relative path or something else?

A typical __init__.pyc from Python 2.7 on Ubuntu:

\ufffd\ufffd\ufffdOc@sddlTdS(i\ufffd\ufffd\ufffd\ufffd(t*N(tdbapi2(((s&/usr/lib/python2.7/sqlite3/__init__.py<module>s

10
  • 1
    I just tested with Python 2.7 on Linux and the .pyc file doesn't contain an absolute path. Commented Jun 25, 2012 at 15:00
  • 2
    Then provide the exact steps you followed to create the pyc and see that it contains an absolute path, in a way that anyone can reproduce. What I did was create an empty a.py file, import it from the interactive prompt, and then run strings a.pyc. Commented Jun 25, 2012 at 15:07
  • @interjay In this case it doesn't happen, but if you import from another folder, you'll probably see the full path (just tried). Commented Jun 25, 2012 at 15:15
  • 2
    @martineau Not necessarily true, see PEP-3147. Commented Jun 25, 2012 at 18:41
  • 1
    @Ned Batchelder, trying to achieve reproducible builds with PyInstaller is what drew me to this question. I DO see file paths for the computer that built the final dist from PyInstaller in the .pyc. This is on Windows 10 with Python 3.8 Commented Jul 9, 2020 at 22:14

1 Answer 1

14

To give the information in tracebacks. See for instance http://docs.python.org/library/compileall#cmdoption-compileall-d

Sign up to request clarification or add additional context in comments.

3 Comments

Note that Éric's link leads to the "-d" option, which can be used to get relative paths into .pyc files instead of the usual absolute path.
What would happen if these files are moved to another machine and the paths don't match anymore?
The information reported in some tracebacks would be wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.