मैं विंडोज एक्सपी एसपी 3 पर पायथन 2.6.1 चला रहा हूं। मेरा आईडीई PyCharm 1.0-बीटा 2 बिल्ड पीवाई -96.1055 है।पायथन "आयात त्रुटि: कोई मॉड्यूल नाम नहीं" समस्या
मैं अपनी .py फ़ाइलों को "src" नामक निर्देशिका में संग्रहीत कर रहा हूं; इसमें __init__.py
फ़ाइल है जो शीर्ष पर "__author__
" विशेषता को छोड़कर खाली है।
उनमें से एक Matrix.py कहा जाता है:
#!/usr/bin/env python
"""
"Core Python Programming" 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)
if self.cols != other.cols:
return self.cols.cmp(other.cols)
for i in range(0, self.rows):
for j in range(0, self.cols):
if self.matrix[i][j] != other.get(i,j):
return self.matrix[i][j] == (other.get(i,j))
return True # if you get here, it means size and values are equal
if __name__ == '__main__':
a = Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = Matrix(3, 3, [[6, 5, 4], [1, 1, 1], [2, 1, 0]])
c = Matrix(3, 3, [[2, 0, 0], [0, 2, 0], [0, 0, 2]])
a.show()
b.show()
c.show()
a.add(b).show()
a.mul(c).show()
मैं "परीक्षण" नामक एक नया निर्देशिका भी एक __init__.py
फ़ाइल है कि शीर्ष पर एक "__author__
" विशेषता को छोड़कर यह खाली है है कि बना लिया है। मैं एक MatrixTest.py इकाई के लिए अपने मैट्रिक्स वर्ग बना लिया है:
#!/usr/bin/env python
"""
Unit test case for Matrix class
See http://jaynes.colorado.edu/PythonGuidelines.html#module_formatting for Python coding guidelines
"""
import unittest #use my unittestfp instead for floating point
from src import Matrix # Matrix class to be tested
__author__ = 'Michael'
__credits__ = []
__license__ = "GPL"
__version__ = "1.0"
__maintainer__ = "Michael"
__status__ = "Development"
class MatrixTest(unittest.TestCase):
"""Unit tests for Matrix class"""
def setUp(self):
self.a = Matrix.Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
self.b = Matrix.Matrix(3, 3, [[6, 5, 4], [1, 1, 1], [2, 1, 0]])
self.c = Matrix.Matrix(3, 3, [[2, 0, 0], [0, 2, 0], [0, 0, 2]])
def testAdd(self):
expected = Matrix.Matrix(3, 3, [[7, 7, 7], [5, 6, 7], [9, 9, 9]]) # need to learn how to write equals for Matrix
self.a.add(self.b)
assert self.a == expected
if __name__ == '__main__': #run tests if called from command-line
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
unittest.TextTestRunner(verbosity=2).run(suite)
फिर भी जब मैं अपने MatrixTest चलाने के लिए मैं इस त्रुटि मिलती है कोशिश:
C:\Tools\Python-2.6.1\python.exe "C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py"
Traceback (most recent call last):
File "C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core/test/MatrixTest.py", line 8, in <module>
from src import Matrix # Matrix class to be tested
ImportError: No module named src
Process finished with exit code 1
सब कुछ मैं पढ़ा है मुझे होने कि बताता है init .py मेरी सभी निर्देशिकाओं में इसका ध्यान रखना चाहिए।
अगर कोई यह बता सकता है कि मुझे क्या याद आया है तो मैं इसकी सराहना करता हूं।
मैं स्रोत और इकाई परीक्षण कक्षाओं को विकसित और बनाए रखने के सर्वोत्तम तरीके पर सलाह भी चाहूंगा। मैं इस बारे में सोच रहा हूं कि जब मैं जावा लिखता हूं:/src और/test निर्देशिका, नीचे समान पैकेज संरचनाओं के साथ। क्या यह "पायथनिक" सोच है, या क्या मुझे एक और संगठन योजना पर विचार करना चाहिए?
अद्यतन:
जो लोग जवाब दे दिया है के लिए धन्यवाद, यहाँ समाधान है कि मेरे लिए काम किया है:
- आयात बदलें
from src import Matrix # Matrix class to be tested
- को
sys.path
मेरी unittest विन्यास के लिए एक वातावरण चर के रूप में, के साथ अर्ध-कॉलन से अलग ./src और ./test निर्देशिकाएं। - दिखाए गए अनुसार MatrixTest.py में घोषणाएं बदलें।
आप कहते हैं कि आपकी फ़ाइलें एक src फ़ोल्डर में संग्रहीत हैं? सी: \ टूल्स \ पायथन-2.6.1 \ python.exe "सी:/दस्तावेज़ और सेटिंग्स/माइकल/मेरे दस्तावेज़/परियोजनाएं/पायथन/सीखना/कोर/परीक्षण/MatrixTest.py" – awithrow
'python -m test.MatrixTest का प्रयोग करें '। Http://stackoverflow.com/questions/24622041/python-importing-a- मॉड्यूल-from-a-parallel-directory – jrc