# How to correctly import Classes from separate .py files

**URL:** <https://discuss.python.org/t/how-to-correctly-import-classes-from-separate-py-files/52927>\
**Category:** Python Help\
**Created:** [May 8, 2024, 11:33pm UTC](https://discuss.python.org/t/how-to-correctly-import-classes-from-separate-py-files/52927 "2024-05-08T23:33:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TortoiseGuy](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@TortoiseGuy](https://discuss.python.org/u/TortoiseGuy)\
**Post date:** [May 8, 2024, 11:33pm UTC](https://discuss.python.org/t/how-to-correctly-import-classes-from-separate-py-files/52927/1 "2024-05-08T23:33:01Z")

</div>

Sorry for probably a “dumb” question, but I am new to Python. In some of the books with example projects I am investigating, I see a CLASS from a separate .py file (eg: Square.py - containing the class called “Square”, are imported into the main\_py run file as, eg: “from Square import \*.” Square is the only class in the Square.py file, yet if I try to import as follows: “import Square”, I get an error. It only works if I import as follows: "from Square import \* - Why is import sometimes: “import pygames” and other times "from Square import \* - ? Thank you anyone can explain this to me.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [May 8, 2024, 11:37pm UTC](https://discuss.python.org/t/how-to-correctly-import-classes-from-separate-py-files/52927/2 "2024-05-08T23:37:46Z")

</div>

Hi,

the same way you do any other object (variables, functions, strings, etc). Assuming you have two modules. One name `A` and the other name `B`.

If there is a class name `MyClass` in module `A`, and you want to import it to module `B`, you can do:

```python
from A import MyClass

or

import A

```

then, to reference the class, you would prefix it with its module, as in:

```python
A.MyClass

```

This is the same thing that you do with imported library modules including the `numpy` module.

```python
import numpy as np

np.sqrt(25)

```

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [May 9, 2024, 12:04am UTC](https://discuss.python.org/t/how-to-correctly-import-classes-from-separate-py-files/52927/3 "2024-05-09T00:04:44Z")

</div>

> [@TortoiseGuy](#):
>
> Square is the only class in the Square.py file, yet if I try to import as follows: “import Square”, I get an error

In Python, when you import a module, you get _the module_. Unlike some other languages, there is **no special relationship** between modules and classes. When you write a module, you’re _allowed to_ have a class with the same name. You’re also allowed to have just one class with a different name. Or many classes, which may or may not have the module’s name. Or no classes at all.

The `Square` class is only one component of your `Square` module, even if it’s the only visible part. It’s the same as how a list with one string element, like `['example']`, is a completely different kind of thing from that string itself, `'example'`.

The things contained in a module are _attributes_ of the module, so you access them using the dot syntax. If you have source code `Square.py` and it defines a class `Square`, then `import Square` gives you a module named `Square`, which has a `Square` attribute (the class), which you access like: `Square.Square`.

> [@TortoiseGuy](#):
>
> It only works if I import as follows: `from Square import *`

Well, no, you have many other options.

This code means: load the `Square` module, but _don’t_ give it the name `Square`. Instead, look up _every_ attribute that the module has, and set _each of their_ names directly. So, if there’s a class `Square`, then `Square` becomes a name for that class. If there’s a function `resize`, then `resize` becomes a name for that function. If there’s an integer assigned like `NUMBER_OF_SIDES = 4`, then `NUMBER_OF_SIDES` becomes a name for that integer. Name by name, you get the same names in the current code, as were in the code you’re importing.

[This is a dangerous tool.](https://stackoverflow.com/questions/2386714) You don’t get to control what names are assigned; the module does. If you were already using one or more of those names, well, they get replaced. If the module [shadows](https://stackoverflow.com/questions/20125172) a [built-in name](https://stackoverflow.com/questions/6039605/), now [your code does too](https://stackoverflow.com/questions/75514722).

If you only want the `Square` class, you can say that: `from Square import Square`.

If you want a few names, but want to keep them organized inside the module, you can just import the module - `import Square` - and access the contents explicitly: `my_square = Square.Square()`.

---

<div class="post-metadata">

**Author:** ![TortoiseGuy](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@TortoiseGuy](https://discuss.python.org/u/TortoiseGuy)\
**Post date:** [May 9, 2024, 4:00am UTC](https://discuss.python.org/t/how-to-correctly-import-classes-from-separate-py-files/52927/4 "2024-05-09T04:00:05Z")

</div>

Thank you. I appreciate your clarification and time.

---

<div class="post-metadata">

**Author:** ![TortoiseGuy](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@TortoiseGuy](https://discuss.python.org/u/TortoiseGuy)\
**Post date:** [May 9, 2024, 4:04am UTC](https://discuss.python.org/t/how-to-correctly-import-classes-from-separate-py-files/52927/5 "2024-05-09T04:04:11Z")

</div>

Thank you for clarifying this for me. I greatly appreciate it.
