包括多个文件夹以使用PyInstaller创建独立可执行文件。

huangapple go评论64阅读模式
英文:

Including multiple folder to make standalone executable with pyinstaller

问题

I'm trying to make a standalone executable from my python project, but I'm unable to make it work. Below is complete detail....

My project structure look something like this ( in project root dir )

csab:
  2022:
    round1:
      some_files.csv
tool.py

Since I wanted to create standalone executable form it, and ofcrs i need those csv files as its contain data which is required by my program so i used pyinstaller for this purpose.

On windows i tried following command.

pyinstaller --noconfirm --onefile --console --add-data "E:/python/jee_counsellor/csab;csab/" --add-data "E:/python/jee_counsellor/josaa;josaa/"  "E:/python/jee_counsellor/tool.py"

And on linux, i tried with this

python -m PyInstaller --onefile --add-data 'csab/*:csab' --add-data 'josaa/*:josaa' --exclude-module _tkinter tool.py

But none of them seems to work as it cant find my csv paths correctly.
Here is the error details ( same on both linux and windows )

Traceback (most recent call last):
  File "tool.py", line 250, in <module>
  File "tool.py", line 16, in pre_setup
  File "tool.py", line 36, in csab_rounds
  File "tool.py", line 54, in csv_files
  File "pandas\io\parsers\readers.py", line 912, in read_csv
  File "pandas\io\parsers\readers.py", line 577, in _read
  File "pandas\io\parsers\readers.py", line 1407, in __init__
  File "pandas\io\parsers\readers.py", line 1661, in _make_engine
  File "pandas\io\common.py", line 859, in get_handle
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\python\\jee_counsellor\\output\\csab\22\\round_2\\ranks.csv'
[19596] Failed to execute script 'tool' due to unhandled exception!

Snippet from my code where it fails.

        cwd = os.getcwd()
        csab_rounds = round
        csv_path = os.path.join(cwd, "csab", "2022", f"round_{csab_rounds}", "ranks.csv")
        df = pd.read_csv(csv_path)

Do I need to update my code or any other workaround for this with pyinstaller ?

英文:

I'm trying to make a standalone executable from my python project, but I'm unable to make it work. Below is complete detail....

My project structure look something like this ( in project root dir )

csab:
  2022:
    round1:
      some_files.csv
tool.py

Since I wanted to create standalone executable form it, and ofcrs i need those csv files as its contain data which is required by my program so i used pyinstaller for this purpose.

On windows i tried following command.

pyinstaller --noconfirm --onefile --console --add-data &quot;E:/python/jee_counsellor/csab;csab/&quot; --add-data &quot;E:/python/jee_counsellor/josaa;josaa/&quot;  &quot;E:/python/jee_counsellor/tool.py&quot;

And on linux, i tried with this

python -m PyInstaller --onefile --add-data &#39;csab/*:csab&#39; --add-data &#39;josaa/*:josaa&#39; --exclude-module _tkinter tool.py

But none of them seems to work as it cant find my csv paths correctly.
Here is the error details ( same on both linux and windows )

Traceback (most recent call last):
  File &quot;tool.py&quot;, line 250, in &lt;module&gt;
  File &quot;tool.py&quot;, line 16, in pre_setup
  File &quot;tool.py&quot;, line 36, in csab_rounds
  File &quot;tool.py&quot;, line 54, in csv_files
  File &quot;pandas\io\parsers\readers.py&quot;, line 912, in read_csv
  File &quot;pandas\io\parsers\readers.py&quot;, line 577, in _read
  File &quot;pandas\io\parsers\readers.py&quot;, line 1407, in __init__
  File &quot;pandas\io\parsers\readers.py&quot;, line 1661, in _make_engine
  File &quot;pandas\io\common.py&quot;, line 859, in get_handle
FileNotFoundError: [Errno 2] No such file or directory: &#39;E:\\python\\jee_counsellor\\output\\csab\22\\round_2\\ranks.csv&#39;
[19596] Failed to execute script &#39;tool&#39; due to unhandled exception!

Snippet from my code where it fails.

        cwd = os.getcwd()
        csab_rounds = round
        csv_path = os.path.join(cwd, &quot;csab&quot;, &quot;2022&quot;, f&quot;round_{csab_rounds}&quot;, &quot;ranks.csv&quot;)
        df = pd.read_csv(csv_path)

Do I need to update my code or any other workaround for this with pyinstaller ?

答案1

得分: 1

Your build command has a slight mistake. On Linux, it should be

python -m PyInstaller --onefile --add-data 'csab:csab' --add-data 'josaa:josaa' --exclude-module _tkinter tool.py.

Notice the --add-data 'josaa:josaa'.

From the documentation,

  1. The first string (josaa here) specifies the file or files as they are in this system now.
  2. The second string (again josaa here) specifies the name of the folder to contain the files at run-time.

You do not need to write /*, the entire content of the folder will be automatically included.

Link to documentation: https://pyinstaller.org/en/stable/usage.html#cmdoption-add-data

Recommended way would be to use a spec file.

https://pyinstaller.org/en/stable/spec-files.html#adding-data-files

英文:

Your build command has a slight mistake.
On Linux, it should be

python -m PyInstaller --onefile --add-data &#39;csab:csab&#39; --add-data &#39;josaa:josaa&#39; --exclude-module _tkinter tool.py.

Notice the --add-data &#39;josaa:josaa&#39;.

From the documentation,

  1. The first string (josaa here) specifies the file or files as they are in this system now.
  2. The second string (again josaa here) specifies the name of the folder to contain the files at run-time.

You do not need to write /*, the entire content of the folder will be automatically included.

Link to documentation: https://pyinstaller.org/en/stable/usage.html#cmdoption-add-data

Recommended way would be to use a spec file.

https://pyinstaller.org/en/stable/spec-files.html#adding-data-files

huangapple
  • 本文由 发表于 2023年5月7日 12:37:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定