Google App Engine - Importing my own source modules (multiple files) - python

I am writing a GAE application and am having some difficulty with the following problem.
I've created multiple python files (say a.py and b.py) which are both stored in the same folder. I am able to call code in a.py or b.py by mapping URL's to them (using app.yaml). What I haven't figured out how to do is import the code from one into another.
Can anyone help me with the syntax and/or any config that is required here? For instance, I am under the impression that I can include the code from b.py in the file a.py by issuing the following statement in a.py
import b
I'm not having any success with this approach. Specifically I receive this error:
ImportError: No module named b
Any suggestions?
Thanks,
Matt

Have you tried importing as if you were starting at the top level? Like
import modules.b

If the files a.py and b.py aren't located, be sure to include the respective paths in sys.path.
import sys
sys.path.append(r"/parent/of/module/b")

Note that the usual pattern with GAE is not to have each one independently mapped in app.yaml, but rather to have a single 'handler' script that has all (or all but static and special) URLs mapped to it, and have that script import both a and b and use Handlers they define.

as #toby said, it must be imported as if importing from the top directory, and a file named init.py must be placed in the folder.

Related

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!

Python finding some, not all custom packages

I have a project with the following file structure:
root/
run.py
bot/
__init__.py
my_discord_bot.py
dice/
__init__.py
dice.py
# dice files
help/
__init__.py
help.py
# help files
parser/
__init__.py
parser.py
# other parser files
The program is run from within the root directory by calling python run.py. run.py imports bot.my_discord_bot and then makes use of a class defined there.
The file bot/my_discord_bot.py has the following import statements:
import dice.dice as d
import help.help as h
import parser.parser as p
On Linux, all three import statements execute correctly. On Windows, the first two seem to execute fine, but then on the third I'm told:
ImportError: No module named 'parser.parser'; 'parser' is not a package
Why does it break on the third import statement, and why does it only break on Windows?
Edit: clarifies how the program is run
Make sure that your parser is not shadowing a built-in or third-party package/module/library.
I am not 100% sure about the specifics of how this name conflict would be resolved, but it seems like you can potentially a). have your module overridden by the existing module (which seems like it might be happening in your Windows case), or b). override the existing module, which could cause bugs down the road. It seems like b is what commonly trips people up.
If you think this might be happening with one of your modules (which seems fairly likely with a name like parser), try renaming your module.
See this very nice article for more details and more common Python "import traps".
Put run.py outside root folder, so you'll have run.py next to root folder, then create __init__.py inside root folder, and change imports to:
import root.parser.parser as p
Or just rename your parser module.
Anyway you should be careful with naming, because you can simply mess your own stuff someday.

Python No module named

I have a custom module that I am trying to read from a folder under a hierarchy:
> project-source
/tests
/provider
my_provider.py
settings_mock.py
__init__.py
I am trying to call, from my_provider.py
import tests.settings_mock as settings
Example from command line:
project-source> python tests/provider/my_provider.py
Error:
... ImportError: No module named settings_mock
I keep getting No module named settings_mock as error. I have already exported project_source path to PYTHONPATH. I have made tests into a package by creating a __init__.py file in its root, but no change in the error then.
I can print the settings_mock.py attributes when cd'ing project source
>>> import tests.settings_mock as settings
>>> print settings.storage_provider
correct storage provider value
Is anyone able to point out my mistake here? Thanks!
You only have one small mistake. To use subfolders, you need __init__.py, not init.py as you stated in the question. The difference is that __init__ is a builtin function of python, whereas init is not. Having this file in each subfolder tells the pyhon interpreter that the folder is a "package" that needs to be initialized.
UPDATED: It should be noted that python usually runs from the current directory that the script is located. If your executable main script is my_provider.py, then it's not going to know what to import, since the main script is located in a lower directory than the object it is trying to import. Think of it as a hierarchy. Scripts can only import things that are beneath them. Try separating out the executable from everything else in that file, if there are things that settings_mock needs to import.

Better approach to use script inside nested directory PYTHONPATH

Sorry for asking my own question 2nd time, but i am totally stuck in import file in python.
I have a directory structure below:
|--test/foo.py
|--library #This is my PYTHONPATH
|--|--script1.py
|--|--library_1
|--|--|--script2.py
|--|--library_2
|--|--library_3
I am accessing library/library_1/script2.py from test/foo.py.
Here i am confused about what is the better approach. Generally all library folders or utility functions should be added to pythonpath.
This is a folder structure i am maintaining to differentiate utility functions and test scripts.
I tried putting __init__.py in library and library1 & then imported like from library1 import script2, but getting error as No module named script.
I have tried appending that path to system path as well.
Working: if i add another pythonpath like path/to/library/libray_1/. So should i do this for all folders which are inside library folder to make it work ?
Here's what you need to do:
|--test/foo.py
|--library #This is my PYTHONPATH
|--__init__.py
|--|--script1.py
|--|--library_1
|--|--|--__init__.py
|--|--|--script2.py
|--|--library_2
|--|--|--__init__.py
|--|--library_3
|--|--|--__init__.py
And inside the first __init__.py below library you need to do:
import library1
import library2
import script
Then, if library is your python path, you can do this within test/foo.py with no errors:
import library
library.library1.bar()
library.script.foo()

When do I need to modify a Python __init__ file?

Suppose that you create a Python package called app, and it contains a module called foo, which has a function called say_hello that prints out Hello! to the console.
Suppose that you also create a second Python package called boss_app that has a module main. The app and boss_app are in the same directory, and the directory is on the Python path. Also, the __init__ files are all blank. The structure is:
app
__init__.py
foo.py
boss_app
__init__.py
main.py
I want to import app into boss_app.main so that I can call app.foo.say_hello.
I use this command:
import app
app.foo.say_hello()
and I expect to see in the console:
>>>> Hello!
Instead, the behavior I get is that app is imported but it does not have access to foo.
The solution I came up with was to modify app.__init__ so that it contained the following command:
from .foo import *
Now I get the expected behavior.
Is it always necessary to make a custom __init__ for a package if that package is going to be imported from an outside package?
Saying
import app
only runs app/__init__.py and makes everything it initializes available to be used as app.SOMENAME. If you want app.foo module to be available, you need to say import app.foo. This will load the module. A common example of this distinction is probably import os vs import os.path. Just because you say import os, you won't have the contents of os.path package available.
You don't have to break modules into packages, but it helps to avoid module-name collision. It may also help to keep concepts clear in the mind of a user of these modules.

Categories