How to import python files on Google app engine project? - python

I recently started playing with Google app engine (GAE) and I'm failing to do something very simple.
The GAE provides a main.py file which is where I'm trying to import a file I created util.py.
I saw some examples saying that you need to create a module for that in order to be able to include it.
So I did
main.py
utilities/
__init__.py
util.py
In main.py I imported import utilities.util, I still can't call the functions inside of util.py.
Any ideas where I could learn more about these things?

import utilities.util as myutil
myutil.function_name() #Using the module name you can access the functions
from utilities.util import function_name()
function_name()
Ref: https://docs.python.org/3/tutorial/modules.html#modules

Related

Unable to import module when developing a python package

My project structure is:
APP(dir)
app(dir)
model(dir)
metric_data.py
__init__.py
app.py
setup.py
When I'm running app.py with import statement of
from model.metric_data import MetricData
MetricData is a class, I'm able to successfully run the application retrieving data from metric_data.py file. But when I build it as a package and then try importing package app
from model.metric_data import MetricData
this statement is failing. Can anyone help me with the issue
here, I looked on the relative import part and tried but it didn't work.
Try to create a __init__.py file in the model directory.

How to import app file into test file in different directory Python

apologies if I'm going about this wrong, but I am quite new to python and can't quite figure out what the problem is! I have a simple Flask app that I'm trying to write tests using pytest for. The file structure is like this:
│── app.py
|── tests
│ ├── tests_app.py
I originally had app.py and tests_app.py in the same level and it all worked fine with tests passing, but now I have put tests in their own folder I can no longer import app without error.
I have tried the following in tests_app.py:
from app import app #this is what worked fine when app and tests were in the same folder
from ..app import app
from .. import app
and the linting error is 'Attempted relative import beyond top-level package' and when I run pytest it says 'ImportError: attempted relative import with no known parent package'.
Many thanks in advance!
p.s. I am using Python 3.8.5
You need to have app on the module search path. A quick fix would be to add the path to app.py to the PYTHONPATH environment variable:
export PYTHONPATH=/path/to/dir # where app.py is
If app.py is on the python path, then import app will work (barring any other import problems such as circular imports).
An effective and reproducible way to do this is to install your application. You can do this in develop mode, which means the code runs from its current directory. This is an understated, important part of developing a Python application, but to do it you will need to write code to package your application.

Importing file from same directory working in one python file but not another?

I have these two python files.
api.py has a class called API, which is imported by __init__.py using from api import API. Before this file was named __init__.py, this worked perfectly. But when I changed the name to __init__.py so that I could use it with Flask, I now get the error Unable to import 'api' under the from keyword. Doing from directoryName.api import API works, but the directory name is different on my computer and on my VPS. I could just change the name of the directory to make them the same, but I'd prefer if there was a way that I could change the directory name at any point and it wouldn't break the script.
Does anyone know, firstly, why this doesn't work in __init__.py? And secondly, a way to import a file from the same directory in the init file?
Thanks!

How does a Python module that contains class of same name work when imported?

I have recently been using web.py for some simple web applications in Python. I thought I understood how import statements, packages and modules worked but now I am a little confused.
According to web.py's API it says that the class application is located inside the module web.application. In order to use this class, the tutorial gives examples such as
import web
app = web.application(urls, globals())
What confuses me is how I am creating an instance of the application class using web.application. If there exists a class application that is inside a module named application within the web package, from what I have learned I would expect to have to do something as such :
web.application.application(urls, globals())
package -> module -> class
Could someone please clear up my confusion? Here is a link to the web.py API I am referencing http://webpy.org/docs/0.3/api#web.application
Thanks in advance for the help!
The attributes of application.py module have been imported into the __init__.py of the web package (via __all__).
In the __init__.py of the web package, you have:
from application import * # referring to application.py
And in application.py:
__all__ = [
"application", "auto_application",
"subdir_application", "subdomain_application",
"loadhook", "unloadhook",
"autodelegate"
]
Therefore, the attributes specified in the __all__ found in application.py are now accessible directly from the top level import -- web.
You can read more about packages, __init__.py and __all__

How to import python script files in folders on Google App Engine?

I'm fairly new to both Python and Google App Engine. I want to organize my script files by creating a folder structure. However when I do that I can no longer figure out how to import them.
For example:
main.py
/eggs/spam.py
How do I import spam.py in main.py?
Make eggs a package by adding an __init__.py file in the folder. It can be empty, as long as it's there. Then, import spam like this:
import eggs.spam
More information on packages.

Categories