2016-05-30 8 views
-1

私は適当な言語だと信じているimport carを実行することによって使用したいPythonクラスを作成しました。現在、私のクラスで何かを使用できる唯一の方法は、Python3 IDEでファイル(/home/pi/Desktop/python/car.py)を開き、実行してからクラスを使用することです。私は/usr/lib/python3.4がそれを置くための適切な場所だろうと信じて私自身のpythonモジュールを作る

は、しかし、私はそれを試してみましたし、出力はここにある:

>>> import car 
>>> my_car = car('n', 'vns', '15', '13') 
Traceback (most recent call last): 
    File "<pyshell#6>", line 1, in <module> 
    my_car = car('n', 'vns', '15', '13') 
TypeError: 'module' object is not callable 

クラスコードは不要かもしれませんが、ここにある:

class car(): 
     """Your car.""" 

    def __init__(self, make, model, year, fuel_capacity): 
     """Atributtes of your car, fuel in gallons.""" 
     self.make = make 
     self.model = model 
     self.year = year 
     self.fuel_capacity = fuel_capacity 
     self.fuel_level = 0 

    def fill_tank(self): 
     """Fill up your gas.""" 
     self.fuel_level = self.fuel_capacity 
     print("Fuel tank is full") 

    def drive(self): 
     """Drive your car""" 
     print("The car is moving") 
     self.fuel_level = self.fuel_level 

    def specs(self): 
     print(self.year, self.make, self.model, self.fuel_capacity, "Gallons") 
+2

'mycar = car.car( 'n'、 'vns'、 '15'、 '13') ' – Mephy

+2

または' from car輸入車 ' – Blorgbeard

答えて

2

まず第一に、あなたのPythonインストールディレクトリにローカルモジュールを作成しないでください。

PYTHONPATHを指定していないため、モジュールをインポートできません。 コマンドラインでそのモジュールへのパスをエクスポートする必要があります。 export PYTHONPATH=/home/pi/Desktop/python/

また、PEP8によれば、クラス名はCapWords規約を使用する必要があります。 https://www.python.org/dev/peps/pep-0008/#id39

あなたのPythonインタラクティブシェルを今入力すると、そのモジュールをインポートできるはずです。

from car import Car

2
import car 
my_car = car('n', 'vns', '15', '13') 
Traceback (most recent call last): 
    File "<pyshell#6>", line 1, in <module> 
    my_car = car('n', 'vns', '15', '13') 
TypeError: 'module' object is not callable 

あなたは、一般的なPythonの開発に関するいくつかのミスを作っているが、それは今働いていない理由として、あなたの質問に答えるために:

car.car('n', 'vns', '15', '13')代わりのcar

+1

または' from car import car' – zondo

関連する問題