打包exe后闪退问题?小白求助
D:\new>pyinstaller -F fuli.py58 INFO: PyInstaller: 4.5
59 INFO: Python: 3.6.5
59 INFO: Platform: Windows-10-10.0.18362-SP0
60 INFO: wrote D:\new\fuli.spec
63 INFO: UPX is not available.
64 INFO: Extending PYTHONPATH with paths
D:\new\dist>fuli.exe
Traceback (most recent call last):
File "fuli.py", line 3, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "parsel\__init__.py", line 10, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "parsel\selector.py", line 8, in <module>
File "src\lxml\etree.pyx", line 74, in init lxml.etree
ImportError: cannot import name _elementpath
Failed to execute script 'fuli' due to unhandled exception!
求解惑!!!!!!!!! from fake_useragent import UserAgent
import requests
import parsel
import os
import random
import re
闪退的原因:
1. 文件调用问题,指你调用了某些媒体文件,比如图片或者音乐,你只需要把打包好的文件移动到程序的主模块相同的路径下即可使用;
2,子模块路径问题,指自己写的子模块放在了不同的路径下导致没有被打包,解决方法一:将所有模块放到相同的路径下,解决方法2,pyinstaller 打包语句中添加 -p “子模块路径”
3,程序内调用程序运行目录,指你使用了 os.getcwd() 或 os.path.abspath() 函数调用了软件的运行目录,因为 软件打包前和打包后的运行目录是不同的,所以出错,这里推荐使用下面的代码获得运行目录,
if getattr(sys, 'frozen', False):
BASE_DIR = os.path.dirname(sys.executable)
file_path = f'{os.path.join(BASE_DIR,sys.executable)}'
elif __file__:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
print(BASE_DIR) {:5_108:} nahongyan1997 发表于 2021-8-5 09:06
闪退的原因:
1. 文件调用问题,指你调用了某些媒体文件,比如图片或者音乐,你只需要把打包好的文件移动 ...
谢谢你的热心帮忙!!!
cannot import name _elementpath
产生这个问题的原因是什么,
我是小白,就是一个简单的爬虫代码打包,没有调用什么..... nahongyan1997 发表于 2021-8-5 09:06
闪退的原因:
1. 文件调用问题,指你调用了某些媒体文件,比如图片或者音乐,你只需要把打包好的文件移动 ...
你写的代码前面的01 02是怎么出来的
pyinstaller4.5,python3.6.5,会冲突吗? 1,代码前面的行号是网站自动加的;
2,把代码给我我看看我这边能不鞥正常打包。 nahongyan1997 发表于 2021-8-5 12:03
1,代码前面的行号是网站自动加的;
2,把代码给我我看看我这边能不鞥正常打包。
import requests
import os
import re
from fake_useragent import UserAgent
# headers = {'User-Agent': UserAgent().random}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0',
'Referer':'http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6',
'csrf': 'HYAUG9BYRAI',
'Cookie': '__guid=112476674.3310808051482316000.1628257329943.9053; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1628257330; _ga=GA1.2.1222733808.1628257331; _gid=GA1.2.156578177.1628257331; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1628258543; _gat=1; kw_token=HYAUG9BYRAI; monitor_count=8'
}
# print(headers)
print('==========酷我音乐下载============\n===========按歌手下载=============')
name = input('请输入歌手名字:')
page1 = int(input('请输入开始页数:'))
page2 = int(input('请输入结束页数:'))
def change_name(name):
pattern = re.compile('[\\\/\:\"\?\|\<\>\:]') # \\\/\:\"\?\|\<\>\:
new_name = re.sub(pattern, '-', name)
return new_name
for page in range(page1, page2):
print(f'正在下载第{page}页歌曲')
# url = http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=%E5%91%A8%E6%9D%B0%E4%BC%A6&pn=1&rn=30&httpsStatus=1&reqId=639a10c0-f6ca-11eb-842a-290d889eed46 歌名搜索
# url = http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=%E5%91%A8%E6%9D%B0%E4%BC%A6&pn=1&rn=30&httpsStatus=1&reqId=0ece6030-f6bd-11eb-b3e0-6fbaea9acb93 歌手搜索
ss_url = 'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key='+name+f'&pn={page}&rn=30&httpsStatus=1&reqId=0ece6030-f6bd-11eb-b3e0-6fbaea9acb93'
# print(ss_url)
response = requests.get(ss_url, headers=headers)
try:
json_data = response.json()
music_list = json_data['data']['list']
except:
pass
if not os.path.exists('D:\\酷我音乐\\'):
os.mkdir('D:\\酷我音乐\\')
path = 'D:\\酷我音乐\\'
for music in music_list:
music_name = music['name']
new_name = change_name(music_name)
music_id = music['rid']
# print('正在下载:', music_name)
# 解析url:传入音乐ID 解析出这首音乐真实的播放地址(歌曲ID)
jx_url = 'http://www.kuwo.cn/url?format=mp3?&rid=' + str(music_id) + '&response=url&type=convert_url3&br=128kmp3&from=web&t=1577081015618&reqId=f4af2221-2549-11ea-92dc-ble779c8d1d6'
# print(music_name, music_id)
try:
jx_data = requests.get(jx_url, headers=headers).json()
music_url = jx_data['url']
music_data = requests.get(music_url, headers=headers).content
except:
pass
with open(path + new_name +'.mp3', 'wb') as f:
f.write(music_data)
print(f'...当前第{page}页...下载完成...' + new_name)
nahongyan1997 发表于 2021-8-5 12:03
1,代码前面的行号是网站自动加的;
2,把代码给我我看看我这边能不鞥正常打包。
D:\pythonProject\dist>kuowo.exe
==========酷我音乐下载============
===========按歌手下载=============
请输入歌手名字:张学友
请输入开始页数:1
请输入结束页数:10
正在下载第1页歌曲
Traceback (most recent call last):
File "kuowo.py", line 61, in <module>
NameError: name 'music_data' is not defined
Failed to execute script 'kuowo' due to unhandled exception!
234657812 发表于 2021-8-7 02:11
D:\pythonProject\dist>kuowo.exe
==========酷我音乐下载============
===========按歌手下载======== ...
import requests
import os
import re
from fake_useragent import UserAgent
# headers = {'User-Agent': UserAgent().random}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0',
'Referer':'http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6',
'csrf': 'HYAUG9BYRAI',
'Cookie': '__guid=112476674.3310808051482316000.1628257329943.9053; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1628257330; _ga=GA1.2.1222733808.1628257331; _gid=GA1.2.156578177.1628257331; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1628258543; _gat=1; kw_token=HYAUG9BYRAI; monitor_count=8'
}
# print(headers)
print('==========酷我音乐下载============\n===========按歌手下载=============')
name = input('请输入歌手名字:')
page1 = int(input('请输入开始页数:'))
page2 = int(input('请输入结束页数:'))
def change_name(name):
pattern = re.compile('[\\\/\:\"\?\|\<\>\:]') # \\\/\:\"\?\|\<\>\:
new_name = re.sub(pattern, '-', name)
return new_name
for page in range(page1, page2):
print(f'正在下载第{page}页歌曲')
# url = http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=%E5%91%A8%E6%9D%B0%E4%BC%A6&pn=1&rn=30&httpsStatus=1&reqId=639a10c0-f6ca-11eb-842a-290d889eed46 歌名搜索
# url = http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=%E5%91%A8%E6%9D%B0%E4%BC%A6&pn=1&rn=30&httpsStatus=1&reqId=0ece6030-f6bd-11eb-b3e0-6fbaea9acb93 歌手搜索
ss_url = 'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key='+name+f'&pn={page}&rn=30&httpsStatus=1&reqId=0ece6030-f6bd-11eb-b3e0-6fbaea9acb93'
# print(ss_url)
response = requests.get(ss_url, headers=headers)
try:
json_data = response.json()
music_list = json_data['data']['list']
except:
pass
if not os.path.exists('D:\\酷我音乐\\'):
os.mkdir('D:\\酷我音乐\\')
path = 'D:\\酷我音乐\\'
for music in music_list:
music_name = music['name']
new_name = change_name(music_name)
music_id = music['rid']
# print('正在下载:', music_name)
# 解析url:传入音乐ID 解析出这首音乐真实的播放地址(歌曲ID)
jx_url = 'http://www.kuwo.cn/url?format=mp3?&rid=' + str(music_id) + '&response=url&type=convert_url3&br=128kmp3&from=web&t=1577081015618&reqId=f4af2221-2549-11ea-92dc-ble779c8d1d6'
# print(music_name, music_id)
try:
jx_data = requests.get(jx_url, headers=headers).json()
music_url = jx_data['url']
music_data = requests.get(music_url, headers=headers).content
except:
pass
with open(path + new_name +'.mp3', 'wb') as f:
f.write(music_data)
print(f'...当前第{page}页...下载完成...' + new_name) 234657812 发表于 2021-8-7 02:14
import requests
import os
import re
我直接打包成功了 文件在 dist/kuwo/kuwo.exe,
重装pyinstaller吧
页:
[1]