2

I am new to python compiling and I try to make it work for compiled scripts using python 3.9 when having miniconda installed.

Here is my main.py in the root folder of my project.

from say_hello import just_say_hello

print('Hi')
just_say_hello()

Here is say_hello.py


def just_say_hello():
    print('Hello')

The files are in the same folder. In that folder, I run the following command-line statement to compile:

 python -m compileall . 

Afterwards, I run the following commandline-statement to run:

cd __pycache__
python .\main.cpython-39.pyc

This results in the following error:

ModuleNotFoundError: No module named 'say_hello'

Why this module cannot be found when running the script from the __pycache__ folder? Both script files (compiled) are in there. It should not be such a big problem. When running the normal original script, everything works fine. When using pycompile, suddenly, things break. However, I really need to compile.

How can I just compile this python script in such a way I can run it without such issues?

I tried several things. For example, I renamed the the pyc files to normal python files. After running

python main.py

I received the following error:

ValueError: source code string cannot contain null bytes

So I really need a solution to run my compiled multi file script.

Daan
  • 2,478
  • 3
  • 36
  • 76

3 Answers3

1

Python is an interpreted language, what this means for you is that you don't have to compile the scripts to run them (Even though they are technically still compiled). This means that when you run a python file it is compiled to byte code (Machine code) and then runs. However, if other python files are referenced in the file you are running, then they are compiled, so that they do not have to be compiled when they are called, this is what .pyc files are. I would not recommend deleting them. I hope this helps.

John Hill
  • 51
  • 5
  • 1
    I do not need to compile to run. But that does not mean there is no (other) reason at all to compile my scripts, for example obfuscation. Please read my post carefully, I already mentioned that it is not needed for just running: "When running the normal original script, everything works fine." – Daan May 18 '22 at 06:47
1

Module import is one of many things that are wrong with Python, and just like significant whitespace, 'Python' seems proud of it.

Having said that, I usually solve my problems with import by adding everything to the PYTHONPATH. Either in the environment variable (depends on OS) or in code itself with sys.path.append('..')

Expect new problems when you reboot or move your application to a different machine or even a different directory. Or when you want to make your application cross-platform.

Test carefully for these scenarios.

PS. There is also a somewhat older answer that explains different ways of importing here: How can I import other Python files?

Pieter21
  • 1,765
  • 1
  • 10
  • 22
0

When you compile it down to machine code, it hardcodes the directory paths to the source files.

All you need to do to make the compiled files work is move them into the same directory where the uncompiled versions are.

Alexander
  • 16,091
  • 5
  • 13
  • 29