0

For example, I have package X, which contains modules module1 and module2. In module1 I have a class MyClass, which I want to use in module2. Can I just do this in module2:

import module1

a = module1.MyClass()

?

I mean usage of relative path to module. In python documentation(in " Intra-package References" section) says, that in python2.7 we can use "import module1", but for python 3 they say, that we can use "from module1 import MyClass". But there are no information, is this first form prohibited. And this is my question.

1 Answer 1

1

Why don't you try it out?

This code won't work. It needs to be

import module1

a = module1.MyClass()

I suggest reading up on packages and import. They can get more confusing when your package starts to grow.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, for lost "module1." part. But I mean usage of relative path to module. In python documentation(in " Intra-package References" section) says, that in python2.7 we can use "import module1", but for python 3 they say, that we can use "from module1 import MyClass". But there are no information, is this first form prohibited. And this is my question.
"import module1" would import the entire module. You would be able to reach all code in the file using "module1.XXX". Using "from module1 import MyClass" only gives you MyClass. Python version isn't important for this, It should work in both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.