Skip to content
← Back to Community
How do I separate the class code into its own module from the import code?
Profile icon
JohnDoe47

I have tried adding folders and files to the main.py, separating the class code and the import code, but I cannot get the import code to call the class code. In other words, I need help creating the proper directories in repl.it.

Voters
Profile icon
outrun
Profile icon
Codejira
Profile icon
Highwayman
Profile icon
JohnDoe47
Comments
hotnewtop
Profile icon
outrun

how do i get an idel dice import code

Profile icon
JohnDoe47

It worked. Thx, Codejira and Highwayman. I owe you each a beer (or a roll of toilet paper, given the times we're in).
Put class code in a folder under the main py
From folder name import class name

Profile icon
Codejira

Start reading here: (https://docs.python.org/3.8/tutorial/modules.html)

A basic solution:
Move your class code to a file, say vehicle.py, in your repl root.
In main.py do

import vehicle # name of the file without .py my_car = vehicle.car("Volga", "Comrade", 1986)

A simple program of mine has some examples.
Have a look at Caesar Pypher.

Edit: fix markdown

Profile icon
Highwayman

Lines 3 to 18 would go in their own file named Car.py and then you’d change your import statement to be from Car import * or from Car import Car, and then it should work as intended.