Unable to find vcvarsall.bat

本文详细记录了解决在Windows系统下使用py-bcrypt进行密码哈希加密时遇到的问题,包括安装mingw32环境、修复gcc命令行选项、应用补丁以及最终使用微软的MSVC编译器成功解决问题的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目在Windows下构建,某个密码哈希加密模块使用了py-bcrypt,可惜的是官方只提供了C语言源代码,没有现成的Windows二进制包,我还翻阅了《Unofficial Windows Binaries for Python Extension Packages》也没找到现成的包,当然这个对于Linux系统不成问题,按部就班的安装就OK啦,可是Windows下遇到了些麻烦,比如error: Setup script exited with error: Unable to find vcvarsall.bat错误,于是我采用mingw32进行编译,当然如果你的电脑上没有安装mingw32,可以参考下面的步骤,[来源]:

1. 点击这里下载最新版本的MinGW,并安装。

2. 设置PATH环境变量,比如你装在C:\MinGW路径下,你需要在PATH环境变量中添加C:\MinGW\bin路径。


完成上面两步后切换到项目路径,然后运行命令python setup.py install build --compiler=mingw32,本来以为一切顺利,结果偏偏报cc1.exe: error: unrecognized command line option '-mno-cygwin'错误:

running install
running bdist_egg
running egg_info
creating py_bcrypt.egg-info
writing py_bcrypt.egg-info\PKG-INFO
writing top-level names to py_bcrypt.egg-info\top_level.txt
writing dependency_links to py_bcrypt.egg-info\dependency_links.txt
writing manifest file 'py_bcrypt.egg-info\SOURCES.txt'
reading manifest file 'py_bcrypt.egg-info\SOURCES.txt'
writing manifest file 'py_bcrypt.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib.win-amd64-2.7
creating build\lib.win-amd64-2.7\bcrypt
copying bcrypt\__init__.py -> build\lib.win-amd64-2.7\bcrypt
running build_ext
building 'bcrypt._bcrypt' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\bcrypt
D:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -ID:\Python27\include -ID:\Pytho
n27\PC -c bcrypt/bcrypt_python.c -o build\temp.win-amd64-2.7\Release\bcrypt\bcry
pt_python.o
cc1.exe: error: unrecognized command line option '-mno-cygwin'
error: command 'gcc' failed with exit status 1
搜索了网络,找到这么篇求助《mingw32编译安装Python第三方库失败,:unrecognized command line option ‘-mno-cygwin’》,得知-mno-cygwin参数已经不被新版本的gcc支持了,所以需要修改源代码移除这个参数:

如果你的Python27装在C盘的,那么可以打开C:\Python27\Lib\distutils\cygwinccompiler.py文件,然后找到大概322行左右下面一段:

322
323
324
325
326
327
328
self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
compiler_so='gcc -mno-cygwin -mdll -O -Wall',
compiler_cxx='g++ -mno-cygwin -O -Wall',
linker_exe='gcc -mno-cygwin',
linker_so='%s -mno-cygwin %s %s'
% (self.linker_dll, shared_option,
entry_point))
删除所有的-mno-cygwin字符串,然后保存。

好了,接下来可以继续了,重复刚才的安装命令,但是我又想错了,编译py-bcrypt出现下面的错误提示:

running install
running bdist_egg
running egg_info
writing py_bcrypt.egg-info\PKG-INFO
writing top-level names to py_bcrypt.egg-info\top_level.txt
writing dependency_links to py_bcrypt.egg-info\dependency_links.txt
reading manifest file 'py_bcrypt.egg-info\SOURCES.txt'
writing manifest file 'py_bcrypt.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
running build_ext
building 'bcrypt._bcrypt' extension
D:\MinGW\bin\gcc.exe -mdll -O -Wall -ID:\Python27\include -ID:\Python27\PC -c bc
rypt/bcrypt_python.c -o build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o
bcrypt/bcrypt_python.c:29:26: error: unknown type name 'u_int8_t'
bcrypt/bcrypt_python.c:29:38: error: unknown type name 'u_int16_t'
bcrypt/bcrypt_python.c:29:49: error: unknown type name 'u_int8_t'
bcrypt/bcrypt_python.c: In function 'bcrypt_encode_salt':
bcrypt/bcrypt_python.c:56:2: warning: implicit declaration of function 'encode_s
alt' [-Wimplicit-function-declaration]
error: command 'gcc' failed with exit status 1
重点是形如unknown type name 'u_int8_t'的错误,Windows SDK环境下没有定义这个类型,最后在stackoverflow的这篇求助《py-bcrypt installing on win 7 64bit python》找到了答案,为了能够在Windows下编译,我们还需要打一个Patch,我们可以到这里下载这个Patch。

