Python Test Import Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this python unittest import error site About Us Learn more about Stack Overflow the company Business Learn more
Importerror: No Module Named Unittest
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x importerror python cannot import name Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Python “ImportError: import error python no module named No module named” Problem up vote 10 down vote favorite 1 I'm running Python 2.6.1 on Windows XP SP3. My IDE is PyCharm 1.0-Beta 2 build PY-96.1055. I'm storing my .py files in a directory named "src"; it has an __init__.py file that's empty except for an "__author__" attribute at the top. One of them is called Matrix.py: #!/usr/bin/env python """ "Core Python Programming"
No Module Named Tests
chapter 6. A simple Matrix class that allows addition and multiplication """ __author__ = 'Michael' __credits__ = [] __version__ = "1.0" __maintainer__ = "Michael" __status__ = "Development" class Matrix(object): """ exercise 6.16: MxN matrix addition and multiplication """ def __init__(self, rows, cols, values = []): self.rows = rows self.cols = cols self.matrix = values def show(self): """ display matrix""" print '[' for i in range(0, self.rows): print '(', for j in range(0, self.cols-1): print self.matrix[i][j], ',', print self.matrix[i][self.cols-1], ')' print ']' def get(self, row, col): return self.matrix[row][col] def set(self, row, col, value): self.matrix[row][col] = value def rows(self): return self.rows def cols(self): return self.cols def add(self, other): result = [] for i in range(0, self.rows): row = [] for j in range(0, self.cols): row.append(self.matrix[i][j] + other.get(i, j)) result.append(row) return Matrix(self.rows, self.cols, result) def mul(self, other): result = [] for i in range(0, self.rows): row = [] for j in range(0, other.cols): sum = 0 for k in range(0, self.cols): sum += self.matrix[i][k]*other.get(k,j) row.append(sum) result.append(row) return Matrix(self.rows, other.cols, result) def __cmp__(self, other): """ deep equals between two matricies first check rows, then cols, then values """ if self.rows != other.rows: return self.rows.cmp(other.rows)
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site python importerror About Us Learn more about Stack Overflow the company Business Learn more about importerror no module named src hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Attempted Relative Import In Non-package
Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up python import error http://stackoverflow.com/questions/3646307/python-importerror-no-module-named-problem up vote 3 down vote favorite what's wrong with my imports? App folder structure: myapp/ models/models.py contains SpotModel() tests/tests.py contains TestSpotModel(unittest.TestCase). tests.py imports from myapp.models.models import * which works like a charm scripts/import.py contains from myapp.models.models import * the problem is that import.py when executed results in an error: ImportError: No module named myapp.models.models but tests.py runs. I have __init__.py files in myapp/__init__.py, myapp/models/__init__.py, myapp/tests/__init__.py http://stackoverflow.com/questions/3537850/python-import-error and as mentioned, running the unit tests using nosetests works as intended. python importerror python-import share|improve this question edited Jun 4 '13 at 7:47 ferkulat 1,48421428 asked Aug 21 '10 at 13:55 kristian nissen 1,12532252 from foo import * is highly discouraged because it causes namespace collisions. It is hard to tell what might be happening with that statement present. –msw Aug 21 '10 at 14:03 1 I'd also try to avoid naming a module with a Python keyword like import. Now you can't import myapp.import! –bobince Aug 21 '10 at 14:06 I renamed it to python myapp/scripts/data.py but it's still the same problem –kristian nissen Aug 21 '10 at 14:26 @msw Within packages, import * is much less of a problem if one knows what they're doing, and also ubiquitous. –Nick T Aug 21 '10 at 15:32 2 @Nick T: sexually transmitted diseases are ubiquitous, it doesn't mean they need an advocate. And in this instance one didn't know what one was doing. –msw Aug 21 '10 at 19:03 add a comment| 4 Answers 4 active oldest votes up vote 4 down vote accept
what seems to be http://kronosapiens.github.io/blog/2014/07/28/understanding-package-imports-in-python.html an incidental change. Tests will run in one https://schettino72.wordpress.com/2008/01/19/11/ directory but not another, but then inadvertently start working, only to stop a few days or minutes later. I’ve tried to be methodical in investigating what changes lead to what behavior, but it’s no module been difficult. To hopefully put this to rest, I’m going to investigate and methodically record all the behavior I can isolate regarding package imports, to hopefully make some sense of what’s going on. PYTHONPATH First, let’s start with a project I’m calling no module named ‘backend’. Here’s the file structure: backend/ backend/ __init__.py analyzer.py tests/ __init__.py test_analyzer.py And my PYTHONPATH: ƒ: echo $PYTHONPATH /Users/kronosapiens/Dropbox/Documents/Development/code/jobs/paragon/backend/: So, my PYTHONPATH is pointing to the directory containing the backend package, but not to the backend package itself (which contains __init__.py). Let’s open up a terminal and play around: >>> import backend >>> backend <module 'backend' from 'backend/__init__.pyc'> Very cool. Now let’s cd into the backend package and see if anything changes: >>> import backend >>> backend <module 'backend' from '/Users/kronosapiens/Dropbox/Documents/Development/code/jobs/paragon/ but i used to get confused on how to organize the import(s) and when to use PYTHONPATH for my tests. my requirements are: i want to be able to run my tests both from my_project folder and from the tests folder i don't want to set the PYTHONPATH manually (and i don't want to add my development path to PYTHONPATH. i want to import the lib modules on the test on the same way i would import after the lib is deployed. (yes i am lazy) bar.py: def dumb_true(): return True 1st try the code for test_bar.py should like something like this: import unittest from foo import bar class TestBar(unittest.TestCase): def test_bar_true(self): self.assertTrue(bar.dumb_true()) if __name__ == '__main__': unittest.main() the problem here is that you need to set the PYTHONPATH manually. 2nd try the first thing that comes in my mind is to add ‘foo' folder to PYTHONPATH on the test module ... import sys,os sys.path.insert(0,os.path.abspath(__file__+"/../..")) from foo import bar ... not bad. but you need to add this to every single test module. and hope that your folder structure wont change. 3rd try i guess we need a smarter test runner (like nose). get your code back to just: ... from foo import bar ... you can just run "nosetests" from the project folder (‘my_project' in the example above) and it will just work. the same doesn't happen if run "nosetests" from the tests folder. from nose homepage: When nose imports a module, it adds that module's directory to sys.path; when the module is inside of a package, like package.module, it will be loaded as package.module and the directory of package will be added to sys.path. so the trick here is to make the tests folder a package (adding __init__.py to it). so in the tests folder you will find: ./tests ./__init__.py ./test_foo.py ./test_bar.py now from the tests folder you can: execute all tests doing "nosetests" execute just one file "nosetests test_bar.py" check nose docs for more options and you don't have to worry about PYTHONPATH at all :) more nose nose also free you from all the unittest boilerplate code. the test file could be just: from foo i