[英]Cannot use fortran code from f2py compile function
我有以下Fortran代碼:
!routines.f90
module mymodule
contains
function add(a, b)
real(8), intent(in):: a
real(8), intent(in):: b
real(8) :: add
add = a + b
end function
end module
而不是使用命令: python -m numpy.f2py -m routines -c routines.f90
,我想在python腳本中編譯,如下所示:
#main.py
import numpy as np
import numpy.f2py as f2py
with open(r'routines.f90', 'r') as f:
source = f.read()
f2py.compile(source, modulename='routines')
print('OK')
但是當我嘗試執行這個腳本時: python main.py
我收到以下錯誤:
Traceback (most recent call last):
File "main.py", line 7, in <module>
f2py.compile(source, modulename='routines')
File "/home/user1/anaconda3/lib/python3.6/site-packages/numpy/f2py/__init__.py", line 59, in compile
f.write(source)
File "/home/user1/anaconda3/lib/python3.6/tempfile.py", line 485, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
你能告訴我這是什么問題嗎?
open(r'routines.f90', 'r')
打開你的文件來讀取文本 (又名str
),但是,顯然, f2py.compile
要求它的第一個參數是bytes
類型。 為了滿足這一要求,請以二進制模式打開文件:
open(r'routines.f90', 'rb')
(另外, r'routines...'
不需要第一個r
r'routines...'
,你可以做'routines.f90'
,雖然它沒有太大變化)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.