Unable to load apache server while loading external modules in a class - python

This is my django project hierarchy
project/
apache/ django.wsgi
project/ __init__.py, settings.py, urls.py ..
pages/
__init__.py
widgets.py
website_views.py
services/
__init__.py
apis/
__init__.py
fparser.py
googleData.py
wp.py
...
wservice.py
...
So, the wservice.py is wrap-up like class which lies over all classes of apis module. It even provides some common functionality all classes that it inherit.
wservice.py
import feedparser
from bs4 import BeautifulSoup
class WidgetService(FParser):
def post_content_summary(self, post_number):
....
content_text = content_soup.get_text()
...
def get_random_image(self, post_number):
...
content_soup = BeautifulSoup(html_content)
...
FParser class is located at fparser.py
The methods in fparser.py use the above class in this way.
from services.wservice import WidgetService
def method1():
obj = WidgetService()
m1 = obj.foo1() # described in FParser class
m2 = obj.foo2() # described in WidgetService class
I am using this WidgetService() in pages/widgets.py. So, what I found is, when ever I start using BeautifulSoup, the apache server is not loading.. Its not even showing any syntax error.
I don't even see any error in log file.
What might have possibly went wrong??? The interesting part is, I haven't faced this kind of error in development server, heroku (gunicorn)

This may be the interaction between Cython and mod_wsgi described here, and explored in a Beautiful Soup context here. Here's an earlier question similar to yours.

Related

Python class with a decorator is being flagged by pylint as unused-import

For example, let's say I have three Python files; one file is the init.py which has routines for creating the namespaces of my Flask API and initializing the Flask app:
from flask import Flask
app = Flask(...)
... initialize the namespace etc. ...
... and another Python file with the definitions of the API Resource subclasses that exist in the enpoint.py file. It contains a few Python classes that make use of decorators from Flask-RESTX to wire up the endpoints:
#namespace.route("/path")
class EndpointApi:
def get():
...
The third file is main.py which simply starts the Flask server running. Unfortunately for me though, it contains an import which is flagged by pylint.
My app is working fine, but when I run pylint, it tells me there are unused imports. If I remove the imports, then the logic in the decorator that adds the route to the Flask API does not execute, and the result is that the endpoint is no longer added to the API.
Is there some way to add a class file (like endpoints.py) without importing it? I want pylint to stop warning me about unused imports, when clearly I'm using the decorator to call some global function that adds the API Resource handlers to Flask.
Sure, I could ignore the pylint error with a comment, but is there a better way? I am truly disgusted with placing a comment on every line of an import statement which I'm sure is not an "unused-import" (I have about 30).
Obviously, I could just refactor the decorator pattern into its constituent parts, and extract the relevant code to be included inside the main.py file. The equivalent code would look like this in main.py:
from endpoint import EndpointApi
EndpointApi = namespace.route("/path")(EndpointApi)
This is exactly the same code that's run in the decorator, so pylint considers my EndpointApi to be unused even though the decorator is using it to append a "/path" route to the namespace. Removing the decorator and adding the equivalent code to main.py decreases maintainability because now the relevant parts of EndpointApi are in two different files instead of all being defined one.
Edit
No, from endpoint import * makes it worse:
main.py:3:0: W0614: Unused import(s) EndpointApi, Resource and ns from wildcard import of endpoint (unused-wildcard-import)
Minimal example
flask-restx-hello $ pylint *py; for x in *py; do echo $x; cat $x; done
************* Module main
main.py:3:0: W0611: Unused EndpointApi imported from endpoint (unused-import)
------------------------------------------------------------------
Your code has been rated at 9.29/10 (previous run: 7.86/10, +1.43)
endpoint.py
"""docstring"""
from flask_restx import Resource
from init import ns
#ns.route('/hello')
class EndpointApi(Resource):
"""docstring"""
def get(self):
"""docstring"""
return {'hello': 'world'}
init.py
"""docstring"""
from flask import Flask
from flask_restx import Api
app = Flask(__name__)
api = Api(app)
ns = api.namespace('sick', description='crazy', path='/root/haha')
main.py
"""docstring"""
from init import app
from endpoint import EndpointApi
if __name__ == '__main__':
app.run(debug=True)
$ cat requirements.txt
aniso8601==9.0.1
attrs==22.1.0
click==8.0.4
dataclasses==0.8
Flask==2.0.3
flask-restx==0.5.1
importlib-metadata==4.8.3
itsdangerous==2.0.1
Jinja2==3.0.3
jsonschema==4.0.0
MarkupSafe==2.0.1
pkg-resources==0.0.0
pyrsistent==0.18.0
pytz==2022.2.1
six==1.16.0
typing-extensions==4.1.1
Werkzeug==2.0.3
zipp==3.6.0
Pylint isn't wrong. You aren't using endpoint anywhere in main.py. The only reason you're importing endpoint is to execute the decorator. Which is fine, but there is no way for pylint to know that.
In this case, it's ok to ignore the warning.
If you don't want to disable each of the import errors on every single link like this:
# pylint: disable=no-name-in-module
you could use:
# pylint: disable=import-error
Commenting this once in the file will disable pylint import errors for the entire file.
Additionally, this post on stack overflow may help:
Is it possible to ignore one single specific line with Pylint?
For more info on W0611 pylint unused imports:
https://pylint.pycqa.org/en/latest/user_guide/messages/warning/unused-import.html

