I have the current directory structure:
- api
|
- app.py
|
- tests
|
- test_app.py
What is the correct way to import app.py into test_app.py?
Related
I have a pretty standart Django project and i can't find a way to import /middleware/utils/compare.py from /middleware/utils/request.py
This is the proyect tree:
|--middleware/
| |--utils/
| | |--compare.py
| | |--request.py
| |--__init__.py
| |--asgi.py
| |--settings.py
| |--urls.py
| |--views.py
| |--wsgi.py
|--manage.py
Where __init__.py, asgi.py, settings.py, urls.py, wsgi.py have no major modifications. (__init__.py is empty)
# middleware/views.py
from .utils.request import requests # This line works fine
# middleware/utils/request.py
from compare import compare # ModuleNotFoundError: No module named 'compare'
from .compare import compare # Attempted relative import beyond top-level package pylint(relative-beyond-top-level)
# ^^^ This line absolutly breaks my code, but it runs
from utils.compare import compare # ModuleNotFoundError: No module named 'utils'
from .utils.compare import compare # ModuleNotFoundError: No module named 'middleware.utils.utils'
Note: compare.py has a function named compare, i also tried renaming it but had the same problems.
This might be obvious, but i run the proyect with python manage.py runserver
Simply add empty __init__.py in utils folder.
And read more about it here:
https://docs.python.org/3/tutorial/modules.html#packages
Change your file tree like this,
|--middleware/
| |--utils/
| |--|__init__.py # Added this file
| | |--compare.py
| | |--request.py
| |--__init__.py
| |--asgi.py
| |--settings.py
| |--urls.py
| |--views.py
| |--wsgi.py
|--manage.py
And import like this,
from utils.compare import compare
am a beginner in python and wants to test some code in webjob and function app. usually i write code using c# so in visual studio we have templates to create webjob/function app so that we get all required files and init code. Now using python i need the required file structure and init code.
The folder/file structure for a Python Functions project looks like:
<project_root>/
| - .venv/
| - .vscode/
| - my_first_function/
| | - __init__.py
| | - function.json
| | - example.py
| - my_second_function/
| | - __init__.py
| | - function.json
| - shared_code/
| | - __init__.py
| | - my_first_helper_function.py
| | - my_second_helper_function.py
| - tests/
| | - test_my_second_function.py
| - .funcignore
| - host.json
| - local.settings.json
| - requirements.txt
| - Dockerfile
init method for webjob/function using python
init__.py
import azure.functions as func
import logging
def main(req: func.HttpRequest,
obj: func.InputStream):
logging.info(f'Python HTTP triggered function processed: {obj.read()}')
Please follow developer guide of Azure functions using python and Python Azure Functions using VS Code.
My current project structure is
package
|
- __init__.py
|
- minipackage1
|
- __init__.py
- file1.py
- file2.py
- minipackage2
|
- __init__.py
- file3.py
- file4.py
extrenal init looks like
__all__ = ["minipackage1", "minipackage2"]
and others like:
__all__ = ["file1", "file2"]
and
__all__ = ["file3", "file4]
But on running my script it says:
AttributeError: module 'package' has no attribute 'minipackage1'
So, where am I wrong and how to fix this?
I've been struggling with imports in my package for the last hour.
I've got a directory structure like so:
main_package
|
| __init__.py
| folder_1
| | __init__.py
| | folder_2
| | | __init__.py
| | | script_a.py
| | | script_b.py
|
| folder_3
| | __init__.py
| | script_c.py
I want to access code in script_b.py as well as code from script_c.py from script_a.py. How can I do this?
If I put a simple import script_b inside script_a.py, when I run
from main_package.folder_1.folder_2 import script_b
I am met with an
ImportError: no module named "script_b"
For accessing script_c.py, I have no clue. I wasn't able to find any information about accessing files two levels up, but I know I can import files one level up with
from .. import some_module
How can I access both these files from script_a.py?
To access script_c and script_b from script_a, you would use:
from ...folder_3 import script_c
from . import script_b
Or if you use python3, you can import script_b from script_a by just using:
import script_b
However, you should probably use absolute imports:
from mypackage.folder_3 import script_c
from mypackage.folder1.folder2 import script_b
Also see: Absolute vs Relative imports
Let us say that we have the following directory structure ...
+-- main.py
|
+--+ ./web
| |
| +--- ./web/bottleApp.py
Currently, I want to organize the files so that the I can separate different functionality in different areas. Template main.py and ./web/bottleApp.py look like the following ...
This is the ./web/bottleApp.py file:
import bottle
app = bottle.Bottle()
#app.route('/')
def root():
return 'This is the root application'
# some additional functions here ...
And this is the main.py file ...
from web import bottleApp as app
with app.app as report:
# Some random routes here ...
report.run(host = 'localhost', port=8080)
Now I want to add another folder which can handle some functions which I may optionally use is a bunch of my projects, (for example configuration file handling via the web interface just created)
Let us say we want to insert the following folder/file configuration ...
+-- main.py
|
+--+ ./web
| |
| +--- ./web/bottleApp.py
|
+--+ ./configure
|
+--- ./configure/config.py
Given the original app = bottle.Bottle() I want to create the following sample route in the file ./configure/config.py:
#app.route('/config/config1')
def config1():
return 'some config data'
How do I even go about doing this? Once I run the main.py file, how do I make sure that the other routes are available?
Bottle can run multiple bottle apps as a single instance.
You can use something like this on main.py
import bottle
from web.bottleApp import app
from configure.config import configure_app
main = bottle.Bottle()
main.mount("/config/",configure)
main.mount("/",app)
main.run(host = 'localhost', port=8080)
and on configure/config.py something like this:
import bottle
config_app = bottle.Bottle()
#config_app.route('/config1')
def config1():
return 'some config data'