python 如何解决ModuleError当我已经在虚拟环境中安装模块时,在gitlab yml中?

gblwokeq  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(130)

我有一个main.pyx文件,我将其cythonize,以给予给main.c。我在gitlab CI/CD中构建了这个C文件,并运行它,但它给了我NoModuleFoundError。
~

main.pyx

import numpy as np
..

~

  • cythonmain.pyx--embed*

~

.gitlab-ci.yml

stages:

- build

image: python:3.9.16

variables:
HTTP_PROXY: $CODE_PROXY
HTTPS_PROXY: $CODE_PROXY

before_script:

- apt-get clean
- apt-get --allow-unauthenticated update
- apt-get upgrade
- apt-get install -y gcc
- apt-get update
- apt search '^python\[0-9\]\*-dev'
- python3 --version

build-job:  
stage: build
script:
- echo "Compiling the code..."
- python3 -m pip install --upgrade pip
- python3 -m venv venv
- source /path/venv/bin/activate
- python3 -m pip install --upgrade pip
- pip install pyimagej
- pip show numpy
- python3 check_py.py
- gcc -v main.c -I /usr/include/python3.9/  -I /usr/include/ -I /usr/local/include -I /usr/local/include/python3.9 -L /usr/lib/python3.9 -lpython3.9 -o main_output
- ./main_output
- echo "Compile complete."

~
我尝试运行CI/CD,但得到以下错误:

Traceback (most recent call last):
  File "main.pyx", line 1, in init main
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

我希望main_output能顺利运行,但我不确定如何使其工作。如有任何意见,将不胜感激。

flmtquvp

flmtquvp1#

我必须将pyx文件定向到numpy包的正确位置。

import sys
sys.path.append(".../venv/lib/python3.9/site-packages")
import numpy as np

很简单!

相关问题