Settings file reference is not found

I'm working on migrating an exsting Python 2.7 project to Python 3.9. I'm facing a directory structure-related issue in Python 3.
My current project directory structure is:
├───project
│ ├───core
| +--__init__.py
| +--main.py
| +--settings.py
│ ├───jobs
| +--job.py
main.py:
import settings
class Main:
def __init__(self, a=None, b=settings.B):
self.a = a
self.b = b
def start(self):
print(self.a, self.b)
job.py:
import sys
# sys.path.insert(0, '../core/')
from core.main import Main
from core import settings
main = Main(settings.A)
main.start()
There is no issues with this structure when Python 2.7 interpreter is used, but in Python 3.9 I see the following error when job.py is executed:
File "project\core\main.py", line 1, in <module>
import settings
ModuleNotFoundError: No module named 'settings'
The issue is fixable by uncommenting the code on line #2 of the job.py script, but I would like to avoid hardcoding folder values like that. I would appreciate if someone could provide an alternative approach and an explanation why it's behaving this way in the newer Python version.
Due to ambiguity absolute import was removed in python3 for such use case. (This explains it very well: Changes in import statement python3)
You can use relative import for this use case maybe - https://docs.python.org/3/reference/import.html#package-relative-imports

Why can't I reference this class from another app in my Django project?

I'm using Django and Python 3.7. I have two applications in my project -- common and mainsite . In common, I have a file "model_utils.py" at the root of my application (right next to models.py). It contains this code
class RawCol(Expression):
def __init__(self, model, field_name):
field = model._meta.get_field(field_name)
self.table = model._meta.db_table
self.column = field.column
super().__init__(output_field=CharField())
def as_sql(self, compiler, connection):
sql = f'"{self.table}"."{self.column}"'
return sql, []
How do I reference this class from my other application, "mainsite"? I put this at the top of one of my files in mainsite ..
from common import RawCol
but when I run some tests, I get this error ...
ImportError: cannot import name 'RawCol' from 'common' (/Users/davea/Documents/workspace/mainsite_project/common/__init__.py)
Edit: Project structure at the top level directories looks like ...
+ common
+ mainsite
+ mainsite_project
+ manage.py
+ templates
+ venv
Try from common.model_utils import RawCol
instead of from common import RawCol
You always need to specify the exact .py file (without the .py ending) to import from.
If it still doesn't work, it can be a circular import problem.
If you try to import something from mainsite.model into common.model_utils and the other way around, you are creating an impossible import loop.
You can fix this by creating a seperate file like common/dependent_model.py and putting only the RawCol() class in there without any import from mainsite. Like this, the both files are not importing from each other (which doesn't work).

'ImportError: No module named ' error in python

I am trying to create a simple Restful service in Python using Flask. I am a little confused with Python modules and how to use them. There have been many questions related tot this, however applying it in my case, doesn't help.
My project directory structure is as below:
algorithmservice
|-api.py
|-resources
|- __init__.py
|- optimization
|- __init__.py
|- instances.py
My instances.py looks like the following
from flask_restful import Resource
class Instance(Resource):
def get(self):
pass
def post(self):
pass
api.py is the following:
from flask import Flask
from flask.ext.restful import Api, Resource
from resources.optimization.instances import *
app = Flask(__name__)
api = Api(app)
When I run the command python api.py, I get the following error:
File "api.py", line 3, in <module>
from resources.optimization.instances import *
ImportError: No module named optimization.instances
i have tried a few other variations of the import statement, but none seem to work.

importing error in python while importing two classes

I am working with a python script, and i face importing problem when i try to import a class from another python script. Here is how my python project folder looks:
Mysql_Main/
checks.py
Analyzer/
config.py
ip.py
op.py
__init__.py
Now i want to import two classes named: Config() and Sqlite() from config.py into the checks.py script.How do i do it?
This is what i tried, but its resulting in an error!
inside checks.py:
from Analyzer import config
config = config.Config()
sqlite = config.Sqlite()
The problem is that Config class is imported properly, but Sqlite class is not getting imported.It is showing error - Config instance has no attribute 'Sqlite'
When you do:
config = config.Config()
You write over the variable config and it no longer points to the module config. It stores the new Config instance.
Try:
from Analyzer import config
config_instance = config.Config()
sqlite_instance = config.Sqlite()

Categories

Resources