[英]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 spacy
和pip 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 环境;
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.