ImportError: No module named FlaskApp - python

I am puzzled by no module named app importerror. When I use name FlaskApp, everything works, when I change name to xyzApp, it does not and I am getting ImportError.
Here is the error code:
Target WSGI script '/var/www/xyz/xyzApp.wsgi' cannot be loaded as Python module.
Exception occurred processing WSGI script '/var/www/xyz/xyzApp.wsgi'.
Traceback (most recent call last):
File "/var/www/xyz/xyzApp.wsgi", line 11, in <module>
from xyzApp import app as application
ImportError: No module named 'xyzApp'
Target WSGI script '/var/www/xyz/xyzApp.wsgi' cannot be loaded as Python module.
Exception occurred processing WSGI script '/var/www/xyz/xyzApp.wsgi'.
Traceback (most recent call last):
File "/var/www/xyz/xyzApp.wsgi", line 11, in <module>
from xyzApp import app as application
ImportError: No module named 'xyzApp'
The Structure of the Flask applications looks as follows:
/var/www/xyz/
|--xyzApp.wsgi
|--/instance/config.py
|--/xyzApp
|--/static
|--/templates
|--__init__.py
When I do application with similar structure but with FlaskApp folders, it works. Why is that?
The structure that works is as follows:
/var/www/FlaskApp/
|--flaskapp.wsgi
|--/FlaskApp
|--/static
|--/templates
|--__init__.py
The content of flaskapp.wsgi file is the same except for xyzApp:
#! /usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp")
from FlaskApp import app as application

Related

unittesting fails on ModuleNotFound in the file to be tested imports

I am writing a small unittest for my method move(). The application is running but when I do python -m unittest app.test.test_move_items, it fails with the following error:
ImportError: Failed to import test module: test_move
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "/Users/daniel/app/test/test_move.py", line 13, in <module>
from app.move_items import move
File "/Users/daniel/apps/app/move.py", line 29, in <module>
from helpers.calculator import determine_move_amount
ModuleNotFoundError: No module named 'helpers'
If i do python app/move.py, the output is correct.
The folder structure is as follows:
app
helpers
|-calculator.py
move_items.py
test
|-test_move_items.py
move_items.py
from helpers.calculator import determine_move_amount
def move(list_of_items, move_by):
pass
There is init.py file in app and the test directory as well.

ModuleNotFoundError in Using 3rd party module in external .py file

I installed third party module and its egg file was created in following path
D:\Utkarsh\Lib\site-packages
I am not getting error while importing module in IDLE in following way
import snakebite
When i am importing the same in HDFS.py file having following lines
import snakebite
from snakebite.client import Client
client = Client('localhost', 9000)
it causes following stack error:
============ RESTART: D:/Utkarsh/Python Projects/HDFS.py ============
Traceback (most recent call last):
File "D:/Utkarsh/Python Projects/HDFS.py", line 1, in <module>
import snakebite
ModuleNotFoundError: No module named 'snakebite'
sys.path had following values:
D:/Utkarsh/Python Projects
D:\Utkarsh\Installation\Lib\idlelib
D:\Utkarsh\Installation\python36.zip
D:\Utkarsh\Installation\DLLs
D:\Utkarsh\Installation\lib
D:\Utkarsh\Installation
D:\Utkarsh\Installation\lib\site-packages
Being newbie,can anybody help me in knowing exact cause of it.

What are the reserved Python module/package names?

I got a strange error while working with Python unittest. I have two folders in my project:
project
code
__init__.py (empty)
app.py (defines my App class)
test
test.py (contains my unit tests)
test.py is:
import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from code.app import App
class test_func1(unittest.TestCase):
...
When I run test.py I get the message:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...test.py, line 5, in <module>
from code.app import App
ImportError: No module named 'code.app': 'code' is not a package
After verifying that __init__.py was present and banging my head for a while, on a whim I changed the name of the app directory from code to prog:
import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from prog.app import App
... and everything was suddenly fine. Unittest imported my app properly and ran the tests.
I've searched through https://docs.python.org/3.5/reference/lexical_analysis.html#keywords and https://docs.python.org/3/reference/import.html#path-entry-finders and don't see any indication that code is an illegal directory name. Where would this be documented, and what other directory names are reserved?
System: python 3.4.3 [MSC v1600 32 bit] on win32, Windows 7
code isn't reserved, but it is already defined in the standard library, where is it a regular module and not package. To import from your package, you should use a relative import.
from .code.app import App

importing a file as a package - Python

I have the following directory structure:
/testlib
__init__.py
ec2.py
unit/
__init__.py
test_ec2.py
utils/
__init__.py
I'm trying to create a unittest class for ec2.py:
import ec2
class TestEC2(unittest.TestCase):
def setUp(self):
self.ec2obj = ec2.EC2(name="testlib_unittest")
if __name__ == '__main__':
unittest.main()
However, when I execute test_ec2.py I'm getting the following error:
python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
import ec2
ImportError: No module named ec2
I still don't understand why I'm getting that, since I have the __init__.py properly set in the directory. The __init__.py files are completely empty: I've created them with touch __init__.py. Any clues?
**** Updates ****
Here are some suggestions:
/testlib# python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
from ..ec2 import EC2
ValueError: Attempted relative import in non-package
testlib# python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
import testlib.ec2 as ec2
ImportError: No module named testlib.ec2
It can't find the module because you're running the script incorrectly. Run the following in testlib/:
python -m unit.test_ec2
Python is not looking for files in directories above yours. Ignacio's answer is correct, and see this for elaboration.

Pyramid cookbook WSGI example out of date?

Working through this example in the Pyramid cookbook: http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/modwsgi/index.html
This stanza is throwing an error: (when adapted to my circumstances anturally)
from pyramid.paster import get_app, setup_logging
ini_path = '/Users/chrism/modwsgi/env/myapp/production.ini'
setup_logging(ini_path)
application = get_app(ini_path, 'main')
The error message:
mod_wsgi (pid=27548): Target WSGI script '/home/rsadmin/modwsgi/env/hydra/hydra.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=27548): Exception occurred processing WSGI script '/home/rsadmin/modwsgi/env/hydra/hydra.wsgi'.
Traceback (most recent call last):
File "/home/rsadmin/modwsgi/env/hydra/hydra.wsgi", line 1, in <module>
from pyramid.paster import get_app, setup_logging
ImportError: No module named pyramid.paster
I suspect what has happened is that pyramid.paster has been refactored since this doc was written, and no one has had time to correct it for the newest release.
Can anyone tell me what ought to be there instead, nowadays ?
TIA,
Erik
I had the hydra.wsgi script in the wrong directory. It needed to be one up from the path it was in: should be here:
/home/rsadmin/modwsgi/env/hydra.wsgi
The example in the cookbook is correct.

Categories