I have an api with a structure like this
my-api
├── Dockerfile
├── __init__.py
├── app
│ ├── __init__.py
│ ├── api
│ │ ├── __init__.py
│ │ └── mymodule.py
│ ├── config.py
│ ├── main.py
│ └── requirements.txt
└── docker-compose.yml
I'm trying to import mymodule.py into main.py per the docs but when I do so get the error
File "./app/main.py", line 3
from my-api.app.api import allocate
^
SyntaxError: invalid syntax
My import looks like this
from my-api.app.api import mymodule
and my Dockerfile CMD is
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7833"]
Is there something I'm doing wrong here?
Related
I am not able to import certain files/classes
My directory structure is below:
Main Project Directory
├── config
│ ├── config.py
│ ├── constants.py
│ └── __init__.py
└── utils
├── __init__.py
├── logger_config.py
├── sql_helper.py
├── update_data_on_request.py
└── utility_functions.py
I want to import config.py from config into utility_functions.
Currently init.py is blank.
Error I face was ModuleNotFoundError: No module named 'config'
Try to use from config.config import * in utility_functions.py
I am trying to test Python modules. Currently, the file structure is like the following displayed:
project
├── __init__.py
├── __pycache__
│ └── __init__.cpython-37.pyc
├── p.py
├── package1
│ ├── __init__.py
│ ├── __pycache__
│ ├── module1.py
│ └── module2.py
└── package2
├── __init__.py
├── __pycache__
└── module3.py
In the module3.py, I would like to import the functions defined in module2.py. I believe I just need to use from package1.module2 import function_name. However, that doesn't work and the error is ModuleNotFoundError: No module named 'package1'.
BTW, I am using Python3.6 for the testing.
You need to add the directory path to your sys.path or PYTHONPATH, the following code would work.
In your module3.py, add this:
import os, sys
ab_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../package1'))
sys.path.append(ab_path)
Then you can do from module2 import function_name after it.
I have following project structure,
|
├── database
│ ├── db_adapter.py
│ ├── db_layer
│ │ ├── __init__.py
│ │ └── mysql_adapter.py
│ ├── __init__.py
│ └── scripts
│ └── schema.sql
|
└── workflow
├── dags
│ ├── dss_controller.py
│ ├── __init__.py
|
├── __init__.py
├── plugins
I want to import db_adapter.py module inside dss_controller module, when I tried do it following error shown,
ModuleNotFoundError: No module named 'database'
How can I do the correct importing of the modules?
I am currently working on a flask_app. This is my project structure
├── pypi_org
│ ├── __pycache__
│ │ └── app.cpython-37.pyc
│ ├── app.py
│ ├── services
│ │ └── package_services.py
│ ├── static
│ │ ├── css
│ │ │ └── site.css
│ │ ├── html
│ │ └── js
│ ├── templates
│ │ ├── home
│ │ │ ├── about.html
│ │ │ └── index.html
│ │ ├── packages
│ │ │ └── details.html
│ │ └── shared
│ │ └── _layout.html
│ ├── tests
│ ├── viewmodels
│ └── views
│ ├── home_views.py
│ └── package_views.py
├── requirements-dev.txt
└── requirements.text
I have defined the blueprint in home_views.py
from flask import render_template
from flask import Blueprint
from pypi_org.services import package_services
blueprint = Blueprint('home', __name__, template_folder='templates')
#blueprint.route('/')
def index():
test_packages = package_services.get_latest_packages()
return render_template('home/index.html', packages=test_packages)
#blueprint.route('/about')
def about():
return render_template('home/about.html')
Code for app.py is given below
from flask import Flask
from pypi_org.views import home_views, package_views
app = Flask(__name__)
def main():
register_blueprints()
app.run(debug=True)
def register_blueprints():
app.register_blueprint(home_views.blueprint)
app.register_blueprint(package_views.blueprint)
if __name__ == '__main__':
main()
When I run the app.py I get the 404 error.
I'm not sure what I am doing wrong. Everything looks right.
Can someone take a look?
I ran into the exact same issue within the same course.
If you right-click the app.py file and select run, Pycharm will create a run configuration, in which it will execute "app.run". It won't care about what you defined in your main(), and so the register_blueprints() will not be run!
The same run configuration will be created, if you create one yourself selecting Flask server. This would be again a bad idea.
To overcome this issue, create a run configuration where you select python. The same way as you would run any python script. This way Pycharm will execute "python app.py" (not "app.run") and it will run all lines in that file.
I'm trying to add pytest to a flask app. This is the project structure.
root
├── myapp
│ ├── app.py
│ ├── config.py
│ ├── module
│ │ ├── models.py
│ ├── pages
│ │ ├── forms.py
│ │ ├── templates
│ │ │ ├── base.html
│ │ │ ├── home.html
│ │ │ └── login.html
│ │ └── views.py
├── Pipfile
├── Pipfile.lock
├── __pycache__
└── tests
├── conftest.py
└── test_pages.py
The flask app is under myapp folder. The command flask run runs ok under this folder. So the app is working fine on its own.
The test folder is at the same level as myapp. If I run pytest or py.test I get this error.
conftest.py:3: in <module>
from app import app
E ModuleNotFoundError: No module named 'app'
conftest.py
from pytest import fixture
from app import app
#fixture
def client():
return app.app
I want a way to pytest to recognize myapp as the root of the project. Even if they are in different folders.