Python Forum
What do you think of this procedure to create a path to a folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What do you think of this procedure to create a path to a folder
#4
For your benefit I copy the relevant section here (I make the bold parts for emphasis)

--- EXTRACT FROM PYINSTALER DOCS---

Using __file__

When your program is not bundled, the Python variable __file__ refers to the current path of the module it is contained in. When importing a module from a bundled script, the PyInstaller bootloader will set the module’s __file__ attribute to the correct path relative to the bundle folder.

For example, if you import mypackage.mymodule from a bundled script, then the __file__ attribute of that module will be sys._MEIPASS + 'mypackage/mymodule.pyc'. So if you have a data file at mypackage/file.dat that you added to the bundle at mypackage/file.dat, the following code will get its path (in both the non-bundled and the bundled case):

from os import path
path_to_dat = path.join(path.dirname(__file__), 'file.dat')
In the bundled main script itself the above might not work, as it is unclear where it resides in the package hierarchy. So in when trying to find data files relative to the main script, sys._MEIPASS can be used. The following will get the path to a file other-file.dat next to the main script if not bundled and in the bundle folder if it is bundled:

from os import path
import sys
bundle_dir = getattr(sys, '_MEIPASS', path.abspath(path.dirname(__file__)))
path_to_dat = path.join(bundle_dir, 'other-file.dat')
---- END OF EXTRACT ---

The last snippet show what/how you need to change your code (not the spc file).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: What do you think of this procedure to create a path to a folder - by buran - Sep-02-2019, 06:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to capture the result set from a stored procedure? dee 5 315 May-25-2024, 01:03 AM
Last Post: deanhystad
  Create dual folder on different path/drive based on the date agmoraojr 2 562 Jan-21-2024, 10:02 AM
Last Post: snippsat
  python script is hanging while calling a procedure in database prasanthi417 4 675 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 681 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  create a default path with idle to a specific directory greybill 0 922 Apr-23-2023, 04:32 AM
Last Post: greybill
  python call stored procedure mg24 2 1,165 Oct-18-2022, 02:19 AM
Last Post: mg24
  python call stored procedure with two parameter mg24 4 1,730 Sep-27-2022, 05:02 AM
Last Post: deanhystad
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,668 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,280 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,564 Mar-22-2021, 10:59 AM
Last Post: shantanu97

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020