Pycharm imports works correctly, but when I execute test_settings.py in the terminal, I have this error:
ImportError while importing test module '/home/ed/PycharmProjects/TTime/tests/test_settings.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_settings.py:3: in
from ttime import app, db
E ModuleNotFoundError: No module named 'ttime'
Here is my Flask project structure. I tried to include init.py in the main folder and in the tests folder, but nothing changes. It seems like somethings is wrong with python paths.
.
├── config.py
├── README.md
├── requirements.txt
├── runserver.py
├── tests
│ ├── functional
│ │ └── __init__.py
│ ├── pytest.ini
│ ├── test_settings.py
│ └── unit
│ ├── __init__.py
│ └── test_models.py
└── ttime
├── __init__.py
├── models.py
├── routes.py
├── static
└── templates
├── base.html
└── index.html
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 am trying to study how to use alembic in flask, I want to import a method in flask app:
tree .
.
├── README.md
├── alembic
│ ├── README
│ ├── env.py
│ ├── env.pyc
│ ├── script.py.mako
│ └── versions
│ ├── 8f167daabe6_create_account_table.py
│ └── 8f167daabe6_create_account_table.pyc
├── alembic.ini
├── app
│ ├── __init__.py
│ ├── main
│ │ ├── __init__.py
│ │ ├── errors.py
│ │ ├── forms.py
│ │ └── views.py
│ ├── models.py
│ └── templates
│ ├── 404.html
│ ├── 500.html
│ ├── base.html
│ ├── index.html
│ └── user.html
├── config.py
├── data.sqlite
├── manage.py
└── requirements.txt
in app/__init__.py:
def create_app(config_name):
app = Flask(__name__)
I want to import create_app in env.py:
from app import create_app
but the error shows as below when I run the command alembic upgrade head:
File "alembic/env.py", line 5, in <module>
from app import create_app
ImportError: No module named app
Any idea for this?
I guess you are trying to run
python env.py
In this case, your app directory is not in PYTHONPATH.
solution 1
Run the app from parent dir:
python alembic/env.py
solution 2
Set the PYTHONPATH before running the app
PYTHONPATH=/path/to/parent/dir python env.py
edit
I read about alembic. As #mrorno said, just set the PYTHONPATH before running alembic:
PYTHONPATH=. alembic upgrade head
alembic just tries to load your env.py source code. It's not in your package, so it can't access your app module.
Use solution 2 #tomasz-jakub-rup suggested, you can execute like
$ PYTHONPATH=. alembic upgrade head
and you should get your result.
Create file .env and insert PYTHONPATH=.
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 .
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. ;-)