Calling function without import the other file function in same directoryHow to avoid circular imports in Python?Calling a function of a module by using its name (a string)How do I check whether a file exists without exceptions?How do I return multiple values from a function?How to import other Python files?How do I list all files of a directory?Run a Python script from another Python script, passing in argumentsFind all files in a directory with extension .txt in PythonHow to import the class within the same directory or sub directory?Importing files from different folderfatal error: Python.h: No such file or directory
Is it ethical to tell my teaching assistant that I like him?
Why did computer video outputs go from digital to analog, then back to digital?
Is an easily guessed plot twist a good plot twist?
Extrapolation v. Interpolation
Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?
How can I indicate that what I'm saying is not sarcastic online?
Is the apartment I want to rent a scam?
Can GPL and BSD licensed applications be used for government work?
Is there a way to shorten this while condition?
Why is the UH-60 tail rotor canted?
German phrase for 'suited and booted'
High income and difficulty during interviews
Does downing a character at the start of its turn require an immediate Death Saving Throw?
Does quantity of data extensions impact performance?
Does Impedance Matching Imply any Practical RF Transmitter Must Waste >=50% of Energy?
Are there any English words pronounced with sounds/syllables that aren't part of the spelling?
Strange LED behavior: Why is there a voltage over the LED with only one wire connected to it?
How could Barty Crouch Jr. have run out of Polyjuice Potion at the end of the Goblet of Fire movie?
Chemistry Riddle
Is it better to merge "often" or only after completion do a big merge of feature branches?
Considerations when providing money to one child now, and the other later?
Are there any documented cases of extinction of a species of fungus?
How did pilots avoid thunderstorms and related weather before “reliable” airborne weather radar was introduced on airliners?
Why do people say "I am broke" instead of "I am broken"?
Calling function without import the other file function in same directory
How to avoid circular imports in Python?Calling a function of a module by using its name (a string)How do I check whether a file exists without exceptions?How do I return multiple values from a function?How to import other Python files?How do I list all files of a directory?Run a Python script from another Python script, passing in argumentsFind all files in a directory with extension .txt in PythonHow to import the class within the same directory or sub directory?Importing files from different folderfatal error: Python.h: No such file or directory
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to call im1.py's want_to_call() method from im2.py Test's class func1() and func1() will be called by im1's some_func() method....
Your help will be appreciated. Thanks in advance
im1.py
from im2 import Test
def some_func(value):
Test.func1()
print(value)
def want_to_call():
return 'called from im2'
some_func("ola")
im2.py
from im1 import want_to_call
class Test:
def func1():
variable = want_to_call()
print(variable)
print('How do I call want_to_call method in im1')
class Test1:
def func():
print('Thanks in advance')
python django python-3.x
add a comment |
I want to call im1.py's want_to_call() method from im2.py Test's class func1() and func1() will be called by im1's some_func() method....
Your help will be appreciated. Thanks in advance
im1.py
from im2 import Test
def some_func(value):
Test.func1()
print(value)
def want_to_call():
return 'called from im2'
some_func("ola")
im2.py
from im1 import want_to_call
class Test:
def func1():
variable = want_to_call()
print(variable)
print('How do I call want_to_call method in im1')
class Test1:
def func():
print('Thanks in advance')
python django python-3.x
1
Possible duplicate of How to avoid circular imports in Python?
– ruddra
Mar 26 at 13:55
@ruddra thanx sir, can u please do it with my question, I'll be thankful for that.....
– Nikhil Bhardwaj
Mar 26 at 16:16
add a comment |
I want to call im1.py's want_to_call() method from im2.py Test's class func1() and func1() will be called by im1's some_func() method....
Your help will be appreciated. Thanks in advance
im1.py
from im2 import Test
def some_func(value):
Test.func1()
print(value)
def want_to_call():
return 'called from im2'
some_func("ola")
im2.py
from im1 import want_to_call
class Test:
def func1():
variable = want_to_call()
print(variable)
print('How do I call want_to_call method in im1')
class Test1:
def func():
print('Thanks in advance')
python django python-3.x
I want to call im1.py's want_to_call() method from im2.py Test's class func1() and func1() will be called by im1's some_func() method....
Your help will be appreciated. Thanks in advance
im1.py
from im2 import Test
def some_func(value):
Test.func1()
print(value)
def want_to_call():
return 'called from im2'
some_func("ola")
im2.py
from im1 import want_to_call
class Test:
def func1():
variable = want_to_call()
print(variable)
print('How do I call want_to_call method in im1')
class Test1:
def func():
print('Thanks in advance')
python django python-3.x
python django python-3.x
asked Mar 26 at 13:04
Nikhil BhardwajNikhil Bhardwaj
721 silver badge10 bronze badges
721 silver badge10 bronze badges
1
Possible duplicate of How to avoid circular imports in Python?
– ruddra
Mar 26 at 13:55
@ruddra thanx sir, can u please do it with my question, I'll be thankful for that.....
– Nikhil Bhardwaj
Mar 26 at 16:16
add a comment |
1
Possible duplicate of How to avoid circular imports in Python?
– ruddra
Mar 26 at 13:55
@ruddra thanx sir, can u please do it with my question, I'll be thankful for that.....
– Nikhil Bhardwaj
Mar 26 at 16:16
1
1
Possible duplicate of How to avoid circular imports in Python?
– ruddra
Mar 26 at 13:55
Possible duplicate of How to avoid circular imports in Python?
– ruddra
Mar 26 at 13:55
@ruddra thanx sir, can u please do it with my question, I'll be thankful for that.....
– Nikhil Bhardwaj
Mar 26 at 16:16
@ruddra thanx sir, can u please do it with my question, I'll be thankful for that.....
– Nikhil Bhardwaj
Mar 26 at 16:16
add a comment |
1 Answer
1
active
oldest
votes
Don't do that.
The advice to "only import the module" works (How to avoid circular imports in Python?).
But you'd be better off putting your functions into more files that are arranged in a hierarchy.
In other words, break the cycle.
It will be beneficial for the organization of your code,
and of your unit tests,
and for how you think about your high level problem.
Here, the definitions of want_to_call()
and func1()
belong in additional files, which both im1 & im2 import
.
Tests should depend upon target code, not the other way around.
your answer is good but I am doing this in django like one is views.py and 2nd file is some services.py, so I want to do that thing only in views.py. And Second thing, importing a file instead of function may create performance problem because I have so many functions and so many things in views.py, by the way your answer is also very helpful thanks
– Nikhil Bhardwaj
Mar 26 at 15:59
Even for views & services, you still have an opportunity to evict common functions into some utility.py file. As far as elapsed time goes, theimport
happens just once when django starts up. Your code is small, relative to the other libraries you are importing, so absent timing results I wouldn't worry much about such overheads, they're not the dominant source of delay.
– J_H
Mar 26 at 16:46
but I tried that code which is given in answer, it is not working or I'm doing this wrong can u please check it once according to their answer
– Nikhil Bhardwaj
Mar 26 at 16:52
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%2f55357907%2fcalling-function-without-import-the-other-file-function-in-same-directory%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't do that.
The advice to "only import the module" works (How to avoid circular imports in Python?).
But you'd be better off putting your functions into more files that are arranged in a hierarchy.
In other words, break the cycle.
It will be beneficial for the organization of your code,
and of your unit tests,
and for how you think about your high level problem.
Here, the definitions of want_to_call()
and func1()
belong in additional files, which both im1 & im2 import
.
Tests should depend upon target code, not the other way around.
your answer is good but I am doing this in django like one is views.py and 2nd file is some services.py, so I want to do that thing only in views.py. And Second thing, importing a file instead of function may create performance problem because I have so many functions and so many things in views.py, by the way your answer is also very helpful thanks
– Nikhil Bhardwaj
Mar 26 at 15:59
Even for views & services, you still have an opportunity to evict common functions into some utility.py file. As far as elapsed time goes, theimport
happens just once when django starts up. Your code is small, relative to the other libraries you are importing, so absent timing results I wouldn't worry much about such overheads, they're not the dominant source of delay.
– J_H
Mar 26 at 16:46
but I tried that code which is given in answer, it is not working or I'm doing this wrong can u please check it once according to their answer
– Nikhil Bhardwaj
Mar 26 at 16:52
add a comment |
Don't do that.
The advice to "only import the module" works (How to avoid circular imports in Python?).
But you'd be better off putting your functions into more files that are arranged in a hierarchy.
In other words, break the cycle.
It will be beneficial for the organization of your code,
and of your unit tests,
and for how you think about your high level problem.
Here, the definitions of want_to_call()
and func1()
belong in additional files, which both im1 & im2 import
.
Tests should depend upon target code, not the other way around.
your answer is good but I am doing this in django like one is views.py and 2nd file is some services.py, so I want to do that thing only in views.py. And Second thing, importing a file instead of function may create performance problem because I have so many functions and so many things in views.py, by the way your answer is also very helpful thanks
– Nikhil Bhardwaj
Mar 26 at 15:59
Even for views & services, you still have an opportunity to evict common functions into some utility.py file. As far as elapsed time goes, theimport
happens just once when django starts up. Your code is small, relative to the other libraries you are importing, so absent timing results I wouldn't worry much about such overheads, they're not the dominant source of delay.
– J_H
Mar 26 at 16:46
but I tried that code which is given in answer, it is not working or I'm doing this wrong can u please check it once according to their answer
– Nikhil Bhardwaj
Mar 26 at 16:52
add a comment |
Don't do that.
The advice to "only import the module" works (How to avoid circular imports in Python?).
But you'd be better off putting your functions into more files that are arranged in a hierarchy.
In other words, break the cycle.
It will be beneficial for the organization of your code,
and of your unit tests,
and for how you think about your high level problem.
Here, the definitions of want_to_call()
and func1()
belong in additional files, which both im1 & im2 import
.
Tests should depend upon target code, not the other way around.
Don't do that.
The advice to "only import the module" works (How to avoid circular imports in Python?).
But you'd be better off putting your functions into more files that are arranged in a hierarchy.
In other words, break the cycle.
It will be beneficial for the organization of your code,
and of your unit tests,
and for how you think about your high level problem.
Here, the definitions of want_to_call()
and func1()
belong in additional files, which both im1 & im2 import
.
Tests should depend upon target code, not the other way around.
edited Mar 26 at 14:38
answered Mar 26 at 14:32
J_HJ_H
6,0361 gold badge9 silver badges24 bronze badges
6,0361 gold badge9 silver badges24 bronze badges
your answer is good but I am doing this in django like one is views.py and 2nd file is some services.py, so I want to do that thing only in views.py. And Second thing, importing a file instead of function may create performance problem because I have so many functions and so many things in views.py, by the way your answer is also very helpful thanks
– Nikhil Bhardwaj
Mar 26 at 15:59
Even for views & services, you still have an opportunity to evict common functions into some utility.py file. As far as elapsed time goes, theimport
happens just once when django starts up. Your code is small, relative to the other libraries you are importing, so absent timing results I wouldn't worry much about such overheads, they're not the dominant source of delay.
– J_H
Mar 26 at 16:46
but I tried that code which is given in answer, it is not working or I'm doing this wrong can u please check it once according to their answer
– Nikhil Bhardwaj
Mar 26 at 16:52
add a comment |
your answer is good but I am doing this in django like one is views.py and 2nd file is some services.py, so I want to do that thing only in views.py. And Second thing, importing a file instead of function may create performance problem because I have so many functions and so many things in views.py, by the way your answer is also very helpful thanks
– Nikhil Bhardwaj
Mar 26 at 15:59
Even for views & services, you still have an opportunity to evict common functions into some utility.py file. As far as elapsed time goes, theimport
happens just once when django starts up. Your code is small, relative to the other libraries you are importing, so absent timing results I wouldn't worry much about such overheads, they're not the dominant source of delay.
– J_H
Mar 26 at 16:46
but I tried that code which is given in answer, it is not working or I'm doing this wrong can u please check it once according to their answer
– Nikhil Bhardwaj
Mar 26 at 16:52
your answer is good but I am doing this in django like one is views.py and 2nd file is some services.py, so I want to do that thing only in views.py. And Second thing, importing a file instead of function may create performance problem because I have so many functions and so many things in views.py, by the way your answer is also very helpful thanks
– Nikhil Bhardwaj
Mar 26 at 15:59
your answer is good but I am doing this in django like one is views.py and 2nd file is some services.py, so I want to do that thing only in views.py. And Second thing, importing a file instead of function may create performance problem because I have so many functions and so many things in views.py, by the way your answer is also very helpful thanks
– Nikhil Bhardwaj
Mar 26 at 15:59
Even for views & services, you still have an opportunity to evict common functions into some utility.py file. As far as elapsed time goes, the
import
happens just once when django starts up. Your code is small, relative to the other libraries you are importing, so absent timing results I wouldn't worry much about such overheads, they're not the dominant source of delay.– J_H
Mar 26 at 16:46
Even for views & services, you still have an opportunity to evict common functions into some utility.py file. As far as elapsed time goes, the
import
happens just once when django starts up. Your code is small, relative to the other libraries you are importing, so absent timing results I wouldn't worry much about such overheads, they're not the dominant source of delay.– J_H
Mar 26 at 16:46
but I tried that code which is given in answer, it is not working or I'm doing this wrong can u please check it once according to their answer
– Nikhil Bhardwaj
Mar 26 at 16:52
but I tried that code which is given in answer, it is not working or I'm doing this wrong can u please check it once according to their answer
– Nikhil Bhardwaj
Mar 26 at 16:52
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55357907%2fcalling-function-without-import-the-other-file-function-in-same-directory%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
Possible duplicate of How to avoid circular imports in Python?
– ruddra
Mar 26 at 13:55
@ruddra thanx sir, can u please do it with my question, I'll be thankful for that.....
– Nikhil Bhardwaj
Mar 26 at 16:16