admin管理员组文章数量:1313086
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 Answers
Reset to default 0As 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
版权声明:本文标题:How to prevent imported Python module from being included in .exe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741925315a2405248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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