Importing Python modules from a select location

2020-06-16 02:57发布

问题:

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)

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

Two issues are present:

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

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.

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

Logic: Compile Main.py using pyinstaller -F –onefile Main.py (15mb).

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

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

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.

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

回答1:

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

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



回答2:

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.

To address my first concern, UPX and virtual directory. 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.

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. They may fix the onefile in the future, but I am more than satisfied with this solution.

As a result: Smaller File.

Can I run these exe's where ever I want on that pc? 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.