Python环境及模块安装、调用等问题——环境设置、导出、pip安装模块等

一、解决Fatal error in launcher错误

使用pip安装Python模块时,出现Fatal error in launcher:
问题:
win10的Python3安装第三方模块

解决:
使用pip时前面加入python -m pip install model_name
如:
win10环境下

二、安装时

1、指定/更改安装源

使用国内源安装:
使用清华的安装源
国内常用安装源:https://blog.csdn.net/yuzaipiaofei/article/details/80891108
【注意】报错ERROR: Cannot determine archive format of /tmp/pip-req-build-l6ljuc7l
需完善安装命令,即:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/  --trusted-host pypi.tuna.tsinghua.edu.cn Beautifulsoup4==4.9.3

2、查看安装的模块文件所在位置

1)查看自带包 / 第三方包:

>>> import sklearn
>>> sklearn.__file__
'/home/yxpt/.local/lib/python3.10/site-packages/sklearn/__init__.py'

2)下述方式只能用来查看第三方包:

pip show scikit-learn

结果:
在这里插入图片描述

3、基于requirements.txt安装

pip install -r requirements.txt # 后面可以跟 -i https://pypi.tuna.tsinghua.edu.cn/simple/ 

4、查看安装的所有python模块

pip查看命令:pip list
conda查看命令:conda list

5、

Ubuntu使用代理安装第三方库:
本地电脑需要安装代理软件,如CCProxy。
在这里插入图片描述

6、部分模块的常见安装问题

2)安装fitz:ModuleNotFoundError: No module named 'exceptions'
在这里插入图片描述
安装PyMuPDF模块即可: pip install PyMuPDF
1)安装docx:ModuleNotFoundError: No module named 'frontend'
在这里插入图片描述
卸载docx模块安装python-docx即可:pip install python-docx

三、安装pyinstaller相关

1、安装
pip安装成功后需要进入Python的scripts文件下执行,验证安装是否成功

pyinstaller -v

当出现如下错误时:

File "D:\cqf_软件\Python38\Scripts\pyinstaller-script.py", line 1
SyntaxError: Non-UTF-8 code starting with '\xc8' in file D:\cqf_软件\Python38\Scripts\pyinstaller-script.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

需要在pyinstaller-script.py文件中首行加入:# coding:utf-8
2、使用
使用pyinstaller为指定的.py文件生成exe可执行程序:

D:\cqf_软件\Python38\Scripts>pyinstaller -F D:\CQF\work_内容\package_post_automatic\post_pkg_reviews.py

四、安装bertopic相关

使用pip安装bertopic时,因为其依赖库hdbscan的原因,总是出现错误,包括:

Could not build wheels for hdbscan which use PEP 517 and cannot be installed directly
error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: http://landinghub.visualstudio.com/visual-cpp-build-tools
ValueError: ZIP does not support timestamps before 1980

针对第①个错误,更换安装源、upgrade pip啥的都不行;
针对第②个错误,下载 visualcppbuildtools_full.exe ,然后安装Microsoft Visual C++ Build Tools,因为电脑上有VS2015,必须得卸载了才能安装,卸载要很久很久很久……
然后,
直接下载压缩包形式的安装包hdbscan-0.8.27.tar.gz,运行
Python setup.py build
python setup.py install
第一步依然会报第②个错误,bertopic的压缩包形式安装会报第③个错误。
解决:
下载 .whl 安装包
下载路径:
将安装包复制到Python安装目录的Scripts文件夹下,
打开cmd(命令提示符),跳转到该目录下分别执行,安装成功:

pip install hdbscan-0.8.27-cp38-cp38-win_amd64.whl
pip install bertopic-0.8.1-py2.py3-none-any.whl

hdbscan安装包下载路径:https://www.lfd.uci.edu/~gohlke/pythonlibs/#hdbscan
bertopic安装包下载路径:https://pypi.org/project/bertopic/#files

五、环境设置

1、环境导出

1、Python环境导出
保存当前Python环境下所有类库包。(保存到当前路径下)
在这里插入图片描述
结果:
在这里插入图片描述

2、指定项目在当前环境下的导出
注意:默认情况下,是没有安装“pipregs”插件,所以会提示错误pipreqs: command not found,可以使用以下命令安装:

pip install pipreqs

安装好后,先定位到项目根目录,然后调用

 pipreqs ./ --encoding=utf8 

并自动在根目录生成 requirements.txt 文件。
如:
在这里插入图片描述

六、模块调用相关问题

文件夹
  code_general 文件夹
    主程序.py
  operate_DB 文件夹
    operate_mysql.py
    operate_kbase.py
当主程序和被调用模块关系如上时,模块调用方式如下:

import os
import sys
curPath = os.path.dirname(__file__)
curPath_root = os.path.dirname(curPath)
sys.path.append(curPath_root)
from operate_DB import operate_mysql
from operate_DB import operate_kbase
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值