Here is my file structure and requirements.txt:
Getting ModuleNotFoundError, any help will be appreciated.
main.py
from fastapi import FastAPI
from .import models
from .database import engine
from .routers import ratings
models.Base.metadata.create_all(bind=engine)
app = FastAPI()
app.include_router(ratings.router)
The error comes from the fact that you were not using the right environment and python version on VSCODE.
Your environment knew your different packages, but VSCode probably did not take them into account.
The solution was, in VSCODE: CTRL + SHIFT + P then Python:select interpreter and choose the version of python linked to your environment.
You can try to change the version of python to see the consequences on your imports
Try this to install all dependencies:
$ pip install "fastapi[all]"
Try this :
python -m pip install fastapi uvicorn[standard]
Related
I am used to see import errors of my own modules.
But this time, this is about modules installed via pip and located in site-packages.
Here is a test file expurged from everything but just the imports :
import flask
import pygments
from flask_restful import Resource, Api
#from weather_tool import *
#from flask_restless import *
while running pytest :
Traceback:
test_MonAPPflaskRest.py:3: in <module>
from flask_restful import Resource, Api
E ModuleNotFoundError: No module named 'flask_restful'
Actually, every module with an underscore will fail !!!
flask_restless will fail to import to.
But it work when executed outside of pytest, or simply on the python shell...
Ok, I went through it.
Actually, Anaconda was installed. From here, no problem with that.
I installed Python from source as I'm used to do on a Linux platform and it works normally as expected.
I found out that pytest was not is the list of packages installed via pip.
Seems Anaconda provides a default pytest install. Maybe something is wrong with this version. (which pytest will return a pystest file in the python bin directory.
Actually, a simple pip install pytest in your virtualenv will 'overwrite' this - maybe messy - pytest version.
However calling pytest won't still work. You have to user py.test.
I know this is pretty much empirical. But that's it...
I got a similar error while importing stocker (Unable to find stocker module).
Try adding your 'code directory' to 'sys.path' where python looks for modules and it should be able to import then.
For more details:
https://bic-berkeley.github.io/psych-214-fall-2016/sys_path.html
I used the code:
>>> import sys
>>> sys.path.append('code') # code = the directory where your module is saved
>>> # Now the import will work
>>> import a_module # a_module = the module you are trying to import
I also faced the same issue when installed in virtual environment. I've deactivated virtual environment, installed flask-restful from PIP and then activated virtual environment. The issue is resolved.
This is probably one of those annoying newbie questions. I'm trying to use a python package called jieba in my Django project. I've tried pip install and dragging the package folder into my apps directory, but have not succeeded in importing the package (ModuleNotFoundError). Please tell me how this could be done. Thanks!
Edit:
I mean I tried pip install jieba, and it didn't work (ModuleNotFoundError).
I made sure that it was correctly installed in my project virtual environment, but don't know how to import that in the Django project. Tried: import jieba and from jieba import jieba, no luck.
Then, I tried dragging that folder to the apps directory, and it still didn't work.
Update:
It turns out I have correctly installed jieba, but Atom Runner somehow cannot import it. I switched to PyCharm, and now it works fine. Nothing has changed except the editor.
Most likely you can't do:
import jieba
anymore because you've dragged/moved the package folder from where it supposed to be after pip installing it. Try to drag it back or uninstall and install package again, then import as normal.
Also if you are using virtual env make sure you activated your env before installing the package.
You don't need to copy python package from dist to your project root.
Pip packages is works standalone and not need to implement for django.
this is mean that after you need to only install your package with pip and use in project like following sample :
pip install jieba
# encoding=utf-8
import jieba
seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
print("Full Mode: " + "/ ".join(seg_list)) # 全模式
seg_list = jieba.cut("我来到北京清华大学", cut_all=False)
print("Default Mode: " + "/ ".join(seg_list)) # 精确模式
seg_list = jieba.cut("他来到了网易杭研大厦") # 默认是精确模式
print(", ".join(seg_list))
seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造") # 搜索引擎模式
print(", ".join(seg_list))
I am trying to create a project in python using some libraries from GitHub that I found. I am using InstaPy and Instagram-API-python.
My project structure is as follows
Project(main folder)
Instapy(sub-folder)
Instagram-API-python(sub-folder)
insta.py
How would I import both these into the insta.py file? I am new to python and not sure how the imports work.
Don't copy them into your project folder, use pip to install them on your Python path.
But first, read about virtual environments.
Once you have your virtual environment set up and activated you can install the packages thus:
$ pip install git+https://github.com/timgrossmann/InstaPy.git
$ pip install git+https://github.com/LevPasha/Instagram-API-python.git
Then, in your python script just import them
from instapy import InstaPy
import InstagramAPI
and use them.
I followed these instructions and it (mostly) worked. It looks like InstagramAPI needs an executable installed (ffmpeg) which I'm not wanting to install on my laptop. It reports the error during the initial import.
I could definitely import instapy .. but notice that it is all lower case.
But since there is only the InstaPy class in the instapy module, it's best to simply import it this way.
More on InstaPy here: https://github.com/timgrossmann/InstaPy
I installed tzwhere on an AWS EC2 instance using Python 2.7 and:
pip install tzwhere
When I import it in Python at "/opt/python/run/venv/lib/python2.7/site-packages", where it is installed, it works, but I cannot import it anywhere else. I added "/opt/python/run/venv/lib/python2.7/site-packages" to my system path, but it did not help.
I'll appreciate it if you guide me how to solve this problem.
Let me explain more details: I am deploying a Django 1.6 project on Amazon Elastic Beanstalk. I need to import tzwhere only once when I deploy the project. So I imported it in setting.py. When I activate the virtual environment (explained in this post), I am able to import tzwhere in python, but when I run:
sudo python manage.py schemamigration southtut --initial
It returns the following error message:
ImportError: Could not import settings 'JoinWikipedians.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named tzwhere
When I deactivate the virtual environment, importing tzwhere returns the import error. In addition, in any python environment, when I use:
sys.path.append('/opt/python/run/venv/lib/python2.7/site-packages')
I can import tzwhere, but the same code before importing tzwhere in my setting.py does not help.
I found the solution. The problem was that AWS EC2 has some problem with pip install tzwhere. Whenever I tried to install it, it returned the memory error message. I completely removed it and installed pytzwhere, using:
git clone --recursive https://github.com/pegler/pytzwhere.git
python setup.py install
Now it works perfectly.
In fact I cannot use any pakage now!
importerror: no module named flask.ext.script
importerror: no module named Pymongo
It seems that you virtual environment doesn't work. You've installed the flask-script package, but when you run the script, it still looks for it in C:\Python3.4. You may give us more info so that we can figure it out where is wrong. (How do you install it, how do you active the virtualenv, does reinstall virtualenv work, close the cmd shell and try again works?)
Also note that from flask.ext.extension import xxx is the old way to use the flask extension. Instead you should use from flask_script import Manager, Server if you are using the latest flask-script 2.0.5
If you are using the IDE such as pycharm, then maybe need to set the interpreter of python for the right version. Otherwise the packages that you have installed can not be used for the current project. I have also encountered such kind of questions until I set my IDE's interpreter to the Python 2.7.
Then you can freely import the flask_script
I think you should use python 2 to use this module flask.ext.script" because this is the old way of doing it, or you could install Flask-Script and import it this way from flask_script import ...
from flask.ext.extension import xxx is the old way of importing extensions ,now it's not working .You have to freeze first to outputs the package and its version installed in the current environment . Then you can check your module and import it as usual
e.g: -from flask_bcrypt import Bcrypt
#this import the Bcrypt from flask_bcrypt