Total python noob here, I want to run my .py flask api file, i cd to my directory, activate my python virtual env, then i try run.py
Issue is that it prints:
$ python run.py
Traceback (most recent call last): File "run.py", line 1, in
from flask_sqlalchemy import SQLAlchemy ImportError: No module named flask_sqlalchemy
As far as im aware as im in my virtualenv and have my packages installed, this shouldnt happen. Trying to pip3 install on any packages shows them as already existing.
To further assist with the issue, i tried to run the script inside pycharm and i get the following error prints in the log:`Traceback (most recent call last):
File "/Users/Jack/Developer/Python/lift_api/run.py", line 26, in <module>
sqlAlchemy.create_all()
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 1007, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 999, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 941, in get_engine
return connector.get_engine()
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 543, in get_engine
self._engine = rv = sqlalchemy.create_engine(info, **options)
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/engine/__init__.py", line 387, in create_engine
return strategy.create(*args, **kwargs)
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 80, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 554, in dbapi
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'`
Anyone have an idea?
To run your code with python 3.x use python3 command.
python3 run.py
What is saying what it needs - psycopg2. If you are on Ubuntu it can help:
sudo apt install libpq-dev python-dev
Then don't forget to install flask-sqlalchemy and other libraries while your virtualenv activated.
Related
I created a virtualenv with following installed:
$ pip freeze
aniso8601==9.0.1
attrs==22.2.0
click==8.1.3
configparser==5.3.0
Flask==2.2.2
flask-restplus==0.13.0
Flask-SQLAlchemy==3.0.2
greenlet==2.0.1
itsdangerous==2.1.2
Jinja2==3.1.2
jsonschema==4.17.3
MarkupSafe==2.1.1
mysql-connector-python==8.0.31
protobuf==3.20.1
PyMySQL==1.0.2
pyrsistent==0.19.2
pytz==2022.7
six==1.16.0
SQLAlchemy==1.4.45
Werkzeug==2.2.2
You can see I have PyMySQL and mysql-connector-python installed but still I am getting this error:
Traceback (most recent call last):
File "/home/ciasto/Dev/crud_app/src/start.py", line 1, in <module>
from todo_app.extensions import db
File "/home/ciasto/Dev/crud_app/src/todo_app/extensions.py", line 5, in <module>
db = SQLAlchemy(app)
File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 219, in __init__
self.init_app(app)
File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 320, in init_app
engines[key] = self._make_engine(key, options, app)
File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 606, in _make_engine
return sa.engine_from_config(options, prefix="")
File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 743, in engine_from_config
return create_engine(url, **options)
File "<string>", line 2, in create_engine
File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned
return fn(*args, **kwargs)
File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 548, in create_engine
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 163, in dbapi
return __import__("MySQLdb")
ModuleNotFoundError: No module named 'MySQLdb'
I have tried reinstalling
pip install MySQL-python
pip install ConfigParser
pip install MySQLdb
I have tried the solutions from the ModuleNotFoundError: No module named 'MySQLdb'
but it didn't worked for me.
I am using ubuntu jammy on x86 platform.
I was able to fix this issue by first installing
apt install libmysqlclient-dev
then pip install mysqlclient
I am facing the below issue while installing thonny ide for python.
Traceback (most recent call last):
File "/home/shrivatsa/.local/lib/python3.5/site-packages/thonny/workbench.py", line 287, in _init_runner runner.start()
File "/home/shrivatsa/.local/lib/python3.5/site-packages/thonny/running.py", line 67, instart self.reset_backend()
File "/home/shrivatsa/.local/lib/python3.5/site-packages/thonny/running.py", line 427, in reset_backend self._proxy = backend_class(configuraration_option)
File "/home/shrivatsa/.local/lib/python3.5/site-packages/thonny/running.py", line 596, in __init__ self._start_new_process()
File "/home/shrivatsa/.local/lib/python3.5/site-packages/thonny/running.py", line 762, in _start_new_process my_env["JEDI_LOCATION"] = self_prepare_jedi()
File "/home/shrivatsa/.local/lib/python3.5/site-packages/thonny/running.py", line 703, in _prepare_jedi import jedi ImportError: No module named 'jedi'
The application launches but no run button and dedug button is active.
As the error message shows you are missing the module named "jedi".
Try to install it before installing the IDE:
pip install jedi
According to Thonny blog, this IDE uses Jedi (autocompletion/static analysis library). So make sure it also installed (via pip, for example).
If i locally run my code with python main.py, then this error occurs locally. If i run it with python3 main.py, it works as expected. I suspect my azure app service is not running the correct version of python, however, it is set to python 3.4
StdErr:
2018-06-30 19:47:13.785205: Unhandled exception in wfastcgi.py: Traceback (most recent call last):
File "D:\Python34\Scripts\wfastcgi.py", line 711, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\Python34\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
return env, get_wsgi_handler(handler_name)
File "D:\Python34\Scripts\wfastcgi.py", line 541, in get_wsgi_handler
handler = handler()
File ".\ptvs_virtualenv_proxy.py", line 120, in get_venv_handler
handler = get_wsgi_handler(os.getenv('WSGI_ALT_VIRTUALENV_HANDLER'))
File ".\ptvs_virtualenv_proxy.py", line 89, in get_wsgi_handler
raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb))
ValueError: "main.app" could not be imported: Traceback (most recent call last):
File ".\ptvs_virtualenv_proxy.py", line 73, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File "D:\home\site\wwwroot\main.py", line 2, in <module>
from newspaper import Article
ImportError: No module named 'newspaper'
UPDATE
So I went to the debug console in kudo and i ran python
-m pip install --upgrade -r d:\home\site\wwwroot\requirements.txt.
Here is the error
ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" ** make sure the development packages of libxml2 and libxslt are installed ** Using build configuration of libxslt
One of the requirements is Pillow which requires libxml2 and libxslt. I've tried using wheels however, I've had no success.
UPDATE II
I tried to install virtualenv since it does not exist and I receive a permissions error.
UPDATE III
I ran this command
D:\home\python364x64>python -m pip install newspaper3k
I test everything locally, and it works perfect. I push and receive this error below.
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "D:\home\python364x64\wfastcgi.py", line 791, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\home\python364x64\wfastcgi.py", line 633, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "D:\home\python364x64\wfastcgi.py", line 600, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File ".\app.py", line 8, in <module>
from newspaper import Article
File "D:\home\python364x64\lib\site-packages\newspaper\__init__.py", line 10, in <module>
from .api import (build, build_article, fulltext, hot, languages,
File "D:\home\python364x64\lib\site-packages\newspaper\api.py", line 12, in <module>
import feedparser
File "D:\home\python364x64\lib\site-packages\feedparser.py", line 316
raise KeyError, "object doesn't have key 'category'"
^
SyntaxError: invalid syntax
StdOut:
StdErr:
Per my experience, we have no permission to modify D:\home environment.
So , It's better to use python extension. Please follow the steps in this case: Azure Flask Routes Not found and use command python -m pip install newspaper to install additional packages in python extension.
Please make sure the package you want to install must match with version of python extension. Otherwise, it will throw error as follows.
Hope it helps you.
I want to learn django so I tried to create a virtual env and I am getting this error:
mkvirtualenv django
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 5, in <module>
from pkg_resources import load_entry_point
File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2881, in <module>
parse_requirements(__requires__), Environment()
File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 596, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: virtualenv==1.8.2
I haven't used python in a while so i'm not sure what the issue is:
virtualenv --version
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 5, in <module>
from pkg_resources import load_entry_point
File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2881, in <module>
parse_requirements(__requires__), Environment()
File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 596, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: virtualenv==1.8.2
You're using mkvirtualenv which is a virtualenvwrapper command. You probably do not have virtualenvwrapper installed.
Type virtualenvwrapper in your command line and observe the input. If you get something like command not found, then you should install it first.
You should install it with pip like so pip install virtualenvwrapper.
There are some post-installation steps, the most important one being source /usr/local/bin/virtualenvwrapper.sh that makes commands like mkvirtualenv, rmvirtualenv available.
See virtualenvwrapper's documentation.
Alternatively, you can also just make use of virtualenv. Check that you have it installed like so
virtualenv --version
If you do not, you can install it with pip. Once you have it installed, you can create your virtualenv like so
virtualenv <name> in the directory of your choice.
I created a virtualenv with python2.7 a few weeks ago and got this project running fine. It uses setuptools and has a few console scripts defined.
Now today I installed python3 and pip3, which I didn't think would conflict with this project but I'm running into this error when I try to run the same console script.
pkg_resources.DistributionNotFound: myproject==1.0.0.0-local
Traceback (most recent call last):
File "/Users/me/dev/myproject/venv-myproject/bin/run_queue_worker_manager", line 5, in <module>
from pkg_resources import load_entry_point
File "/Users/me/dev/project/venv-myproject/lib/python2.7/site-packages/pkg_resources.py", line 2720, in <module>
parse_requirements(__requires__), Environment()
File "/Users/me/dev/project/venv-myproject/lib/python2.7/site-packages/pkg_resources.py", line 588, in resolve
raise DistributionNotFound(req)