[英]Error “ a bytes-like object is required, not 'str'” when using f2py in Python 3 environment
我發現我在python 3環境中的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追溯(最近一次調用)-> 1 f2py.compile(source,modulename ='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)
我想咨詢如何進行。
我通過在numpy / numpy / f2py /中編輯_init_py來解決此問題。
嘗試以二進制模式open("forMatMul_ref.f90", "rb")
文件open("forMatMul_ref.f90", "rb")
-它將為您提供源代碼的原始字節(似乎是它的期望值),而不是解碼為字符串。 這可能是它們移植舊版python 2代碼以在python 3中運行的副作用,否則,字符串對象似乎是源代碼的適當表示形式。 如果您有興趣,本文將介紹Python 2和3之間的字符串變化。https : //timothybramlett.com/Strings_Bytes_and_Unicode_in_Python_2_and_3.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.