Test project with pytest in different folder - python-3.x

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.

Related

importing folder that shadows same name as third party dependency. What to do?

say I have a folder structure that looks like this:
.
├── CODEOWNERS
├── Makefile
├── README.rst
├── __pycache__
│   └── conftest.cpython-37-pytest-6.2.2.pyc
├── airflow
│   ├── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── dev
│   │   └── dags
│   └── <some_file_I_need>
how do I import the file I need from the airflow local package (not the third party dependency named airflow). I have a dependency called airflow unfortunately and that gets imported when I do: import airflow.dev... and that errors out.

Import a module from a subdirectory in FastAPI

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?

Importing python modules and packages

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?

Unable to solve Flask blueprint 404 error

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.

express serve index.html from create-react-app

for special Heroku purposes, I need to deploy the solution which would redirect all routes to create-react-app index.html.
$ tree -L 2 -I 'node_modules|temp|semantic|build'
.
├── package.json
├── package-lock.json
├── public
│   ├── favicon.ico
│   ├── index.html // all requests from "redirect" express app should point to this file
│   ├── libs
│   ├── manifest.json
│   └── semantic.css
├── README.md
├── redirect // express app
│   ├── app.js
│   ├── bin
│   ├── package.json
│   ├── public
│   ├── views
│   └── yarn.lock
├── semantic.json
├── src
│   ├── appConfig.js
│   ├── assets
│   ├── components
│   ├── index.js
│   ├── index.scss
│   ├── old
│   ├── pages
│   ├── serviceWorker.js
│   └── utils
├── yarn.lock
└── yarn-error.log
the redirect directory contains the express app where I need to redirect every request to index.html
everything is fine but in the index.html there is env variable %PUBLIC_URL% which would fail with error below when I run npm start from redirect folder
pandemic#bar ~/foo/redirect
$ npm start
> redirect#0.0.0 start <path>\redirect
> node ./bin/www
URIError: Failed to decode param '/%PUBLIC_URL%/libs/pdfmake/pdfmake.min.js'
How to resolve this issue?
note: I am using windows

Resources