问题又来了:Windows下如何打这个Patch呢?幸好我们可以找到Python下的一个实用脚本python-patch,下载后可以重命名为patch.py并保存到C:\Python27\Scripts(假设Python安装在C盘)下,然后运行形如命令C:\Python27\Scripts\patch.py file1.patch来打patch,注意这里的file1.patch是你刚才下载的patch文件。

打好patch后,让我们再次执行刚才的编译安装命令,结果依旧失败:

running install
running bdist_egg
running egg_info
writing py_bcrypt.egg-info\PKG-INFO
writing top-level names to py_bcrypt.egg-info\top_level.txt
writing dependency_links to py_bcrypt.egg-info\dependency_links.txt
reading manifest file 'py_bcrypt.egg-info\SOURCES.txt'
writing manifest file 'py_bcrypt.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
running build_ext
building 'bcrypt._bcrypt' extension
D:\MinGW\bin\gcc.exe -mdll -O -Wall -ID:\Python27\include -ID:\Python27\PC -c bc
rypt/bcrypt_python.c -o build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o
bcrypt/bcrypt_python.c: In function 'bcrypt_encode_salt':
bcrypt/bcrypt_python.c:56:2: warning: pointer targets in passing argument 2 of '
encode_salt' differ in signedness [-Wpointer-sign]
bcrypt/bcrypt_python.c:29:6: note: expected 'u_int8_t *' but argument is of type
'char *'
D:\MinGW\bin\gcc.exe -mdll -O -Wall -ID:\Python27\include -ID:\Python27\PC -c bc
rypt/blowfish.c -o build\temp.win-amd64-2.7\Release\bcrypt\blowfish.o
D:\MinGW\bin\gcc.exe -mdll -O -Wall -ID:\Python27\include -ID:\Python27\PC -c bc
rypt/bcrypt.c -o build\temp.win-amd64-2.7\Release\bcrypt\bcrypt.o
writing build\temp.win-amd64-2.7\Release\bcrypt\_bcrypt.def
D:\MinGW\bin\gcc.exe -shared -s build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_p
ython.o build\temp.win-amd64-2.7\Release\bcrypt\blowfish.o build\temp.win-amd64-
2.7\Release\bcrypt\bcrypt.o build\temp.win-amd64-2.7\Release\bcrypt\_bcrypt.def
-LD:\Python27\libs -LD:\Python27\PCbuild\amd64 -lpython27 -lmsvcr90 -o build\lib
.win-amd64-2.7\bcrypt\_bcrypt.pyd
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x5b): undefined reference to `_imp__PyArg_ParseTupleAndKeywords'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x73): undefined reference to `_imp__PyExc_ValueError'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x7e): undefined reference to `_imp__PyErr_SetString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x9e): undefined reference to `_imp__PyExc_ValueError'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
xa9): undefined reference to `_imp__PyErr_SetString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
xdc): undefined reference to `_imp__PyString_FromString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x134): undefined reference to `_imp__PyArg_ParseTupleAndKeywords'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x15e): undefined reference to `_imp__PyEval_SaveThread'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x177): undefined reference to `_imp__PyEval_RestoreThread'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x1b0): undefined reference to `_imp__PyExc_ValueError'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x1bb): undefined reference to `_imp__PyErr_SetString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x1cb): undefined reference to `_imp__PyString_FromString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x20a): undefined reference to `_imp__Py_InitModule4'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x223): undefined reference to `_imp__PyModule_AddStringConstant'
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
这次出现大量的undefined reference to错误,搜索网络,然后设置C:\Python27\libs到环境变量中,错误依旧,然后我就怀疑是否和我的64位Windows 7和64位Python 27有关系了,结果还真是这个原因,最后还是使用微软的MSVC编译成功的,关于详细步骤可以参考下一篇文章。
<think>嗯,用户遇到了“无法找到vcvarsall.bat”的问题,这个问题看起来和Visual Studio的环境配置有关。根据用户提供的引用内容,有几个可能的原因和解决方案需要考虑。 首先,引用[1]提到需要清理VSINSTALLDIR环境变量,并且设置VSCMD_START_DIR来防止工作目录被更改。这说明环境变量的设置可能不正确,导致系统找不到vcvarsall.bat的位置。用户可能需要检查这些变量是否正确配置,尤其是VCINSTALLDIR是否指向了正确的Visual Studio安装路径。 然后引用[2]中的错误信息显示路径中有“E:\Program Files (x86)\Microsoft Visual Studio 14.0...”,但系统找不到该文件。这可能是因为Visual Studio没有正确安装,或者安装路径被更改了。用户需要确认Visual Studio是否已经正确安装,并且vcvarsall.bat确实存在于指定的路径下。如果路径不正确,可能需要重新安装或者修复Visual Studio的安装。 引用[3]提到Abaqus CAE找不到Visual Studio 2022,提示设置VS2022INSTALLDIR环境变量。这说明某些应用程序依赖于特定的环境变量来定位Visual Studio的安装路径。用户可能需要手动设置这些变量,指向正确的Visual Studio版本和安装目录。 引用[4]中的解决方案是通过设置VS90COMNTOOLS环境变量指向其他版本的VS工具路径。例如,如果用户安装了Visual Studio 2013,可以将VS90COMNTOOLS设置为%VS120COMNTOOLS%。这可能是因为某些旧工具链需要特定版本的环境变量,而新版本Visual Studio没有自动设置这些变量。用户需要根据自己安装的Visual Studio版本调整这些变量。 此外,引用[5]提到用户已经搜索了很长时间但找不到解决方案,可能问题比较复杂或者需要更详细的步骤。可能需要更系统地检查环境变量、安装路径、以及Visual Studio的组件是否完整安装,特别是与C++开发相关的部分。 综合来看,解决这个问题的步骤可能包括:确认Visual Studio是否正确安装,特别是C++组件;检查环境变量是否正确设置,尤其是VCINSTALLDIR和VS*COMNTOOLS;尝试手动运行vcvarsall.bat来配置环境;或者使用开发者命令提示符来确保环境已经正确初始化。此外,可能需要更新或重新安装Visual Studio,或者安装特定版本的构建工具,如Microsoft Build Tools或旧版本的VC++ redistributable。 用户可能需要按照这些步骤逐一排查,确认每个环节是否正确配置,从而解决找不到vcvarsall.bat的问题。</think>### 解决“vcvarsall.bat not found”的方法 #### 1. 确认Visual Studio安装情况 - **问题根源**:`vcvarsall.bat`是Visual Studio用于配置C++编译环境的脚本。若未安装**C++开发组件**或路径错误,会导致该文件缺失[^4]。 - **解决步骤**: 1. 打开Visual Studio Installer,选择对应版本(如VS2019/2022)。 2. 勾选“使用C++的桌面开发”及相关组件(如MSBuild工具、Windows SDK)。 3. 完成安装后,检查路径是否存在: $$ \text{默认路径:}C:\Program Files (x86)\Microsoft Visual Studio\<年份>\<版本>\VC\Auxiliary\Build\vcvarsall.bat $$ #### 2. 设置环境变量 - **关键变量**: - `VCINSTALLDIR`:指向Visual Studio的VC目录,例如: $$ \text{VS2022: }C:\Program Files\Microsoft Visual Studio\2022\Community\VC\ $$ - `VS90COMNTOOLS`:兼容旧工具链时需映射到新版本路径,例如: ```bat SET VS90COMNTOOLS=%VS160COMNTOOLS% :: 对应VS2019 ``` 具体映射关系见引用[4]。 #### 3. 手动调用vcvarsall.bat 在命令行中直接初始化环境: ```bat call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 ``` 若提示权限问题,以管理员身份运行命令行[^1]。 #### 4. 使用开发者命令提示符 Visual Studio内置的开发者命令提示符已预配置环境变量,可直接使用编译命令[^2]。 #### 5. 安装独立构建工具(无需完整VS) 下载 [Microsoft Build Tools](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022) 或 [Visual C++ Build Tools](https://aka.ms/vs/16/release/vs_buildtools.exe),安装“C++生成工具”组件。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值