I want to run a Pythonic project using Python compilation (.pyc or __pycache__). In order to do that in Python2, I haven't any problem.
Here is a simplified example in a Python2 project:
Project tree:
test2 ├── main.py └── subfolder ├── __init__.py └── sub.pyCompile:
python -m compileall test2Project tree after the compile:
test2 ├── main.py ├── main.pyc └── subfolder ├── __init__.py ├── __init__.pyc ├── sub.py └── sub.pycAs you can see, several
.pycmanually generated. Now I can run this project usingmain.pycas fine, which has a relation with thesub.py:python main.pycOut:
Hi Byemain.py content:
from subfolder import sub print('Bye')sub.py content:
print('Hi')
Now I want to retry this behavior in a Python3 project.
Here is a simplified asyncio (available in Python3) project:
Project tree:
test3 ├── main.py └── subfolder ├── __init__.py └── sub.pyCompile:
python3 -m compileall test3Project tree after the compile:
test3 ├── main.py ├── __pycache__ │ └── main.cpython-36.pyc └── subfolder ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-36.pyc │ └── sub.cpython-36.pyc └── sub.pyAs you can see,
__pycache__folders manually generated. But I cannot run this project usingmain.cpython-36.pycwhich has a relation withsubfolder:cd test3/__pycache__ python3 main.cpython-36.pycOut (I expected that produced the
Hi Byemessage):Traceback (most recent call last): File "test3/main.py", line 2, in <module> ModuleNotFoundError: No module named 'subfolder'main.py content:
import asyncio from subfolder import sub async def myCoroutine(): print("Bye") def main(): loop = asyncio.get_event_loop() loop.run_until_complete(myCoroutine()) loop.close() main()sub.py content:
print('Hi')
Question:
How can I run this project (above Python3 project) using __pycache__ folder?
Or
How can I run a Python3 project with the relation between subfolders using python compilation?
[NOTE]:
I cannot use the
python compileall(Python2 compile) in the abovePython3project due to theasynciomethod.My Python(s) version is Python2.7 and Python3.6