简体   繁体   English

错误“在Python 3环境中使用f2py时,出现错误消息“需要类似字节的对象,而不是'str'”

[英]Error “ a bytes-like object is required, not 'str'” when using f2py in Python 3 environment

I found out that my f2py in python 3 environment may not be installed correctly. 我发现我在python 3环境中的f2py可能未正确安装。 Therefore, I tried to test the f2py using the file follows: 因此,我尝试使用以下文件测试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

Then I test the file in IPYTHON: 然后我在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 )

However, the error appears when running the compilation processes of f2py shows like: 但是,运行f2py的编译过程时会显示错误,如下所示:

TypeError Traceback (most recent call last) in () ----> 1 f2py.compile(source, modulename='forMatMul') ()中的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'

Some essential information are provided here: 这里提供一些基本信息:

## 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) 

I would like to have an advice how to proceed. 我想咨询如何进行。

PS PS

I have fixed this problem by editing the _init_py in numpy/numpy/f2py/. 我通过在numpy / numpy / f2py /中编辑_init_py来解决此问题。

Try opening the file in binary mode - open("forMatMul_ref.f90", "rb") - it will give you raw bytes for the source code (which seems to be what it expects) rather than decoding to a string. 尝试以二进制模式open("forMatMul_ref.f90", "rb")文件open("forMatMul_ref.f90", "rb") -它将为您提供源代码的原始字节(似乎是它的期望值),而不是解码为字符串。 This is probably a side-effect of them porting legacy python 2 code to run in python 3, otherwise a string object would seemingly be an appropriate representation for source code. 这可能是它们移植旧版python 2代码以在python 3中运行的副作用,否则,字符串对象似乎是源代码的适当表示形式。 If you are interested, this post describes what changed with strings between Python 2 and 3: https://timothybramlett.com/Strings_Bytes_and_Unicode_in_Python_2_and_3.html 如果您有兴趣,本文将介绍Python 2和3之间的字符串变化。https : //timothybramlett.com/Strings_Bytes_and_Unicode_in_Python_2_and_3.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python需要类似字节的对象,而不是'str' - Python a bytes-like object is required, not 'str' python错误TypeError:需要一个类似字节的对象,而不是'str' - python error TypeError: a bytes-like object is required,not 'str' Python SocketServer 错误:TypeError:需要类似字节的对象,而不是“str” - Python SocketServer Error : TypeError: a bytes-like object is required, not 'str' 错误:需要一个类似字节的对象,而不是“ str”(cPickle,Python) - Error: a bytes-like object is required, not 'str' (cPickle, Python) 需要类似 object 的字节,而不是 python 中的“str”错误 - a bytes-like object is required, not 'str' error in python Python错误:TypeError:需要一个类似字节的对象,而不是'str' - Python error: TypeError: a bytes-like object is required, not 'str' “需要类似字节的 object,而不是 str” Python 中的错误 - “A bytes-like object is required, not str” Error in Python 需要一个类似字节的对象,而不是'str' - 错误 - a bytes-like object is required, not 'str' - error 错误:需要一个类似字节的对象,而不是'str' - Error: a bytes-like object is required, not 'str' Python 错误:需要类似字节的对象,而不是“str” - Python error:a bytes-like object is required, not 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM