This is my folder structure:
├── Basic
├── Coche
│ ├── __init.py__
│ ├── coche.py
├── miPrimerCoche.py
I want to import the class "coche.py" in miPrimerCoche. In coche.py I have this:
class Coche:
def __init__(self, marca, color, caballos):
self.marca = marca
self.color = color
self.caballos = caballos
def datos(self):
return "Este coche es un: " + self.marca + \
" de color: " + self.color + " y con: " + str(self.caballos) + " caballos"
And, in miPrimerCoche I have this code:
from Coche import coche
miMercedes = coche("Toyota", "verde", 50)
print miMercedes.marca
print miMercedes.datos()
Then, when I run miPrimerCoche, I get this error:
Traceback (most recent call last):
File "/Users/personalUser/PycharmProjects/untitled/Basic/importar_clase.py", line 3, in <module>
miMercedes = coche("Toyota", "verde", 50)
TypeError: 'module' object is not callable
Basic is the src folder (it is in blue color), what I can do?
I resolved with
miMercedes = coche.Coche(par1, par2, par3...)
but I don´t know if is the good way to do it.
From the point of view of miPrimerCoche.py
:
Coche
is a module (the folder Coche
) Coche.coche
is a submodule (the file coche.py
) Choche.coche.Coche
is the class Coche
in the submodule Coche.coche
So you actually want:
from Coche.coche import Coche
The coche
you're importing is just the (sub) module, as the error points out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.