问题描述
为了加快Python软件包中的部分代码的速度,我编写了一个Fortran子例程。在我的本地系统上可以正常工作。这是包的结构。
p_name/
setup.py
p_name/
__init__.py
fortran_sc_folder/
rsp.f95
这是setup.py(的一部分):
from numpy.distutils.core import setup,Extension
ext1 = Extension(name='p_name.fortran_sc_folder.rsp',sources=['p_name/fortran_sc_folder/rsp.f95'])
setup(
...
ext_modules = [ext1]
...
)
在程序内部,我使用以下代码访问模块:
import .fortran_sc_folder.rsp
它可以安装并正常工作,不会引起任何错误。但是,当我推送更改时,它无法通过GitHub操作。错误是:
E ModuleNotFoundError: No module named 'p_name.fortran_cs_folder.rsp'
您知道如何解决此问题,还有在Python包内使用Fortran代码的更好方法吗?
更新: 这是GitHub动作工作流程:
# This workflow will install Python dependencies,run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
name: Python package
on:
push:
branches: [ master,develop ]
pull_request:
branches: [ master,develop ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7,3.8]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Setup conda
uses: s-weigand/setup-conda@v1
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
conda install -c conda-forge cartopy
# - name: Lint with flake8
# run: |
# # stop the build if there are Python syntax errors or undefined names
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
解决方法
@RossMacArthur的评论/问题之后,我意识到GitHub action上的包是代码库,而不是编译后的版本。由于使用Numpy库中的setup
在安装过程中会编译Fortran代码(from numpy.distutils.core import setup
),因此我们需要在GitHub工作流程中安装该软件包。结果,我在install dependencies
部分的末尾添加了以下行。
pip install -e ../p_name
问题解决了。