Error Importing Requests Module in Flask Backend - python

I am currently trying to integrate a flask backend to my react web-app. In my flask subdirectory, I have installed requests module as shown below but I keep running into an unresolved import error although I have installed the module. I have tried to follow different troubleshooting suggestions online but none seem to work. Would really appreciate if someone can help me out.

This looks like that the terminal and the IDE use two different environments.
You should either create a virtual env with PyCharm directly, or choose the existing one.
Please read the PyCharm documentation:
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
There, either choose "New environment" or "Existing environment".

Related

Pycharm with ec2 - no module named instrument event though I am adding the path?

I have an ec2 instance running. Just switched from Pycharm CE to Pycharm Professional. I have setup the remote interpreter. It is connected.
The following runs perfectly if I ssh to ec2 and run it using python3:
import os
import sys
sys.path.append('/home/xxx/yyy/userscripts')
from instrument.baking import baked
When I try to run the same script from Pycharm Professional using the remote interpreter, I get this error:
ModuleNotFoundError: No module named 'instrument.baking'
I have switched off Auto-uploading of files. As I use git for version control.
I saw a lot of similar questions asked and answered, tried the solutions such as marking the directory instrument as source. With no luck.
From what I understood Pycharms is connecting to the remote interpreter and will be using all the files/folders from the remote resource. I might be horribly wrong.
Would be great is someone can explain why exactly this is happening? And a way to resolve?
Edit: some of the packages can be imported.
When I try to import:
from instrument.cooking import coked
It works.
2 out of 5 packages fail.
Very confusing.

Python Package not found

I have created a virtual environment and installed all required packages and running the python code. all looks okay and not seen any issue. Later I have a requirement to connect to an api and get data. For this, I have installed requests==2.22.0 in the virtual environment and running the python code but end up with No module named "requests".
Could you please advise me what went wrong in this case to import the requests module into my code I appreciate you quick help.
Thanks,
Bhaskar.

PyCharm resolving - flask.ext.sqlalchemy vs flask_sqlalchemy

If I use the following format in my application, everything works, except PyCharms resolving / autocomplete feature:
from flask.ext.sqlalchemy import SQLAlchemy
If I use the following format in my application, everything works. But, alas, it is not the correct way to import the libraries:
from flask_sqlalchemy import SQLAlchemy
Is there any way to make PyCharm resolve the first syntax correctly?
The flask.ext namespace is a transistion namespace, see the Extension Import Transition section of the Flask Extension Development docs:
For a while we recommended using namespace packages for Flask extensions. This turned out to be problematic in practice because many different competing namespace package systems exist and pip would automatically switch between different systems and this caused a lot of problems for users.
and
Flask extensions should urge users to import from flask.ext.foo instead of flask_foo or flaskext_foo so that extensions can transition to the new package name without affecting users.
So to transition between versions, the flask.ext alias was added, which will automatically try to import flask_[name] packages when importing flask.ext.[name]. But that transition is now moot; you no longer will find packages that still rely solely on flask.ext.
As such, it is perfectly fine to use the actual module name and have PyCharm autocomplete the module contents.
You only really have to use flask.ext if you are still using an older version of the extension and need to be future compatible. That future is already here.
FYI. flask.ext is deprecated, and the right way is:
from flask_sqlalchemy import SQLAlchemy
In case anyone found this SO question on Google.
Use a virtualenv and set that virtualenv for your project in PyCharm. I had the same problem as you do and after setting the correct virtualenv (which contains the flask and flask_sqlalchemy extension) my problem is solved.
To set a virtualenv for your project in PyCharm (from JetBrains Web Help):
To add an existing virtual environment to the list of available
interpreters
In the Project Interpreter page of the project settings, click .
In the drop-down list, choose Add local.
In the Select Python Interpreter dialog box that opens,
choose the desired Python executable, and click OK.
Moreover try adding requirements.txt to the root of your project, afterward PyCharm will notify you to install missing dependencies which might help.

Python-Django: ImportError: no module named django

Im completely new to django and Ive been trying to set up an environment from here [https://docs.djangoproject.com/en/1.1/intro/tutorial01/]. Python was already setup on my mac
Python 2.7.2. I followed the instructions and setup django using
sudo port install py27-django
And also added /etc/paths.d/macports with
/opt/local/bin
/opt/local/sbin
/opt/local/lib/python2.7/site-packages/django/bin
But I still get the ImportError: No module named django message when I import django from the python shell.
I looked at this link
importerror: No module named django but it didnt really help much. Any help is appreciated
Have a look at this article... they are using virtualenv to set up the project and this fixed a lot of problems like this for me. With virtualenv, the system libraries and the libraries you need for your python project are separated, so your not mangling them together.
http://www.jeffknupp.com/blog/2012/02/09/starting-a-django-project-the-right-way/
If I may, it looks like the last path you added was pointing to a python2.4 directory, you might need to look for django in a python2.7 directory instead.

Dajaxice Autodiscover in Django App (cannot import name dajaxice_autodiscover)

I was following the directions at http://django-dajaxice.readthedocs.org/en/latest/installation.html to install Dajaxice for simple AJAX support and I ran into an error I can't quite figure out. The lines:
from dajaxice.core import dajaxice_autodiscover
#dajaxice_autodiscover()
in urls.py seem to be causing an error (cannot import name dajaxice_autodiscover). When I look at the module files for Dajaxice I don't see one for autodiscover either. If I leave out these lines, will I have any troubles using dajaxice? Thanks.
The import should be correct like that! dajaxice_autodiscover imported in __init.py__ of the core package, so it is supposed to work like that! More likely the whole app is probably installed in a wrong path, you could try to open a shell (python manage.py shell) and try import dajaxice and see what happens!
William What version of dajaxice do you have? the "registration approach" documented in github wiki work with dajaxice>=0.1.5
For older versions of dajaxice you should register your functions in settings.DAJAXICE_FUNCTIONS.
Anyway newest versions of dajaxice has less bugs.
It looks like the wiki installation instructions are ahead of the available downloads. If you pull the latest version of the code from git and install that then the instructions work. I'm not sure how to install versions 1.4 and below though.
I experienced the same problem and I found the solution.
It all about environment vairables in windows. You set following via CMD:
set DJANGO_SETTINGS_MODULE 'Your django project/your site/settings.py
Now it should work fine, Good Luck!

Categories