python初学者 小程序_面向初学者的Python程序的简单部署。

python初学者 小程序

I am a Data scientist aspirant and have conducted several data analysis projects but when it comes to delivering to someone with no working knowledge in Computer science we find ourselves in crossroads. I found a simple way to deliver the hard work we put in by building a single executable file(.exe).

我是一位数据科学家的抱负,并且已经执行了多个数据分析项目,但是当涉及到没有计算机科学工作知识的人时,我们发现自己处于十字路口。 我发现通过构建单个可执行文件(.exe)来交付我们辛勤工作的简单方法。

My goal here is to help you build a simple Single executable file for a python program.

我的目标是帮助您为python程序构建一个简单的Single可执行文件。

I will be using a Python version 3.74 on my Mac Book Pro for doing this example. But The process is similar in windows too so it shouldn't be an issue in this case.

我将在Mac Book Pro上使用Python版本3.74进行此示例。 但是该过程在Windows中也类似,因此在这种情况下应该不成问题。

STEP-I: Installing PyInstaller.

步骤I:安装PyInstaller。

For achieving this you just have to run the following code in Terminal for a Mac or in cmd For Windows users:

为此,您只需要在Mac的Terminal或cmd的Windows用户中运行以下代码:

pip install pyinstall

Some time it will require a pip upgrade which could be achieved by using the following command.

一段时间后,将需要使用以下命令来完成点子升级。

python -m pip install --upgrade pip

STEP-II: Building the executable file.

步骤II:构建可执行文件。

This could be achieved by running the following code.

这可以通过运行以下代码来实现。

pyinstaller -–onefile -w --icon="images\myicon.ico" main.py

Here :

这里 :

  • - -onefile is the command for creating a single executable file.

    --onefile是用于创建单个可执行文件的命令。
  • -w is to avoid the console window from running along with the app.

    -w是为了避免控制台窗口与应用程序一起运行。
  • - -icon is used to apply an icon of your choice to your application.

    --icon用于将您选择的图标应用于应用程序。

There are several other commands available but these are the mainly used basic ones.

还有其他几种命令,但是这些是主要使用的基本命令。

The single executable .exe file will be created in the “dist” folder. Which when run will directly open your application.

单个可执行.exe文件将在“ dist”文件夹中创建。 运行时将直接打开您的应用程序。

Here are Some of the problems that I faced during the execution of the above commands:

这是我在执行上述命令期间遇到的一些问题:

  1. Hidden import warnings, most of them can be ignored as they are just warnings.

    隐藏的导入警告,由于只是警告,因此大多数都可以忽略。
  2. Mac Os:xcrun:error:invalid active developer path, missing xcrun.

    Mac OS:xcrun:错误:无效的活动开发人员路径,缺少xcrun。

To solve this there is an easy fix, we just need to install the Xcode toolkit by using the following command.

为了解决这个问题,有一个简单的解决方法,我们只需要使用以下命令来安装Xcode工具包。

xcode-select --install

we might have to re-register it or update it to the latest version sometimes.

我们有时可能需要重新注册或将其更新为最新版本。

We might have to force reset it if this doesn't work by using the following code.

如果使用以下代码无效,我们可能不得不强制重置它。

sudo xcode-select --reset

3. Unable to find “/user/nltk_data” when adding binary and data files.

3.添加二进制和数据文件时找不到“ / user / nltk_data”。

This can be solved by editing the pyinstaller nltk-hook. For this find the NLTK hook file Python folders then change the contents of the file to the following:

这可以通过编辑pyinstaller nltk-hook来解决。 为此,找到NLTK挂钩文件Python文件夹,然后将文件内容更改为以下内容:

import os
import nltk
from PyInstaller.utils.hooks import collect_data_files
# add datas for nltk
datas = collect_data_files('nltk', False)
# loop through the data directories and add them
for p in nltk.data.path:
if os.path.exists(p):
datas.append((p, "nltk_data"))
# nltk.chunk.named_entity should be included
hiddenimports = ["nltk.chunk.named_entity"]

One more thing, you need to rename the file: pyi_rth__nltk.cpython-36.pyc to pyi_rth_nltk.cpython-36.pyc . There should only be one “_” sometimes the file name will have double “_” underscores.

还有一件事,您需要重命名文件:pyi_rth__nltk.cpython-36.pyc到pyi_rth_nltk.cpython-36.pyc。 只能有一个“ _”,有时文件名会有双“ _”下划线。

4. In windows, you might face a Path issue where your .py file has to be present in the scripts folder. So simply copy-paste your .py file in scripts folder present in the main python folder which can be found by using the following commands:

4.在Windows中,您可能会遇到Path问题,其中.py文件必须出现在scripts文件夹中。 因此,只需将.py文件复制并粘贴到主python文件夹中的scripts文件夹中即可,可使用以下命令找到该文件夹​​:

For mac devices:

对于Mac设备:

which python

For Windows:

对于Windows:

where python

Now This was not the case for everyone so run the code and do this only if you face an issue. Usually, they mention where the file has to be present in the cmd or terminal window so follow the same process but using the path mentioned there.

现在并不是每个人都这样,因此请运行代码并仅在遇到问题时执行此操作。 通常,他们提到文件必须在cmd或终端窗口中存在的位置,因此请遵循相同的过程,但使用此处提到的路径。

5. Some modules may not be installed/present in the python folder a simple fix is to go to the python folder path and pip install the missing module. The following general code can be followed in that case.

5.某些模块可能未安装/存在于python文件夹中,一个简单的解决方法是转到python文件夹路径并pip安装缺少的模块。 在这种情况下,可以遵循以下常规代码。

pip install <Module name>

This should solve most of the issues. I only faced the above issues. In case anyone faces more issues feel free to contact me. Also, I would be thankful if you are willing to share any other problems that you faced while following this article and gladly add that information to this article to further help others.

这应该可以解决大多数问题。 我只面对以上问题。 如果有人面临更多问题,请随时与我联系。 另外,如果您愿意分享在阅读本文时遇到的任何其他问题,并很高兴将这些信息添加到本文中以进一步帮助他人,我将不胜感激。

This is my first article. I am open to suggestions and changes, do let me know if any. This article is for beginners so I have added something that might seem obvious to many, hope it helps someone in need. Thank you for reading my article. happy learning.

这是我的第一篇文章。 我愿意接受建议和变更,请告诉我(如果有)。 本文是针对初学者的,因此我添加了一些对于许多人来说似乎显而易见的东西,希望对需要帮助的人有所帮助。 感谢您阅读我的文章。 学习愉快。

翻译自: https://medium.com/swlh/simple-deployment-of-a-python-program-for-beginners-42733ad08018

python初学者 小程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值