admin管理员组

文章数量:1313276

I am trying to create an executable from a Python file using auto-py-to-exe. My code is set up as two .py files. I have a top level code which I will call main.py, and a secondary code with my functions and classes which I will call functions.py.

Whenever I convert the main.py to an exe, the functions.py is included in the exe. I would like to only convert the main.py to an exe so I can still open functions.py and rewrite or add to it without regenerating the exe.

I think the best solution would be if there were a setting to change in auto-py-to-exe. But if not, is there some other way to prevent it? My first thought was to import from a specific filepath.

i.e. go from this:

# import module
from functions import *

# execute code from functions
Func_From_Functions()
object = Class_From_Functions()
# etc...

to something like this?

# import module
from *filepath* import *

# execute code from functions
Func_From_Functions()
object = Class_From_Functions()
# etc...

I am trying to create an executable from a Python file using auto-py-to-exe. My code is set up as two .py files. I have a top level code which I will call main.py, and a secondary code with my functions and classes which I will call functions.py.

Whenever I convert the main.py to an exe, the functions.py is included in the exe. I would like to only convert the main.py to an exe so I can still open functions.py and rewrite or add to it without regenerating the exe.

I think the best solution would be if there were a setting to change in auto-py-to-exe. But if not, is there some other way to prevent it? My first thought was to import from a specific filepath.

i.e. go from this:

# import module
from functions import *

# execute code from functions
Func_From_Functions()
object = Class_From_Functions()
# etc...

to something like this?

# import module
from *filepath* import *

# execute code from functions
Func_From_Functions()
object = Class_From_Functions()
# etc...
Share Improve this question asked Jan 31 at 5:12 Kenton SuppleeKenton Supplee 211 silver badge1 bronze badge 1
  • 2 How can that possibly work? If you change "functions.py", then the executable is no longer valid. You can use the importlib module to import a module by hand, but I question whether that's really a good user experience. – Tim Roberts Commented Jan 31 at 5:20
Add a comment  | 

2 Answers 2

Reset to default 0

As a one option you can make a system where your exe and required files work when in same directory.

First you need to remove your current imports.

Then your main function must know where it works then it can add your imports. Lets say you have external py file named functions as you give in your example.

Code below finds the your main.py directory, and using importlib it auto imports files at run time so if you update the files they will called in updated form. To use imports in such manner you need to use "functions.myFunc()". To make it work keed your external py files and your main in same folder. You might need to change pytoexe behaviour as one directory and prevent inclusion of your py files.

import importlib.util
import sys
import os

module_name = "functions"
module_path = os.path.join(os.path.dirname(__file__), "functions.py")

spec = importlib.util.spec_from_file_location(module_name, module_path)
functions = importlib.util.module_from_spec(spec)
sys.modules[module_name] = functions
spec.loader.exec_module(functions)

Why would you need such a thing?

One reason executable files exist is to create an abstraction from development files, in your case, those two Python files. This is so that users only deal with one file without seeing the source code. When you have an executable application, everything that application needs is usually being packed into one single file.

Creating the executable while looking for the freedom of editing source code does not sound viable.

本文标签: How to prevent imported Python module from being included in exeStack Overflow