New-ish to python, but I though I had everything set up right. When I run python -m unittest test.unit.test_oyez_case, I get AttributeError: 'module' object has no attribute 'test_oyez_case'
Sorry this is a frequent question, none of the responses were helpful to me
Here's my file structure:
├── README.md
└── test
├── __init__.py
├── __init__.pyc
├── integration
│ └── __init__.py
└── unit
├── __init__.py
├── __init__.pyc
├── mocks
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── responses.py
│ └── responses.pyc
├── test_oyez_case.py
└── test_oyez_case.pyc
Here's test/unit/test_oyez_case.py:
import json
import unittest
import responses
from mocks import responses as api_responses
from puppy_scotus.oyez_case import OyezCase
class TestOyezCase(unittest.TestCase):
. . .
if __name__ == '__main__':
unittest.main()
Try to run it using a commandline like python test_oyez_case.py. It could be that one of your other imports is incorrect. ;-)
Related
I have the following package structure:
.
├── README.md
├── common
│ ├── __init__.py
│ ├── analysis
│ │ ├── __init__.py
│ │ └── base_analysis.py
│ ├── logger
│ ├── __init__.py
│ └── logger.py
└── scripts
└── test_analysis
└── run.py
I would like to access logger in base_analysis.py. If I do this:
from ..logger import Logger
I am getting this error:
ValueError: attempted relative import beyond top-level package
How to import a sub-package from the parent package?
Note: I am running the script from scripts/test_analysis using:
python run.py
following changes to the calling python run.py script fixed it;
from logger.logger import Logger
I've cloned the flaskr application from Github and am trying to follow the Testing Flask Applications tutorial. Following Bonus: Testing the Application, I've added a subdirectory test to the top-level flaskr directory, so that my directory tree looks like this:
.
├── build
│ ├── bdist.linux-x86_64
│ └── lib.linux-x86_64-2.7
│ └── flaskr
│ ├── flaskr.py
│ ├── __init__.py
│ ├── schema.sql
│ ├── static
│ │ └── style.css
│ └── templates
│ ├── layout.html
│ ├── login.html
│ └── show_entries.html
├── dist
│ └── flaskr-0.0.0-py2.7.egg
├── flaskr
│ ├── flaskr.db
│ ├── flaskr.py
│ ├── flaskr.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── schema.sql
│ ├── static
│ │ └── style.css
│ └── templates
│ ├── layout.html
│ ├── login.html
│ └── show_entries.html
├── flaskr.egg-info
│ ├── dependency_links.txt
│ ├── PKG-INFO
│ ├── requires.txt
│ ├── SOURCES.txt
│ └── top_level.txt
├── MANIFEST.in
├── README
├── setup.cfg
├── setup.py
├── test
│ └── test_flaskr.py
└── tests
└── test_flaskr.py
Note that there are also 'built-in' tests in the directory tests; however, I'm writing tests in test_flaskr.py in the directory tests. So far I'm trying just one test:
import os
import flaskr
import unittest
import tempfile
class FlaskrTestCase(unittest.TestCase):
def setUp(self):
self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
flaskr.app.config['TESTING'] = True
self.app = flaskr.app.test_client()
with flaskr.app.app_context():
flaskr.init_db()
def tearDown(self):
os.close(self.db_fd)
os.unlink(flaskr.app.config['DATABASE'])
def test_empty_db(self):
rv = self.app.get('/')
assert b'No entries here so far' in rv.data
if __name__ == '__main__':
unittest.main()
However, if I try to run this I get the following error:
Traceback (most recent call last):
File "/home/kurt/dev/scratch/flask/examples/flaskr/test/test_flaskr.py", line 13, in setUp
flaskr.init_db()
AttributeError: 'module' object has no attribute 'init_db'
I don't understand this error. My flaskr.py is the same as the one on https://github.com/pallets/flask/blob/master/examples/flaskr/flaskr/flaskr.py and has an init_db function defined. How can I make the unit test run?
When you import the flaskr package in your script, you can access the variables, functions etc. declared and defined in flaskr/__init__.py
init_db is not defined in flask/__init__.py, the code provided, expects init_db to be defined in flask/__init__.py.
There are two ways to fix the issue you have:
Replace flaskr.init_db() with flaskr.flaskr.init_db()
Modify the import statement in test_flaskr.py as follows:
Change import flaskr to from flaskr import flaskr
I've got a directory structure like:
Folder_in_PYTHONPATH/
├── Package1
│ ├── __init__.py
│ ├── src
│ │ ├── Class1.py
│ │ ├── Class2.py
│ │ └── __init__.py
│ └── test
│ └── testfile.py
├── Package2
│ ├── __init__.py
│ ├── src
│ │ ├── Class1.py
│ │ ├── Class2.py
│ │ └── __init__.py
│ └── test
│ ├── test1.py
│ └── test2.py
.
.
.
When I import things from this folder, I need to always type
import Package1.src.Class1
Is there any way to set up my __init__.py so that I can just type
import Package1.Class1
instead?
Add them into your packages' __init__.py files so they look like:
from src import Class1
from src import Class2
Have a look at the docs
I would recommend putting the *.py files in the top level folder of their package to get the import Package_1.Class1 behaviour you are after. The unit tests can stay in their own folder to keep them separate.
I have a my projects set up like so(the name package is used isn't the name of the directory only for this example it has been used):
.
├── Installer\ Script.iss
├── LICENSE
├── README.md
├── TODO.md
├── docs
├── requirements.txt
├── resources
│ ├── logo.hqx
│ ├── logo.icns
│ ├── logo.ico
│ └── qt.conf
├── setup.py
└── project
├── Main.py
├── Controller.py
├── Updator.py
├── Updator.pyc
├── __init__.py
├── tests
│ ├── TestController.py
│ ├── TestUpdator.py
│ ├── TestUpdator.pyc
│ └── __init__.py
└── ui.py
In my Main.py how should I import ui.py? Currently my code for importing looks like:
import project.ui as ui
Is this correct? As when I freeze the project with py2app it doesn't like my imports.
If the the package that you want to import in the same directory, you just write "import ui" in your main.py can work as you want it to.If you want to import ui in you setup.py(just for example), you can do it what you ask above.
Hope this can help you .
When I run an import in my development environment, I seem to get the wrong module.
How do I make sense of this?
$ python -c "import breathe.renderer.rst.doxygen.compound as com; print com"
<module 'breathe.parser.doxygen.compound' from 'breathe/parser/doxygen/compound.pyc'>
The layout of the breathe directory is:
breathe
├── directives.py
├── finder
│ ├── doxygen
│ │ ├── base.py
│ │ ├── compound.py
│ │ ├── index.py
│ │ └── __init__.py
│ └── __init__.py
├── __init__.py
├── nodes.py
├── parser
│ ├── doxygen
│ │ ├── compound.py
│ │ ├── compoundsuper.py
│ │ ├── index.py
│ │ ├── indexsuper.py
│ │ ├── __init__.py
│ ├── __init__.py
├── process.py
├── renderer
│ ├── __init__.py
│ └── rst
│ ├── doxygen
│ │ ├── base.py
│ │ ├── compound.py
│ │ ├── domain.py
│ │ ├── filter.py
│ │ ├── index.py
│ │ ├── __init__.py
│ │ └── target.py
│ ├── __init__.py
└── transforms.py
Check your breathe/renderer/rst/__init__.py file. I suspect that you have something like
from breathe.parser import doxygen
What happens is that when a module is imported, the module's __init__.py is executed. Then the next submodule is imported and it's __init__.py is run.
So, in the above situation, you'll get breathe.renderer.rst importing fine, but when running
breathe.renderer.rst.__init__, the breathe.parser.doxygen module is bound to the local name doxygen overriding the doxygen submodule. So you should see something like this:
>>> from breathe.renderer import rst
>>> print(rst.doxygen)
<module 'breathe.parser.doxygen' from 'breathe/parser/doxygen/__init__.pyc'>
Possible solutions:
Use absolute imports in __init__.py
Keep as much code as possible out of __init__.py
Use import inside the functions it's needed rather than globally. (This can also resolve circular import problems)
Note that there is nothing that prohibits code in __init__.py, and several modules in the stdlib do it too. However, because of exactly this sort of issue it is generally considered a good idea to avoid doing it too much.