Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--add-data pack .txt file faile on Win10 #3749

Closed
625781186 opened this issue Sep 19, 2018 · 7 comments

Comments

@625781186
Copy link

commented Sep 19, 2018

I have a python file name "pyi.py" and a txt file name "read.txt" in 'F://' ;

#pyi.py
# -*- coding: utf-8 -*-

if __name__ == "__main__":
    import sys, os
    with open("./read.txt", "r+", encoding="utf-8") as f:
        text = f.read()
        print(text)

I use command

pyinstaller -F --add-data "read.txt;." pyi.py

or

pyinstaller -F --add-data "read.txt;dist" pyi.py

Packing successfull , but when I run pyi.exe , cmd show one error is:

FileNotFoundError: [Errno 2] No such file or directory: './read.txt'

I don't know how to solve it.
Thank you.

@htgoebel

This comment has been minimized.

Copy link
Member

commented Sep 19, 2018

Please read the manual on how to access files at run-time.

@625781186

This comment has been minimized.

Copy link
Author

commented Sep 19, 2018

Please read the manual on how to access files at run-time.

Sorry , I read the Doc of pyinstaller , it told me this ok .
I use command No -F

pyinstaller  --add-data "read.txt;." pyi.py

OK , I run pyi.exe is OK ;

but I use command

pyinstaller  -F --add-data "read.txt;." pyi.py

It's error.
The Doc of pyinstaller told me it's OK.
But I don't know why , if you know , please told me , thanks.
or give me a right command !

@tallforasmurf

This comment has been minimized.

Copy link
Contributor

commented Sep 19, 2018

I think @htgoebel is referring to this manual topic. Key for you is what you get from os.getcwd() which is where the program will look for ./filename. Maybe just put print( 'os.getcwd is', os.getcwd() ) in pyi.pi to see.

@625781186

This comment has been minimized.

Copy link
Author

commented Sep 19, 2018

I think @htgoebel is referring to this manual topic. Key for you is what you get from os.getcwd() which is where the program will look for ./filename. Maybe just put print( 'os.getcwd is', os.getcwd() ) in pyi.pi to see.

Thank you very much ! I'm success !'

@tallforasmurf

This comment has been minimized.

Copy link
Contributor

commented Sep 19, 2018

So what was the answer? What did you have to change in your script?

@625781186

This comment has been minimized.

Copy link
Author

commented Sep 20, 2018

So what was the answer? What did you have to change in your script?

Like this :

#pyi.py
# -*- coding: utf-8 -*-
# author : @mt

import os

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

if __name__ == "__main__":
    import sys, os
    with open(resource_path("read.txt"), "r+", encoding="utf-8") as f:
        text = f.read()
        print(text)

or

#pyi.py
# -*- coding: utf-8 -*-
# author : @上海-开发-韩

import os
import sys

base_path = os.path.realpath(sys.path[0])
base_path = os.path.join(base_path, '.')

if __name__ == '__main__':
    with open(os.path.join(base_path,  'read.txt'), "r+") as f:
        text = f.read()
        print(text)
@625781186

This comment has been minimized.

Copy link
Author

commented Apr 9, 2019

简单说一下 , 使用单文件 -F 打包 ,运行的时候会把app解压到临时目录 , 官方解释
但是运行程序的时候工作目录没有切换 到解压目录 , 所以造成相对路径的资源文件没找到 ,
以上是两种办法 ,
还有一种更简便的办法 , 只要在运行的加一句代码os.chdir(os.getcwd()) 切换工作目录即可

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.