I'm having trouble in deploying my django-react app.
This is my directory tree.
.
├── .elasticbeanstalk
└── backend
├── .ebextensions
├── auth
│ ├── management
│ │ └── commands
│ └── migrations
├── client
│ └── build
│ └── static
│ ├── css
│ ├── js
│ └── media
├── config
│ └── settings
├── postings
│ ├── management
│ │ └── commands
│ └── migrations
└── uploads
# /.ebextensions/django.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: config/wsgi.py
NumProcesses: 3
NumThreads: 20
# /backend/config/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.deploy")
application = get_wsgi_application()
And application enviroment on aws website, WSGIPath: config/wsgi.py, NumProcesses: 3, NumThreads: 20
I can't find what should I fix.
Related
I have this structure for my project:
├── Dockerfile
├── app
│ ├── __init__.py
│ ├── __pycache__
│ ├── config
│ ├── database
│ ├── logging.py
│ ├── main.py
│ ├── routers
│ ├── services
│ ├── static
│ ├── templates
│ ├── utils
│ └── worker
├── k6.js
├── poetry.lock
├── prestart.sh
├── pyproject.toml
├── pytest.ini
└── run.py
Inside app, I have this worker folder that I also open as a kind of separate project.
├── __init__.py
├── database
│ ├── __init__.py
│ └── conn.py
├── engine
│ ├── __init__.py
│ ├── core
│ ├── data
│ ├── main.py
│ └── utils
├── main.py
├── poetry.lock
├── pyproject.toml
└── run.sh
The issue that I have when I open worker project which uses code from upper directory, pylance gives me an error of an import that could not be resolved. However, this code runs fine and perfect.
I created .vscode/settings.json for the worker project and add these options:
"python.analysis.extraPaths": ["../../app"],
"python.autoComplete.extraPaths": ["../../app"]
But I am still getting these errors! How can I fix this?
These paths fixed my issue:
"python.analysis.extraPaths": ["${workspaceFolder}\\..\\.."],
"python.autoComplete.extraPaths": ["${workspaceFolder}\\..\\.."]
Im writing a library for internal use,its called "etllib", and I have the following structure:
etl-lib
├── README.md
├── etllib
│ ├── __init__.py
│ ├── client
│ │ ├── __init__.py
│ │ ├── elastic.py
│ │ └── qradar.py
│ ├── etl
│ │ ├── __init__.py
│ │ └── etl_imperva.py
│ └── util
│ ├── __init__.py
│ ├── config.py
│ ├── daemon.py
│ ├── elastic
│ │ ├── __init__.py
│ │ └── impeva_index_config.py
│ └── imperva
│ ├── __init__.py
│ ├── kpe_config.py
│ └── query_config.py
├── scripts
│ └── etl_imperva
└── setup.py
And I have a script called "etl_imperva" in etllib/scripts. The code inside looks like this :
#!/usr/bin/python3
import sys
from etllib.etl.etl_imperva import ETL
# Run with python3 imperva_run.py start|run|stop|restart
ETL.startup(sys.argv)
If I install this package(etllib) and call this script, it works just fine. But when I need to test stuff, how can I tell python to use the modules that are on my working directory instead the ones are installed? Because each time I make a change on the modules, I need to reinstall the package and this is a little time consuming.
I also tried uninstalling the package for testing, but whe I run this script I get the following error :
Exception has occurred: ModuleNotFoundError
No module named 'etllib'
File "/home/jleonse/etl-lib/scripts/run_imperva", line 3, in <module>
from etllib.etl.etl_imperva import ETL
Is there a better way to do this?
Actually, it is not on the same level in the hierarchy.
from etllib.etl.etl_imperva import ETL
would work only if etllib was in the same directory or in a directory in your system PATH, but the etllib is in the parent directory, hence it can not find it.
so you can make it work if you change the project structure to be:
etl-lib
├── README.md
├── etllib
│ ├── __init__.py
│ ├── client
│ │ ├── __init__.py
│ │ ├── elastic.py
│ │ └── qradar.py
│ ├── etl
│ │ ├── __init__.py
│ │ └── etl_imperva.py
│ └── util
│ ├── __init__.py
│ ├── config.py
│ ├── daemon.py
│ ├── elastic
│ │ ├── __init__.py
│ │ └── impeva_index_config.py
│ └── imperva
│ ├── __init__.py
│ ├── kpe_config.py
│ └── query_config.py
├── etl_imperva
│
└── setup.py
I have a directory that looks like this:
├── libraryweb
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── forms.cpython-37.pyc
│ │ ├── models.cpython-37.pyc
│ │ └── routes.cpython-37.pyc
│ ├── forms.py
│ ├── models.py
│ ├── routes.py
│ ├── save_books.py
│ ├── site.db
│ ├── static
│ │ ├── book_pics
│ │ └── user_pics
│ │ └── default.jpg
│ └── templates
│ ├── account.html
│ ├── base.html
│ ├── book.html
│ ├── book_update.html
│ ├── home.html
│ ├── login.html
│ └── register.html
└── run.py
Now in the file save_books.py, I want to import a class Book from models.py in the libraryweb module. I have tried from libraryweb.models import Book, from .models import Book, and from models import Book, but none of them works.
The __init__.py file contains the following code:
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view='login'
login_manager.login_message_category='info'
from libraryweb import routes
I got the following error message:
Exception has occurred: ModuleNotFoundError
No module named 'libraryweb'
File "/Users/gracezhou/cs/python_flask/flask-library/hey/libraryweb/save_books.py", line 3, in <module>
from libraryweb.models import Book
Have you set your PYTHONPATH variable pointing to your application path ?
Can you please show your ouput of below command.
import sys
sys.path
All directories where module files are exist needed to be in PYTHONPATH env variable. If you dump everything in the single directory then you just need folder which contains python files into your PYTHONPATH. Refer to below https://docs.python.org/3/library/sys.html#sys.path
I'm trying to run django unittest using VSCode, not terminal.
my project tree looks like this:
├── db.sqlite3
├── hero
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── admin.cpython-37.pyc
│ │ ├── apps.cpython-37.pyc
│ │ ├── models.cpython-37.pyc
│ │ ├── tests.cpython-37.pyc
│ │ ├── urls.cpython-37.pyc
│ │ └── views.cpython-37.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0002_hero_age.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-37.pyc
│ │ ├── 0002_hero_age.cpython-37.pyc
│ │ └── __init__.cpython-37.pyc
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── manage.py
└── toh
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── settings.cpython-37.pyc
│ ├── urls.cpython-37.pyc
│ └── wsgi.cpython-37.pyc
├── settings.py
├── urls.py
└── wsgi.py
I made tests.py file inside hero directory.
My tests.py code looks like this:
from django.test import TestCase, Client
from .models import Hero
# Create your tests here.
class HeroTestCase(TestCase):
def setUp(self):
Hero.objects.create(name='Superman', age=10)
Hero.objects.create(name='Batman', age=1)
Hero.objects.create(name='Ironman', age=30)
def test_hero_count(self):
self.assertEqual(Hero.objects.all().count(), 3)
def test_hero_id(self):
client=Client()
response=client.get('/hero/1/')
self.assertEqual(response.status_code, 200)
self.assertIn('1', response.content.decode())
def test_hero_visit_count(self):
client = Client()
response = client.get('/hero/hello')
self.assertEqual(response.status_code, 200)
self.assertIn('1', response.content.decode())
response = client.get('/hero/hello')
self.assertEqual(response.status_code, 200)
self.assertIn('2', response.content.decode())
And my .vscode/settings.json looks like this:
{
"python.pythonPath": "/anaconda3/bin/python",
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.testing.unittestArgs": [
"-v",
"-s",
"./hero",
"-p",
"*test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
But when I ran test by VSCode this error keeps coming out.
======================================================================
ERROR: tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/anaconda3/lib/python3.7/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "toh/hero/tests.py", line 2, in <module>
from .models import Hero
ImportError: attempted relative import with no known parent package
Or:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
So I checked whether there are something wrong with my test code, but my test code pass when I ran python manage.py test ! How do I resolve this problem?
The relative import problem is because you set -p to hero which changes the top-level directory to that and so it no longer looks like a package to Python.
The configuration problem is because unittest isn't running manage.py. You can go to https://github.com/microsoft/vscode-python/issues/73 and 👍 the issue to vote for it to be prioritized.
This is my structure,I have a django project named kkpro
in kkpro,I have a django app called myapp .
And a scrapy project called project1 inder the kkpro/scrapy/music
├── kkpro
├── manage.py
├── savepage.py
├── myapp
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── kkpro
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
│── ── scrapy
│ ├──music
│ ├──project1
│ ├── scrapy.cfg
│ ├──project1
│ ├── __init__.py
│ ├── items.py
│ ├── pipelines.py
│ ├── settings.py
this is my project1/settings.py
import sys, os
path_django_site = os.path.join(os.path.dirname(__file__), "../../../../")
sys.path.append(path_django_site)
os.environ['DJANGO_SETTINGS_MODULE'] = 'kkpro.settings'
import save_page
in my computer it works well to used kkpro setting,
but when I deploy to aws eb , import save_page has error
I edit to from kali import save_page. still has error ImportError: cannot import name save_page
What should I do ?
Please help me,thank you very much