简体   繁体   English

Python 虚拟环境错误:flask 和 spacy 库的模块未找到错误

[英]Python virtual environment error: Module not found error for flask and spacy libraries

I am developing a web application using flask and spacy libraries for which I have created a virtual environment using the following command: conda create -n mylgappflaskenv python=3.6 which gets created and then I activate my virtual environment using the following command activate mylgappflaskenv then to install spacy and flask I execute pip install spacy and pip install flask one after the another and they get installed successfully.我正在使用flask和spacy库开发一个Web应用程序,我使用以下命令为其创建了一个虚拟环境: conda create -n mylgappflaskenv python=3.6它被创建,然后我使用以下命令activate mylgappflaskenv我的虚拟环境activate mylgappflaskenv then to安装 spacy 和flask 我一个接一个地执行pip install spacypip install flask ,它们安装成功。 then I have created a new file name app.py with the following code然后我用下面的代码创建了一个新的文件名 app.py

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
for token in doc:
    print(token.text)

on running this file I am getting following error:在运行此文件时,我收到以下错误:

Traceback (most recent call last):
  File "app.py", line 16, in <module>
     import spacy
ImportError: cannot import name 'spacy' 

steps脚步

step1: conda create -n mylgapp2 python=3.6
step2: conda activate mylgapp2
step3: conda install -c conda-forge spacy
step4: python -m spacy download en_core_web_sm --> it gives error
step5: conda install -c anaconda flask

step6: write code: 

import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
for token in doc:
    print(token.text)

step 7: conda run app.py

what am I missing, how can I resolve this issue我错过了什么,我该如何解决这个问题

Do you have a line in your flask app.py that is attempting to import from spacy import spacy if so I'm not sure that's a valid spacy import.你的烧瓶app.py中有一行试图from spacy import spacy导入from spacy import spacy如果我不确定这是一个有效的 spacy 导入。

EDIT : run python app.py instead of conda run app.py编辑:运行python app.py而不是conda run app.py

Since you are using conda virtual environment manager, it's recommended to use conda package installation tool, which is conda , to install a library.由于您使用的是 conda 虚拟环境管理器,因此建议使用 conda 包安装工具conda来安装库。 In your case, it's better to install flask and spacy libraries with conda which are;在您的情况下,最好使用 conda 安装 flask 和 spacy 库,它们是;

    $ conda install -c anaconda flask
    $ conda install -c conda-forge spacy
    $ python -m spacy download en_core_web_sm

These are the references flask-anaconda and spacy usage这些是参考flask-anaconda和spacy用法

The steps I follow to create a conda environment;我遵循的步骤来创建一个 conda 环境;

  • conda create -n myenv python=3.6 conda create -n myenv python=3.6
  • conda activate myenv conda 激活 myenv

This step shows me the environment name in my line also此步骤还向我显示了我行中的环境名称

    $ (myenv)

You can install required libraries after you make sure you are in the correct virtual environment在确保您处于正确的虚拟环境中后,您可以安装所需的库

    $ (myenv) conda install -c anaconda flask
    $ (myenv) conda install -c conda-forge spacy

After you install all these libraries, make sure they are in your package list安装所有这些库后,请确保它们在您的软件包列表中

    $ (myenv) conda list

If you see libraries in your package list, you are good to go.如果您在包列表中看到库,那么您就可以开始了。

Have you checked that the path of your python and pip executable is the one of your conda environment?您是否检查过 python 和 pip 可执行文件的路径是您的 conda 环境之一? ie: IE:

$ (myenv) which python
XXXX/XXXXX/anaconda3/envs/myenv/bin/python
$ (myenv) which pip
XXXX/XXXXX/anaconda3/envs/myenv/bin/pip

If that is not the case, it might be a path issue, which can happen if you prepend something to your PATH after activating the environment.如果不是这种情况,则可能是路径问题,如果您在激活环境后在PATH某些内容,则可能会发生这种情况。

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

相关问题 python虚拟环境中的错误 - error in python virtual environment Python Flask两个站点和使用Paramiko库错误的虚拟环境 - Python Flask two sites and Virtual Environment using Paramiko Library Error 虚拟环境“未命名模块”错误 - Virtual Environment “no module named” error Python &amp; Flask 错误使用 Flask-MySQL | 未找到模块错误:没有名为“flask_mysqldb”的模块 - Python & Flask error using Flask-MySQL | Module Not Found Error: No module named 'flask_mysqldb' Python - 尝试激活虚拟环境时出现“无法加载模块”错误 - Python - "Module could not be loaded" error while trying to activate a virtual environment 将 Python Flask 应用程序部署到 Azure 应用程序服务时出现错误 ModuleNotFoundError: No module named 'spacy' - While Deploying Python Flask App to Azure App Service getting error ModuleNotFoundError: No module named 'spacy' 在虚拟环境中找不到模块 - Module not found with virtual environment 未找到模块 - 虚拟环境 - Module Not Found - virtual Environment 未找到模块错误:flask 未找到模块 - Module Not Found error :flask module not found 从新环境导入 Python package:找不到模块错误 - Import Python package from new environment: module not found error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM