简体   繁体   中英

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. then I have created a new file name app.py with the following 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)

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.

EDIT : run python app.py instead of 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. In your case, it's better to install flask and spacy libraries with conda which are;

    $ 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

The steps I follow to create a conda environment;

  • conda create -n myenv python=3.6
  • conda activate 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM