How can we get the compiled python pyc file path with python 3.x in the current environment? I know it's in the __pycache__
direcotry, but I couldn't find a way to find the file path. Because the names of pyc files of python 3 changes by the environment.
Given that you know the path to the source (ie .py
) file, there's a function importlib.util.cache_from_source
that does exactly what you want. For example, to get the .pyc
file corresponding to the numpy
package, you would do:
import importlib
import numpy
importlib.util.cache_from_source(numpy.__file__)
For me (on OS X), this prints out something along the lines of:
/<absolute path to site-packages>/numpy/__pycache__/__init__.cpython-36.pyc