Python 脚本在 IDE 中有效,但在内置到可执行文件中时却无效?
Python script works in the IDE but not when built into executable?
当我 运行 我在 Pycharm 中的代码时,它会解析我的 Excel 文件并正确合并 IDE 中的所有内容。但是我使用 cx_Freeze 将它构建到可执行文件中,它停止工作。当我双击可执行文件时,它什么也没有。黑色控制台 window 快速闪烁。是什么赋予了。 Pycharm 中没有堆栈跟踪或错误。
这是我的参考代码:
[In:]
import numpy as np
import pandas as pd
#finds the path to a users pc.
userHome = os.path.expanduser('~')
directory = userHome + '/Desktop/Parsing Script/ParsingScriptOutputFile/'
workerList= (directory+'employeeList.csv')
managerList = (userHome+'/Parsing Script/ParsingScriptOutputFile/managerList.XLSX')
# specify 32 columns, no headers
employee = pd.read_csv(workerList, usecols=range(32), header=None, sep=",",engine='python') # read in the data
original_rows = employee.shape[0] # original number of rows
managers = pd.read_excel(managerList)
managers= managers[['Cost Center','Profit Center','User ID', 'Description','Email address','Person Responsible']]
# Left join so that the rows that do not match are not dropped from employee
# data
merged_df = pd.merge(employee, managers, on='key', how='left')
# Number of rows should be unchanged
# This should print out True
print(merged_df.shape[0] == original_rows)
#File called merged_data.csv returned to ParsingScriptOutputFile
return merged_df.to_csv(directory+'merged_data.csv', index=False, encoding='utf-8') # Save
@sideffect0(感谢指教)
完整追溯
Traceback (most recent call last):
File "C:\Python\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 12, in <module>
__import__(name + "__init__")
File "C:\Python\lib\site-packages\cx_Freeze\initscripts\Console.py", line 24, in <module>
exec(code, m.__dict__)
File "csvProject1.py", line 3, in <module>
File "C:\Python\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Python\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Python\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Python\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Python\lib\site-packages\numpy\core\__init__.py", line 36, in <module>
from . import numeric
File "C:\Python\lib\site-packages\numpy\core\numeric.py", line 1842, in <module>
from .arrayprint import array2string, get_printoptions, set_printoptions
File "C:\Python\lib\site-packages\numpy\core\arrayprint.py", line 24, in <module>
from .fromnumeric import ravel
File "C:\Python\lib\site-packages\numpy\core\fromnumeric.py", line 15, in <module>
from . import _methods
ImportError: cannot import name '_methods'
问题是一个模块没有正确安装在 setup.py 文件中,我使用了 this code 来自@downshift 链接我的这个 Whosebug 答案。我还发现我的 numpy 安装已损坏。所以我快速卸载了 re-install。将脚本重建为可执行文件,并且运行良好。
为了获得完整的回溯,我导航到命令行中的可执行文件并输入文件名。一种更快的方法是按住 alt
并右键单击包含您的 exe 的文件,然后单击打开 open command window
这将为您在文件位置打开 cmd。 运行 你的程序在那里得到回溯
当我 运行 我在 Pycharm 中的代码时,它会解析我的 Excel 文件并正确合并 IDE 中的所有内容。但是我使用 cx_Freeze 将它构建到可执行文件中,它停止工作。当我双击可执行文件时,它什么也没有。黑色控制台 window 快速闪烁。是什么赋予了。 Pycharm 中没有堆栈跟踪或错误。
这是我的参考代码:
[In:]
import numpy as np
import pandas as pd
#finds the path to a users pc.
userHome = os.path.expanduser('~')
directory = userHome + '/Desktop/Parsing Script/ParsingScriptOutputFile/'
workerList= (directory+'employeeList.csv')
managerList = (userHome+'/Parsing Script/ParsingScriptOutputFile/managerList.XLSX')
# specify 32 columns, no headers
employee = pd.read_csv(workerList, usecols=range(32), header=None, sep=",",engine='python') # read in the data
original_rows = employee.shape[0] # original number of rows
managers = pd.read_excel(managerList)
managers= managers[['Cost Center','Profit Center','User ID', 'Description','Email address','Person Responsible']]
# Left join so that the rows that do not match are not dropped from employee
# data
merged_df = pd.merge(employee, managers, on='key', how='left')
# Number of rows should be unchanged
# This should print out True
print(merged_df.shape[0] == original_rows)
#File called merged_data.csv returned to ParsingScriptOutputFile
return merged_df.to_csv(directory+'merged_data.csv', index=False, encoding='utf-8') # Save
@sideffect0(感谢指教) 完整追溯
Traceback (most recent call last):
File "C:\Python\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 12, in <module>
__import__(name + "__init__")
File "C:\Python\lib\site-packages\cx_Freeze\initscripts\Console.py", line 24, in <module>
exec(code, m.__dict__)
File "csvProject1.py", line 3, in <module>
File "C:\Python\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Python\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Python\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Python\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Python\lib\site-packages\numpy\core\__init__.py", line 36, in <module>
from . import numeric
File "C:\Python\lib\site-packages\numpy\core\numeric.py", line 1842, in <module>
from .arrayprint import array2string, get_printoptions, set_printoptions
File "C:\Python\lib\site-packages\numpy\core\arrayprint.py", line 24, in <module>
from .fromnumeric import ravel
File "C:\Python\lib\site-packages\numpy\core\fromnumeric.py", line 15, in <module>
from . import _methods
ImportError: cannot import name '_methods'
问题是一个模块没有正确安装在 setup.py 文件中,我使用了 this code 来自@downshift 链接我的这个 Whosebug 答案。我还发现我的 numpy 安装已损坏。所以我快速卸载了 re-install。将脚本重建为可执行文件,并且运行良好。
为了获得完整的回溯,我导航到命令行中的可执行文件并输入文件名。一种更快的方法是按住 alt
并右键单击包含您的 exe 的文件,然后单击打开 open command window
这将为您在文件位置打开 cmd。 运行 你的程序在那里得到回溯