SqlAlchemy problem in Virtual Environment: cannot import name 'inspect' from 'sqlalchemy' - python

I've been using this tutorial to setup flask on ubuntu server.
It worked fine for one project, now I try to setup it on the other one.
This time the app works fine without SQLAlchemy, but once I use it, it gives 'Internal server error'.
checked the installation of flask_sqlalchemy - looks fine
made sure I don't have a file named 'sqlalchemy'
I can launch the app with sqlalchemy but without virtualenv, it works fine!
I double-checked that flask_sqlalchemy has the same configuration in virtualenv library, as in the python library
error that I receive:
Traceback (most recent call last):
File "myproject2.py", line 4, in <module>
from flask_sqlalchemy import SQLAlchemy
File "/root/myproject2/myprojectenv2/lib/python3.8/site-packages/flask_sqlalch emy/__init__.py", line 16, in <module>
from sqlalchemy import event, inspect, orm
ImportError: cannot import name 'inspect' from 'sqlalchemy' (/root/myproject2/my projectenv2/lib/python3.8/site-packages/sqlalchemy/__init__.py)
Thank you for any suggestions!

It seems your installation is broken in the virtualenv. Try reinstalling by running this from within the virtualenv:
pip install --upgrade --force-reinstall flask-sqlalchemy sqlalchemy

I have run into same problem but with different kind of issue. While upgrading to recent flask_sqlalchemy and sqlalchemy libraries, I have noticed that construct has been changed from:
from flask_sqlalchemy import inspect
this got broken, and new construct is:
from sqlalchemy import inspect
just want to state something that may be easily overseen
apart from that the issue may be wrong libraries in venv on specific installations
I have system python 3.10 however I run app under python 3.7 for compatibility reasons. On that line, I have noticed that it is easy to mix python library versions and get 3.10 library under 3.7 venv - pay attention during installation of SQL libraries - pip may go wild.
hope these hints help someone as these issues drove me nuts for days

Related

Can't import flask-marshmallow into python3 project

