在 Python 3 环境中使用 f2py 时出现错误“需要类似字节的对象,而不是 'str'”
Error " a bytes-like object is required, not 'str'" when using f2py in Python 3 environment
我发现我在python3环境下的f2py可能没有正确安装。因此,我尝试使用以下文件测试 f2py:
$vi forMatMul_ref.f90
subroutine matrixMult (C , A , B , n )
implicit none
integer , intent (in) :: n
real *8 , intent (in) :: A (n , n )
real *8 , intent (in) :: B (n , n )
real *8 , intent ( out ) :: C (n , n )
C = matmul (A , B )
return
end subroutine matrixMultul_ref.f90
然后我测试IPYTHON中的文件:
import numpy as np
import numpy . f2py as f2py
fid = open ("forMatMul_ref.f90")
source = fid.read()
fid.close()
f2py.compile(source, modulename = "forMatMul")
import forMatMul
AB = forMatMul . matrixmult (A , B )
但是,当运行 f2py的编译过程显示如下时出现错误:
TypeError Traceback(最后一次调用)
在 ()
----> 1 f2py.compile(来源,模块名='forMatMul')
/Users/HYF/anaconda/envs/py35/lib/python3.5/site-packages/numpy/f2py/__init__.py in compile(source, modulename, extra_args, verbose, source_fn, extension)
57
58 try:
---> 59 f.write(source)
60 f.flush()
61
/Users/HYF/anaconda/envs/py35/lib/python3.5/tempfile.py in func_wrapper(*args, **kwargs)
481 @_functools.wraps(func)
482 def func_wrapper(*args, **kwargs):
--> 483 return func(*args, **kwargs)
484 # Avoid closing the file as long as the wrapper is alive,
485 # see issue #18879.
TypeError: a bytes-like object is required, not 'str'
此处提供了一些基本信息:
## Computer OS
Darwin I.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64 i386 MacBookAir7,2 Darwin
## Python environment
/Users/HYF/anaconda/envs/py35/bin/python
## Fortran compiler (GCC 4.9.4 contains both C and Fortran compiler)
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../gcc-4.9-20141029/configure --enable-languages=c++,fortran
Thread model: posix
gcc version 4.9.2 20141029 (prerelease) (GCC)
(py35)
我想获得如何进行的建议。
PS
我已经通过编辑 numpy/numpy/f2py/ 中的 _init_py 解决了这个问题。
尝试以二进制模式打开文件 - open("forMatMul_ref.f90", "rb")
- 它会为您提供源代码的原始字节(这似乎是它所期望的)而不是解码为字符串。这可能是他们将遗留 python 2 代码移植到 python 3 中的 运行 的副作用,否则字符串对象似乎是源代码的适当表示。如果您有兴趣,这个 post 描述了 Python 2 和 3 之间字符串的变化:https://timothybramlett.com/Strings_Bytes_and_Unicode_in_Python_2_and_3.html
我发现我在python3环境下的f2py可能没有正确安装。因此,我尝试使用以下文件测试 f2py:
$vi forMatMul_ref.f90
subroutine matrixMult (C , A , B , n )
implicit none
integer , intent (in) :: n
real *8 , intent (in) :: A (n , n )
real *8 , intent (in) :: B (n , n )
real *8 , intent ( out ) :: C (n , n )
C = matmul (A , B )
return
end subroutine matrixMultul_ref.f90
然后我测试IPYTHON中的文件:
import numpy as np
import numpy . f2py as f2py
fid = open ("forMatMul_ref.f90")
source = fid.read()
fid.close()
f2py.compile(source, modulename = "forMatMul")
import forMatMul
AB = forMatMul . matrixmult (A , B )
但是,当运行 f2py的编译过程显示如下时出现错误:
TypeError Traceback(最后一次调用) 在 () ----> 1 f2py.compile(来源,模块名='forMatMul')
/Users/HYF/anaconda/envs/py35/lib/python3.5/site-packages/numpy/f2py/__init__.py in compile(source, modulename, extra_args, verbose, source_fn, extension)
57
58 try:
---> 59 f.write(source)
60 f.flush()
61
/Users/HYF/anaconda/envs/py35/lib/python3.5/tempfile.py in func_wrapper(*args, **kwargs)
481 @_functools.wraps(func)
482 def func_wrapper(*args, **kwargs):
--> 483 return func(*args, **kwargs)
484 # Avoid closing the file as long as the wrapper is alive,
485 # see issue #18879.
TypeError: a bytes-like object is required, not 'str'
此处提供了一些基本信息:
## Computer OS
Darwin I.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64 i386 MacBookAir7,2 Darwin
## Python environment
/Users/HYF/anaconda/envs/py35/bin/python
## Fortran compiler (GCC 4.9.4 contains both C and Fortran compiler)
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../gcc-4.9-20141029/configure --enable-languages=c++,fortran
Thread model: posix
gcc version 4.9.2 20141029 (prerelease) (GCC)
(py35)
我想获得如何进行的建议。
PS
我已经通过编辑 numpy/numpy/f2py/ 中的 _init_py 解决了这个问题。
尝试以二进制模式打开文件 - open("forMatMul_ref.f90", "rb")
- 它会为您提供源代码的原始字节(这似乎是它所期望的)而不是解码为字符串。这可能是他们将遗留 python 2 代码移植到 python 3 中的 运行 的副作用,否则字符串对象似乎是源代码的适当表示。如果您有兴趣,这个 post 描述了 Python 2 和 3 之间字符串的变化:https://timothybramlett.com/Strings_Bytes_and_Unicode_in_Python_2_and_3.html