简体   繁体   English

从选定位置导入Python模块

[英]Importing Python modules from a select location

Let's say I had three scripts. 假设我有三个脚本。 Main.py (has all imports), 1.py (random script), 2.py (random script). Main.py(具有所有导入),1.py(随机脚本),2.py(随机脚本)。

pyinstaller -F --onedir Main.py (80mb)
pyinstaller -F --onedir 1.py (80mb)
pyinstaller -F --onedir 2.py (80mb)

This creates 3 folders I then copy 1.exe and 2.exe to Main folder with all dependencies and this runs fine. 这将创建3个文件夹,然后将1.exe和2.exe复制到具有所有依赖项的Main文件夹中,并且运行良好。

图片

Two issues are present: 存在两个问题:

The issue is the size. 问题是大小。 One file reduces this to 30mb, one folder keeps it at 80mb 一个文件将其减小到30mb,一个文件夹将其保持在80mb

More importantly, the exe's are unable to leave that folder. 更重要的是,exe无法离开该文件夹。 I've had to resort to using shortcuts in Python. 我不得不求助于在Python中使用快捷方式。

I am following this , which supposedly is a workaround. 我正在遵循此方法 ,这应该是一种解决方法。

My question is, how can I read the imports from a select location with pyinstaller so I can move the executables? 我的问题是,如何使用pyinstaller从选定位置读取导入,以便可以移动可执行文件?

Logic: Compile Main.py using pyinstaller -F –onefile Main.py (15mb). 逻辑:使用pyinstaller -F –onefile Main.py (15mb).编译Main.py。

Create folder at C:\\13\\ so exe looks like C:\\13\\Main.exe C:\\13\\创建文件夹,使exe看起来像C:\\13\\Main.exe

link all exe's at C:\\13 ? C:\\13链接所有exe? with --runtime-tmpdir PATH 使用--runtime-tmpdir PATH

I am struggling to find documentation on getting imports from inside the exe or even "one dir" folder. 我正在努力寻找有关从exe甚至"one dir"文件夹中获取导入的文档。 There is plenty on getting imports from the get go, but not much on getting imports from a select location so I can run the exe anywhere. 从开始获取导入有很多,但是从选定位置获取导入没有太多,因此我可以在任何地方运行exe。 What's more, it is hard to even find the modules in pyinstaller short of decompiling it adding to this confusion. 而且,很难在pyinstaller中找到模块,除非对其进行反编译,从而增加了这种混乱。

Perhaps... 也许...

pyinstaller -F --runtime-tmpdir C:\13 --onefile 2.py

pyinstaller -F --runtime-tmpdir C:\13 --onedir 3.py

Though for me no directory is created. 虽然对我来说没有创建目录。 How do I go about getting imports from a select location. 我要如何从选定的位置获取进口。

Another possible solution : 另一个可能的解决方案

  if getattr(sys, 'frozen', False): 
      os.path.join(sys._MEIPASS, "C:\aa")

Though as you can see in the image , it's lets say we wanted pandas there are a few files to choose from... 虽然您可以在图片中看到,但可以说我们希望熊猫有一些文件可供选择...

I had the same issues with PyInstaller when they switched to version 3.0 and Nuitka solved it for me. 当他们切换到3.0版时,我与PyInstaller遇到了同样的问题,而Nuitka为我解决了这个问题。

You could bundle your Main.py into a .pyd lib file and then create executables for 1.py and 2.py which would import all needed packages from this "dll". 您可以将Main.py捆绑到.pyd lib文件中,然后为1.py2.py创建可执行文件, 1.py可执行文件将从该“ dll”中导入所有需要的软件包。 Hence you could put your lib file into a specific location and set the PYTHONPATH accordingly right before launching your executables (eg in a batch file or directly in your shell configuration). 因此,您可以在启动可执行文件之前(例如,在批处理文件中或直接在您的shell配置中)将lib文件放入特定位置并相应地设置PYTHONPATH

I have investigated this further, and believe the solutions presented here to be the answer I am after 我对此进行了进一步调查,并相信这里介绍的解决方案将是我所追求的答案

Using onefile is not possible as this feature is presently broken. 目前无法使用onefile,因为此功能已损坏。

To address my first concern, UPX and virtual directory. 为了解决我的第一个问题,UPX和虚拟目录。 Then lastly, putting all the files into one dir. 最后,将所有文件放在一个目录中。 Lastly, putting the exe's in the folder like I have done in the picture will mean I have a reduced file. 最后,像在图片中一样将exe放入文件夹中将意味着我的文件减少了。

I could create shortcuts through a python script (if this is still a concern), however there is no real need and running all the exe's in that one folder is more than fine by me I have found. 我可以通过python脚本创建快捷方式(如果仍然需要考虑的话),但是没有真正的需要,运行一个文件夹中的所有exe文件对我来说已经足够了。 They may fix the onefile in the future, but I am more than satisfied with this solution. 他们可能会在将来修复onefile,但我对这种解决方案感到非常满意。

As a result: Smaller File. 结果:文件较小。

Can I run these exe's where ever I want on that pc? 我可以在该PC上的任何位置运行这些exe吗? Sadly, no not without multipackage-bundles which is broken. 可悲的是,并非没有多包装捆扎的包装被打破。 You could create a shortcuts though which is not as bad as I thought it to be. 您可以创建一个快捷方式,尽管它没有我想的那么糟糕。 Though they may fix that in the future but in the mean time this solution is as good as it will get. 尽管他们可能会在将来解决此问题,但与此同时,此解决方案将达到最佳效果。 Feel free to correct me if I am wrong, but I am happy with this solution. 如果我错了,请随时纠正我,但是我对这种解决方案感到满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM