Difference between “from .checks import *” and “from . import checks”
I have a file called checks.py in my project.
I want to import everything at once.
Is there a difference between these two or is it just another way of writing it?
from .checks import *
from . import checks
python
add a comment |
I have a file called checks.py in my project.
I want to import everything at once.
Is there a difference between these two or is it just another way of writing it?
from .checks import *
from . import checks
python
1
Try accessingchecks.fooone way or the other later on in the code, and you'll see a difference.
– deceze♦
Nov 14 '18 at 7:14
add a comment |
I have a file called checks.py in my project.
I want to import everything at once.
Is there a difference between these two or is it just another way of writing it?
from .checks import *
from . import checks
python
I have a file called checks.py in my project.
I want to import everything at once.
Is there a difference between these two or is it just another way of writing it?
from .checks import *
from . import checks
python
python
asked Nov 14 '18 at 7:04
saschasascha
6943923
6943923
1
Try accessingchecks.fooone way or the other later on in the code, and you'll see a difference.
– deceze♦
Nov 14 '18 at 7:14
add a comment |
1
Try accessingchecks.fooone way or the other later on in the code, and you'll see a difference.
– deceze♦
Nov 14 '18 at 7:14
1
1
Try accessing
checks.foo one way or the other later on in the code, and you'll see a difference.– deceze♦
Nov 14 '18 at 7:14
Try accessing
checks.foo one way or the other later on in the code, and you'll see a difference.– deceze♦
Nov 14 '18 at 7:14
add a comment |
2 Answers
2
active
oldest
votes
In the first one, to access a member of checks, simply type the member's name (because you are importing all members). e.g.
from .checks import *
foo(5) # foo is member of checks
In the second one, you are only importing checks, which means to access foo, you must write checks.foo(5) as opposed to just foo(5).
Just a namespace thing, no big differences otherwise.
EDIT: As mentioned by @mata, typically in bigger projects imports with *s can get confusing, as you have no idea what module foo came from, and this is an even bigger problem if foo is a member of more than one module you are importing. Thus, importing using *s is considered bad practise.
1
Big difference is that if you have more then one*import you won't be able to easily tell where yourfoocame from. What if two modules have members with colliding names? For such reasons*imports are regarded as bad style.
– mata
Nov 14 '18 at 7:21
add a comment |
First, understand the meaning of the from somewhere import something, where somewhere refers to the directory where a certain package resides or the package itself, and something is a certain package or a feature from a particular package.
In your case, what you are trying to achieve is something called relative import. The from .<module/package> import something refers to a relative import. You can find more information on absolute and relative imports here.
Now, coming to your original question. Yes, there is a difference.
The from .checks import * statement will import all the classes/functions available inside the checks.py file. That is if your checks.py file contains a function named, let's say foo, then after executing this import statement you would be able to call foo() directly.
The from . import checks statement will import the module named checks. Hence, after executing this statement, you would be able to use/call any classes/function by following <module>.<class/function> notation. Therefore, in order to use foo() function, you have to use the syntax checks.foo()
Note: In both the cases, the file checks.py should be residing inside the current directory. The . refers to a relative path, in this case to the current directory.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294743%2fdifference-between-from-checks-import-and-from-import-checks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the first one, to access a member of checks, simply type the member's name (because you are importing all members). e.g.
from .checks import *
foo(5) # foo is member of checks
In the second one, you are only importing checks, which means to access foo, you must write checks.foo(5) as opposed to just foo(5).
Just a namespace thing, no big differences otherwise.
EDIT: As mentioned by @mata, typically in bigger projects imports with *s can get confusing, as you have no idea what module foo came from, and this is an even bigger problem if foo is a member of more than one module you are importing. Thus, importing using *s is considered bad practise.
1
Big difference is that if you have more then one*import you won't be able to easily tell where yourfoocame from. What if two modules have members with colliding names? For such reasons*imports are regarded as bad style.
– mata
Nov 14 '18 at 7:21
add a comment |
In the first one, to access a member of checks, simply type the member's name (because you are importing all members). e.g.
from .checks import *
foo(5) # foo is member of checks
In the second one, you are only importing checks, which means to access foo, you must write checks.foo(5) as opposed to just foo(5).
Just a namespace thing, no big differences otherwise.
EDIT: As mentioned by @mata, typically in bigger projects imports with *s can get confusing, as you have no idea what module foo came from, and this is an even bigger problem if foo is a member of more than one module you are importing. Thus, importing using *s is considered bad practise.
1
Big difference is that if you have more then one*import you won't be able to easily tell where yourfoocame from. What if two modules have members with colliding names? For such reasons*imports are regarded as bad style.
– mata
Nov 14 '18 at 7:21
add a comment |
In the first one, to access a member of checks, simply type the member's name (because you are importing all members). e.g.
from .checks import *
foo(5) # foo is member of checks
In the second one, you are only importing checks, which means to access foo, you must write checks.foo(5) as opposed to just foo(5).
Just a namespace thing, no big differences otherwise.
EDIT: As mentioned by @mata, typically in bigger projects imports with *s can get confusing, as you have no idea what module foo came from, and this is an even bigger problem if foo is a member of more than one module you are importing. Thus, importing using *s is considered bad practise.
In the first one, to access a member of checks, simply type the member's name (because you are importing all members). e.g.
from .checks import *
foo(5) # foo is member of checks
In the second one, you are only importing checks, which means to access foo, you must write checks.foo(5) as opposed to just foo(5).
Just a namespace thing, no big differences otherwise.
EDIT: As mentioned by @mata, typically in bigger projects imports with *s can get confusing, as you have no idea what module foo came from, and this is an even bigger problem if foo is a member of more than one module you are importing. Thus, importing using *s is considered bad practise.
edited Dec 6 '18 at 4:22
answered Nov 14 '18 at 7:16
Tomothy32Tomothy32
4,7011322
4,7011322
1
Big difference is that if you have more then one*import you won't be able to easily tell where yourfoocame from. What if two modules have members with colliding names? For such reasons*imports are regarded as bad style.
– mata
Nov 14 '18 at 7:21
add a comment |
1
Big difference is that if you have more then one*import you won't be able to easily tell where yourfoocame from. What if two modules have members with colliding names? For such reasons*imports are regarded as bad style.
– mata
Nov 14 '18 at 7:21
1
1
Big difference is that if you have more then one
* import you won't be able to easily tell where your foo came from. What if two modules have members with colliding names? For such reasons * imports are regarded as bad style.– mata
Nov 14 '18 at 7:21
Big difference is that if you have more then one
* import you won't be able to easily tell where your foo came from. What if two modules have members with colliding names? For such reasons * imports are regarded as bad style.– mata
Nov 14 '18 at 7:21
add a comment |
First, understand the meaning of the from somewhere import something, where somewhere refers to the directory where a certain package resides or the package itself, and something is a certain package or a feature from a particular package.
In your case, what you are trying to achieve is something called relative import. The from .<module/package> import something refers to a relative import. You can find more information on absolute and relative imports here.
Now, coming to your original question. Yes, there is a difference.
The from .checks import * statement will import all the classes/functions available inside the checks.py file. That is if your checks.py file contains a function named, let's say foo, then after executing this import statement you would be able to call foo() directly.
The from . import checks statement will import the module named checks. Hence, after executing this statement, you would be able to use/call any classes/function by following <module>.<class/function> notation. Therefore, in order to use foo() function, you have to use the syntax checks.foo()
Note: In both the cases, the file checks.py should be residing inside the current directory. The . refers to a relative path, in this case to the current directory.
add a comment |
First, understand the meaning of the from somewhere import something, where somewhere refers to the directory where a certain package resides or the package itself, and something is a certain package or a feature from a particular package.
In your case, what you are trying to achieve is something called relative import. The from .<module/package> import something refers to a relative import. You can find more information on absolute and relative imports here.
Now, coming to your original question. Yes, there is a difference.
The from .checks import * statement will import all the classes/functions available inside the checks.py file. That is if your checks.py file contains a function named, let's say foo, then after executing this import statement you would be able to call foo() directly.
The from . import checks statement will import the module named checks. Hence, after executing this statement, you would be able to use/call any classes/function by following <module>.<class/function> notation. Therefore, in order to use foo() function, you have to use the syntax checks.foo()
Note: In both the cases, the file checks.py should be residing inside the current directory. The . refers to a relative path, in this case to the current directory.
add a comment |
First, understand the meaning of the from somewhere import something, where somewhere refers to the directory where a certain package resides or the package itself, and something is a certain package or a feature from a particular package.
In your case, what you are trying to achieve is something called relative import. The from .<module/package> import something refers to a relative import. You can find more information on absolute and relative imports here.
Now, coming to your original question. Yes, there is a difference.
The from .checks import * statement will import all the classes/functions available inside the checks.py file. That is if your checks.py file contains a function named, let's say foo, then after executing this import statement you would be able to call foo() directly.
The from . import checks statement will import the module named checks. Hence, after executing this statement, you would be able to use/call any classes/function by following <module>.<class/function> notation. Therefore, in order to use foo() function, you have to use the syntax checks.foo()
Note: In both the cases, the file checks.py should be residing inside the current directory. The . refers to a relative path, in this case to the current directory.
First, understand the meaning of the from somewhere import something, where somewhere refers to the directory where a certain package resides or the package itself, and something is a certain package or a feature from a particular package.
In your case, what you are trying to achieve is something called relative import. The from .<module/package> import something refers to a relative import. You can find more information on absolute and relative imports here.
Now, coming to your original question. Yes, there is a difference.
The from .checks import * statement will import all the classes/functions available inside the checks.py file. That is if your checks.py file contains a function named, let's say foo, then after executing this import statement you would be able to call foo() directly.
The from . import checks statement will import the module named checks. Hence, after executing this statement, you would be able to use/call any classes/function by following <module>.<class/function> notation. Therefore, in order to use foo() function, you have to use the syntax checks.foo()
Note: In both the cases, the file checks.py should be residing inside the current directory. The . refers to a relative path, in this case to the current directory.
answered Nov 14 '18 at 8:02
Dhruv JoshiDhruv Joshi
6625
6625
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294743%2fdifference-between-from-checks-import-and-from-import-checks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try accessing
checks.fooone way or the other later on in the code, and you'll see a difference.– deceze♦
Nov 14 '18 at 7:14