webapp2 - how do I include library - python-2.7

I am testing Google Cloud and first I wanted to develop something on my PC before I use in on google cloud.
I am using APACHE and configured it in that way, that when I am going to the page localhost/wsgi_app I see my page which physically is stored in folder /svc/http/webapp2. File wsgi_app.py which contains my app is stored in subfolder webapp2. All works fine. I provide this information just in case it might play any role.
The issue I have is with import from library.
I did it on Django and now try to move it to webapp2.
The first lines of my program look like this:
import webapp2
import MySQLdb
import json
I have file called test.py which contains some classess and funtions.
it is in the same folder as wsgi_app.py.
I want to include it, however this seems not to work:
import webapp2
import MySQLdb
import json
from test import *
my test.py contains definition of the class 'Quote', but when I call the page I see error
NameError: global name 'Quote' is not defined
When I put the inside of the file test.py in the file wsgi_app.py all works fine.
My goal is to separate the code into several files.
Any idea why :
from test import *
does not work ?
It worked on any other program I wrote, so why not here?

issue solved.
issue is not related to weapp2 but to WSGI and the path where python search for files.
Most simple solution is to add something like this ;
execfile("/srv/http/test.py")
it will import the file.
other than this here is quite good article
http://blog.dscpl.com.au/2014/09/python-module-search-path-and-modwsgi.html

Related

ModuleNotFoundError: No module named 'config.wsgi'

I'm trying to run a .py file and in the file I have this import
from config.wsgi import *
import os
from django.template.loader import get_template
from weasyprint import HTML, CSS
from config import settings
The whole project works, if I set runserver, the project starts without any problem, but this file does not work. The structure of the project is as follows
NombreDelProyecto
--app
---config
----__init__.py
----asgi.py
----settings.py
----wsgy.py
----db.py
---core
----general
----login
----user
----archivodetest.py
the case as I say the project works, but in the views of the applications that I have been doing to put imports I get in red underlined but as I say it works for example:
from core.general.forms import ActividadForm
That comes out in red, if I put in front of the core, app.core as follows
from app.core.general.forms import ActividadForm
it does not show red but the project does not work and I get the following error
from app.core.general.forms import ActividadForm
ModuleNotFoundError: No module named 'app'
I understand that it is the routes or something I did wrong from the beginning, please could someone help me.
Thank you very much.
I tried adding the route, changing the app's route in settings, but to no avail.
You've named the file wsgy.py but it needs to be wsgi.py.
Rename your file in config and retry.
To your question, I think its because you're missing the __init__.py file in the general app.
If you haven't already go one, you'll likely need to have add the same again in your core app too.
You probably manually created all of these files and structures I suspect, and if that's the case, please take a look at the documentation regarding creating new apps inside a django project.
If you go a bit further up the page, it will also tell you how to create the initial django project structure with a command.
Thank you very much for the answer, I managed to solve it after a lot of testing.
There are two ways, open the project again from the app folder (I had it open in the ProjectName folder).
Or as a second option in pyCharm on the left where the project folders are, I went to the app folder (which is the root) and right clicked and in the menu, Mark Directorie as - Sources root. Then my problem is fixed.
I had all the arcvhiso init.py, and where I put the wrong name wsgi.py is that I wrote it wrong here but in the project it was right.
Thank you very much for the help.

How To Change A Third Party App's Models.py Due To Invalid Import

I'm trying to add django-messages to my app but I'm getting this error when I add it to my installed apps
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\encoding.py)
I've looked up this error and apparently it's caused by this line in Django-Messages models.py
from django.utils.encoding import python_2_unicode_compatible
To fix it I should change it to
from django.utils.six import python_2_unicode_compatible
As this is a third party app, how should I change this line?
Should I copy the file structure into my own project (creating a django-messages folder among my apps then a models.py file inside it) and copy/paste the entire models.py into there and then change the line? This seems wrong, but I don't know how else to fix it.
Or does the fact that it's using an outdated import signify that the app isn't maintained and therefore shouldn't be used at all?
Thank you.
I can suggest two options. First, you can clone it on GitHub (i.e. download all the files) and then paste this into your project folder (along with your other apps). And then you can make any changes you wish. Of course it remains to be seen whether it will work then, as there might be other issues. It seems to be compatible with Django 1.11 - 2.2. But I would think that it's a better option than doing this all yourself. Unless you can find a different app that suits your needs.
Second, assuming you are using a virtual environment, you can navigate to where all your packages are installed. I'm not sure where this will be located for you, but it should be something like envs\<name of env>\Lib\site-packages. And there you can make any changes you wish - just search for the relevant file.
Hope this helps.

Custom import package Python

I am making a import package and I keep running into a problem when I try and import and use the package. Primarily when I try to run it.
import mypackage
once I import it I can use it I dont get any errors upon importing it, however when I go to use it I get an error that says
TypeError: 'module' object is not callable
In a folder that I have named myfolder which is located in my current working directory i have a empty init.py file as well as two other .py files which contain the functions i have one called test.py and another called testing.py.... my problem isnt importing them but when I go to use them I get the TypeError.
the folders structure is as follows,
current working directory
-Mypackage
-__init__.py
-test.py
-testing.py
I import it like this,
from Mypackage import test
I use it like this,
test(stuff)
You have to learn how to use modules in python. When you import mypackage into your script it imports a module which has two scripts in it. Let's say they are first.py and second.py . Now if you use something like
from mypackage import first
And then you use a function named func like this
first.func()
Then it should work.
In short either import all the functions from your module or use dot notation to reference these functions. More on module can be read here

PyCharm/Django - cannot import module views.py (IDE is wrong)

I'm having a weird problem with import views in Django project. I'm not sure whether it is a problem caused by PyCharm of Django. So PyCharm says that it can't import views.py file, but it works when I run the server.
Here is the picture:
Do you know where could be the problem?
EDIT:
According to Inlangers answer, I've tried to change import to from vlado_web.translations import views which did not helped, moreover, it raises
Exception Value:
No module named translations
When I have from translations import views there, it works correctly but PyCharm says that it can't be resolved.
PyCharm doesn't know where your source files are. Try this:
Right click on folder vlado_web (the folder that contains manage.py) within PyCharm. Go to Mark Directory As and choose Sources Root.
This will let PyCharm know that the vlado_web directory is the root folder for your source code, and will allow you to perform absolute imports from there, e.g.
from vlado_web.translations import views
Try from vlado_web.translations import views

What is the minimal ammount of code I need for using django-cms-search 0.2.6?

I want to use django-cms-search in my project and have installed it together with xapian and the xapian_backend. In my settings file, I have defined HAYSTACK_SEARCH_ENGINE, HAYSTACK_SITECONF and HAYSTACK_XAPIAN_PATH (because the django system is a bit old). Furthermore, I've included haystack.urls and created a corresponding template.
The index is not created when I run manage.py update_index.
Is it necessary to put any code into code into HAYSTACK_SITECONF?
The docs suggest that I sould create a models.py file in the directory defined in HAYSTACK_SITECONF and put this code into it:
from cms_search.cms_app import HaystackSearchApphook
from cms.apphook_pool import apphook_pool
apphook_pool.register(HaystackSearchApphook)
But this has no effect.