从选定位置导入Python模块 [英] Importing Python modules from a select location

查看:99
本文介绍了从选定位置导入Python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有三个脚本. Main.py(具有所有导入),1.py(随机脚本),2.py(随机脚本).

Let’s say I had three scripts. Main.py (has all imports), 1.py (random script), 2.py (random script).

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

这将创建3个文件夹,然后将1.exe和2.exe复制到具有所有依赖项的Main文件夹中,并且运行正常.

This creates 3 folders I then copy 1.exe and 2.exe to Main folder with all dependencies and this runs fine.

存在两个问题:

问题是大小.一个文件将其减小到30mb,一个文件夹将其保持在80mb

The issue is the size. One file reduces this to 30mb, one folder keeps it at 80mb

更重要的是,exe无法离开该文件夹.我不得不求助于在Python中使用快捷键.

More importantly, the exe’s are unable to leave that folder. I’ve had to resort to using shortcuts in Python.

我正在关注,这可能是一种解决方法.

I am following this, which supposedly is a workaround.

我的问题是,如何使用pyinstaller从选定位置读取导入,以便可以移动可执行文件?

My question is, how can I read the imports from a select location with pyinstaller so I can move the executables?

逻辑: 使用pyinstaller -F –onefile Main.py (15mb).

C:\13\创建文件夹,使exe看起来像C:\13\Main.exe

Create folder at C:\13\ so exe looks like C:\13\Main.exe

C:\13链接所有exe吗?与--runtime-tmpdir PATH

link all exe's at C:\13? with --runtime-tmpdir PATH

我正在努力寻找有关从exe甚至"one dir"文件夹中获取导入的文档.从开始获取导入有很多,但是从选定位置获取导入没有很多,因此我可以在任何地方运行exe.而且,很难在pyinstaller中找到这些模块,除非对其进行反编译,这会增加这种混乱.

I am struggling to find documentation on getting imports from inside the exe or even "one dir" folder. 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. What's more, it is hard to even find the modules in pyinstaller short of decompiling it adding to this confusion.

也许...

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.

另一种可能的解决方案:

  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...

推荐答案

当他们切换到3.0版和 Nuitka 为我解决了这个问题.

I had the same issues with PyInstaller when they switched to version 3.0 and Nuitka solved it for me.

您可以将Main.py捆绑到.pyd lib文件中,然后为1.py2.py创建可执行文件,这些可执行文件将从该"dll"中导入所有需要的软件包.因此,您可以在启动可执行文件之前(例如,在批处理文件中或直接在您的shell配置中)将lib文件放入特定位置并相应地设置PYTHONPATH.

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". Hence you could put your lib file into a specific location and set the PYTHONPATH accordingly right before launching your executables (e.g in a batch file or directly in your shell configuration).

这篇关于从选定位置导入Python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