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

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

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 )

  1. csab:
  2. 2022:
  3. round1:
  4. some_files.csv
  5. 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.

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

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

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

Snippet from my code where it fails.

  1. cwd = os.getcwd()
  2. csab_rounds = round
  3. csv_path = os.path.join(cwd, "csab", "2022", f"round_{csab_rounds}", "ranks.csv")
  4. 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 )

  1. csab:
  2. 2022:
  3. round1:
  4. some_files.csv
  5. 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.

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

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

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

Snippet from my code where it fails.

  1. cwd = os.getcwd()
  2. csab_rounds = round
  3. csv_path = os.path.join(cwd, &quot;csab&quot;, &quot;2022&quot;, f&quot;round_{csab_rounds}&quot;, &quot;ranks.csv&quot;)
  4. 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:

确定