I have only python 3.8.5 installed.
I started diving into Python and Flask for the first time (I'm coming from the javascript side of things). I've already ran into an interesting issue in just my first few lines of code.
I was able to run pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy and they all seemed to have installed just fine. They all appear in the pipfile.lock. flask-marshmallow is version 0.13.0.
pipfile.lock
When I started coding, I was able to import flask and flask_sqlalchemy with no issues. Intellisense even helped me out with them. But from flask-marshmallow import Marshmallow didn't seem to work.
When I ran python app.py I get the following error
Traceback (most recent call last):
File "app.py", line 3, in <module>
from flask_marshmallow import Marshmallow
ModuleNotFoundError: No module named 'flask_marshmallow'
I've tried uninstalling flask-marshmallow and marshmallow and reinstalling. The console always says the installation is successful but I can't seem to import it when all the other packages seem to work fine.
What a great start to a new language xD Here is the whole file so far for reference, if I remove the marshmallow line it starts without any issues
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
app = Flask(__name__)
if __name__ == "__main__":
app.run(debug=True)
Make sure that, because you have created a pipenv, you have set the Python interpreter path correctly in your IDE.
I was facing the exact issue (and that is how I reached this question). I am using VS code, was using python3.8 and pipenv.
Even though I have installed the packages using pip3, I was facing import issues while running the code. After a futile search on the Internet, I realised that the issue was very silly.
The Python interpreter path (Cntrl+Shift+P -> Select Interpreter) was not set to the newly created pipenv. After I have set the interpreter path properly, the code resumed to function as expected.
i would suggest to check the site packages file and make sure that it is installed in it if not then it is installed in other directory
Also try working it in another normal python file and check if that works too
Check syntax also i know i know its obvious but just saying
In my case I was trying to run the command flask db init, and it would give the error above. I got it to work by running the command with sudo privileges sudo flask db init. I hope this can help someone else out in a similar situation!

Pylint error: no name in module when import a package

I'm learning a little bit how function python, more specific Flask.
I'm creating a new project using flask, but when I create a new file and import Flask I receive this error: No name 'Flask' in module 'flask'pylint(no-name-in-module)
I did read that, I need create a file called: __init__.py but I have this and the error keeps showing up.
This is my actual code (very simple code, and folder/files structure):
Thanks for the help! :D
I tried this: Flask ImportError: No Module Named Flask, but this solution does not works for me.
I installed flask on python 2.7 and run simple hello world script and it worked fine. Then I updated python to python 3.8 and I got an error similar to yours:
Unable to import 'flask'pylint(import-error)
I simply uninstalled flask and installed again and it worked for me. Run this two commands:
pip uninstall flask
pip install flask
Then export the FLASK_APP environment variable and run flask
set FLASK_APP=file.py
flask run
I solved this changing my python interpreter. I did uninstall pylint and install pylama and now all is fine!

ImportError: No module named flask_login even though I have it installed

I'm new at this and have no idea of what I'm doing wrong:
I have installed the flask_login module with
pip install flask_login
It apparently worked: if see it installed in c:\users\~\python\python36-32\lib\site-packages
And when I use pip list, I can see it listed as Flask-Login (0.4.0)
Despite of this, when I try to run from flask_login import LoginManager, I'm getting ImportError: No module named flask_login.
What am I doing wrong?
I figured out what was happening.
For some reason, Anaconda installed Python 2.7 in a path that was colliding with the one I use normally (w/ Python 3). Once I uninstalled Python 2.7, everything worked fine.
For some reason, only flask_login was being affected. My other flask_** libraries were working fine.
In case you are experiencing this in a Python environment with Apache2 and mod_wsgi: you have to restart the Apache2 server after installing new Python modules, as otherwise Apache2 will not "see" them even though the Python path is correct. So (assuming Debian / Ubuntu Linux):
service apache2 restart
Of course, this issue could happen with any module not being found, not just with flask_login. It just happened to me with flask_login, which is why I'm here …
I had the same problem and was able to solve using sudo:
sudo pip install flask_login
I think import errors are so common in flask because flask users normally work in virtual environments. Running pip install inside a VE or outside leads to different installations in different paths and can cause confusion. sudo installation works globally and can be a solution if you don't need a certain dependency in a specific VE.
I was using VS code. In the .venv there was another copy of flask_login so basically just check if another copy of flask_login exists within your lib

importerror: no module named flask.ext.script

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

Problem import Zope.deprecation

I'm trying to build an application using the new-ish Pyramid framework. I'm new to Pyramid and have no idea what Zope does (the importance of which will become apparent). I followed the basic tutorial, but when I try to run the application I get this traceback:
Traceback (most recent call last):
File "tasks.py", line 4, in <module>
from pyramid.config import Configurator
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pyramid-1.2-py2.6.egg/pyramid/__init__.py", line 1, in <module>
from pyramid.request import Request
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pyramid-1.2-py2.6.egg/pyramid/request.py", line 1, in <module>
from zope.deprecation import deprecate
ImportError: No module named deprecation
I tried install Pyramid using setuptools:
sudo easy_install pyramid
Which, among a lot of other output included this:
Searching for zope.deprecation
Reading http://pypi.python.org/simple/zope.deprecation/
Best match: zope.deprecation 3.5.0
Downloading http://pypi.python.org/packages/source/z/zope.deprecation/zope.deprecation-3.5.0.tar.gz#md5=1e7db82583013127aab3e7e790b1f2b6
Processing zope.deprecation-3.5.0.tar.gz
Running zope.deprecation-3.5.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ZiLy8j/zope.deprecation-3.5.0/egg-dist-tmp-yGFSor
Adding zope.deprecation 3.5.0 to easy-install.pth file
Which I thought should cover it.
I also tried installing zope via macports:
sudo port install zope
sudo port install py26-zopeinterface
I've also tried to run sudo port load zope before running the script, but no help there.
I also tried downloading the source from http://pypi.python.org/pypi/zope.interface and building it manually in directory.
Any ideas what I'm doing wrong?
----EDIT----
One guess is that setuptools in installing its zope components into my system's default Python framework (which is an Enthought distribution), but my default python environment is the one that Macports installed, so perhaps the Macports zope module has some but not all of the components, such as zope.deprecate.
The documentation includes information about how to use a virtualenv to install Pyramid. It's very, very recommended to use a virtualenv this way. System packages already installed in your main Python will have weird interactions with Pyramid's requirements.
You may get an answer from someone more familiar with Pyramid, but the problem here is that pyramid.request depends on zope.deprecation and it isn't installed/available. You'll have to elaborate much more on "I've tried installing zope using ..." for me to help you more.
Be sure you're running pyramid from the same python that you did the installation to. If that's the case then the zope.deprecation package should be installed there. What do you get from python -c "import zope.deprecation ; print import zope.deprecation"?
I solved it by installing zope.deprecation OS package:
sudo apt-get install zope.deprecation
Just re-install pyramid to fix.
I have had problem with moving class into sub-module.
The problem was another component of software was importing class from "OLD" module. And also there was cycling problem with inheritance.
So the solution was to use zope.deferredimport. The code looks like this:
import warnings
import zope.deferredimport
warnings.simplefilter("default")
zope.deferredimport.initialize()
zope.deferredimport.deprecated(
"Import from openprocurement.contracting.core.tests.base instead",
BaseContractWebTest='openprocurement.contracting.core.tests.base:BaseContractWebTest',
)
So now, when any component of software, that uses import from "OLD"
module doesn't get ImportError. Class is imported from "NEW" module. And warning is displayed like this:
DeprecationWarning: BaseContractWebTest is deprecated. Import from openprocurement.contracting.core.tests.base instead
So this is it. The main part zope.deferred took care of cycle imports. That saved my life.

Categories