Python3 - How do I import a class from another file

Chris Angelico rosuav at gmail.com
Sun Dec 8 11:41:30 EST 2019


On Mon, Dec 9, 2019 at 3:31 AM R.Wieser <address at not.available> wrote:
>
> Hello all,
>
> Using Python3 I would like to import a specific class from another file (in
> the same folder), and have trouble doing so.
>
> "from file import function" works, but fails when I try to do the same with
> a class.
>
> "import file
> x = file.class"
>
> works, but also executes commands that are in the root of the file. And
> ofcourse loads all of the file, not just the class that I'm after ...
>
> In other words, what is the syntaxt I should use ?
>

Are you sure there's a difference between classes and functions here?
Try "from file import function" and see if it runs commands at the
root of the file.

ChrisA


More information about the Python-list mailing list