24

I can't seem to figure out how to import a compiled .pyc module into my code so I can use it within my main script. Is this even possible?

Jonno
  • 767
  • 2
  • 7
  • 13
  • Just FYI. PyDev has a bug with importing pyc file. http://stackoverflow.com/questions/13967342/pydev-and-pyc-files – smwikipedia Oct 19 '16 at 05:04

6 Answers6

23

If there is foo.pyc, import foo will automatically use foo.pyc whether foo.py exists or not

(If foo.py is newer, it will be used)

http://docs.python.org/tutorial/modules.html

Intra
  • 2,089
  • 3
  • 19
  • 23
  • Import wasn't working for me in the interpreter (IPython). I tested it in a script and it worked. Can it be done in the interpreter? – Jonno Mar 28 '12 at 18:56
  • 1
    Never mind. Found the problem, I was in the wrong working dir. Thanks for the help. – Jonno Mar 28 '12 at 19:02
14

In a nutshell, to import a Python compiled file (e.g. module.pyc) only, simply place it in the same directory where the source (e.g module.py) would be, and ensure that there is no corresponding source file (module.py in our example) there. Then the usual import module will work seamlessly.

If there is a source file in the same directory as the compiled file, Python will use the compiled file in the __pycache__ directory instead, or recompile from source if it's not there.

If you remove the source file without putting a ".pyc" in the same directory, the import will fail even if the compiled file exists in the __pycache__ directory. Also note that files under __pycache__ follow a different naming convention. If you copy them across, make sure that they are renamed so that it has the same name as the source file, except that the extension must be "pyc" rather than "py".

There is a very nice flow chart in PEP 3147 linked from the documentation.

Nagev
  • 10,835
  • 4
  • 58
  • 69
3

Use the import without the extension. Python will than look if the file has changed, if not it will use the previously created pyc file.

But note that if you really want more performance, I recommend you to use PyPy which is a lot faster than the standard CPython implementation. (But note that it is still Python 2)

evotopid
  • 5,288
  • 2
  • 26
  • 41
  • 2
    This seems like an advertisement; the OP didn't ask about speed, and is a newbie and therefore unlikely to be focused on speed. – sondra.kinsey Apr 22 '18 at 10:38
  • @sondra.kinsey It's always funny when someone digs out one of my old SA answers and I read it again and I'm like D'oh. You are right this does not exactly answer the original question, but asking for loading compiled code I guess I've just wanted to suggest an alternative based on my suspection of OPs intention. – evotopid Apr 22 '18 at 21:42
2

import module

If there's a .py source file, the core will automatically use the .pyc if it's up to date.

If there's no source, python will automatically import .pyc.

Read more about “compiled” Python files here.

Max S.
  • 3,704
  • 2
  • 13
  • 34
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
1

Yes, but caveat that with make sure to start python with the "-O" option! In my personal experience, if all you have is that .pyc or .pyo file, running "python.exe" without the flag and trying to import will fail, however running python.exe -O and then trying to import as normal should work.

fantadisco
  • 21
  • 4
0

In import use the pyc file name,python will automatically load it.

Kratos85
  • 183
  • 2
  • 5