Python - Include another Python script
I have a question. When you are programming in PHP you can use this to include external php script to current script.
include('test_page.php');
So you don't have to repeat a code in every script. Is there a way in Python to include another Python script, just like the php page?
php python include
add a comment |
I have a question. When you are programming in PHP you can use this to include external php script to current script.
include('test_page.php');
So you don't have to repeat a code in every script. Is there a way in Python to include another Python script, just like the php page?
php python include
you can useimport
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:35
Thats for modules, or does it also work with scripts?
– Vreesie
Nov 24 '18 at 10:35
modules are just fancy scripts.import xyruns all code inxy.py, including but not limited to: creating methods, creating variables, method calls, etc
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:37
so I have to use for example:import test_script
– Vreesie
Nov 24 '18 at 10:38
1
aw, arbazz was faster than me with answering :p
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:38
add a comment |
I have a question. When you are programming in PHP you can use this to include external php script to current script.
include('test_page.php');
So you don't have to repeat a code in every script. Is there a way in Python to include another Python script, just like the php page?
php python include
I have a question. When you are programming in PHP you can use this to include external php script to current script.
include('test_page.php');
So you don't have to repeat a code in every script. Is there a way in Python to include another Python script, just like the php page?
php python include
php python include
edited Nov 24 '18 at 11:12
Arbazz Hussain
144114
144114
asked Nov 24 '18 at 10:34
VreesieVreesie
6212
6212
you can useimport
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:35
Thats for modules, or does it also work with scripts?
– Vreesie
Nov 24 '18 at 10:35
modules are just fancy scripts.import xyruns all code inxy.py, including but not limited to: creating methods, creating variables, method calls, etc
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:37
so I have to use for example:import test_script
– Vreesie
Nov 24 '18 at 10:38
1
aw, arbazz was faster than me with answering :p
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:38
add a comment |
you can useimport
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:35
Thats for modules, or does it also work with scripts?
– Vreesie
Nov 24 '18 at 10:35
modules are just fancy scripts.import xyruns all code inxy.py, including but not limited to: creating methods, creating variables, method calls, etc
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:37
so I have to use for example:import test_script
– Vreesie
Nov 24 '18 at 10:38
1
aw, arbazz was faster than me with answering :p
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:38
you can use
import– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:35
you can use
import– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:35
Thats for modules, or does it also work with scripts?
– Vreesie
Nov 24 '18 at 10:35
Thats for modules, or does it also work with scripts?
– Vreesie
Nov 24 '18 at 10:35
modules are just fancy scripts.
import xy runs all code in xy.py, including but not limited to: creating methods, creating variables, method calls, etc– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:37
modules are just fancy scripts.
import xy runs all code in xy.py, including but not limited to: creating methods, creating variables, method calls, etc– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:37
so I have to use for example:
import test_script– Vreesie
Nov 24 '18 at 10:38
so I have to use for example:
import test_script– Vreesie
Nov 24 '18 at 10:38
1
1
aw, arbazz was faster than me with answering :p
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:38
aw, arbazz was faster than me with answering :p
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:38
add a comment |
2 Answers
2
active
oldest
votes
In Python,
you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.
If modulename.py is an function
import modulename
if modulename.py is a set a different functions.
from modulename import particularfunction
add a comment |
If you have "script.py", just make:
import script
If you have a particular function or a class in the script then:
from script import function
Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:
import sys
sys.path.append("PATH_TO_SCRIPT")
import script
If I want to make a database connection in an include, I get the error:if not self._cnx: ReferenceError: weakly-referenced object no longer exists
– Vreesie
Nov 24 '18 at 10:58
What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.
– artona
Nov 24 '18 at 11:09
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%2f53457258%2fpython-include-another-python-script%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 Python,
you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.
If modulename.py is an function
import modulename
if modulename.py is a set a different functions.
from modulename import particularfunction
add a comment |
In Python,
you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.
If modulename.py is an function
import modulename
if modulename.py is a set a different functions.
from modulename import particularfunction
add a comment |
In Python,
you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.
If modulename.py is an function
import modulename
if modulename.py is a set a different functions.
from modulename import particularfunction
In Python,
you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.
If modulename.py is an function
import modulename
if modulename.py is a set a different functions.
from modulename import particularfunction
answered Nov 24 '18 at 10:38
Arbazz HussainArbazz Hussain
144114
144114
add a comment |
add a comment |
If you have "script.py", just make:
import script
If you have a particular function or a class in the script then:
from script import function
Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:
import sys
sys.path.append("PATH_TO_SCRIPT")
import script
If I want to make a database connection in an include, I get the error:if not self._cnx: ReferenceError: weakly-referenced object no longer exists
– Vreesie
Nov 24 '18 at 10:58
What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.
– artona
Nov 24 '18 at 11:09
add a comment |
If you have "script.py", just make:
import script
If you have a particular function or a class in the script then:
from script import function
Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:
import sys
sys.path.append("PATH_TO_SCRIPT")
import script
If I want to make a database connection in an include, I get the error:if not self._cnx: ReferenceError: weakly-referenced object no longer exists
– Vreesie
Nov 24 '18 at 10:58
What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.
– artona
Nov 24 '18 at 11:09
add a comment |
If you have "script.py", just make:
import script
If you have a particular function or a class in the script then:
from script import function
Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:
import sys
sys.path.append("PATH_TO_SCRIPT")
import script
If you have "script.py", just make:
import script
If you have a particular function or a class in the script then:
from script import function
Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:
import sys
sys.path.append("PATH_TO_SCRIPT")
import script
answered Nov 24 '18 at 10:38
artonaartona
71247
71247
If I want to make a database connection in an include, I get the error:if not self._cnx: ReferenceError: weakly-referenced object no longer exists
– Vreesie
Nov 24 '18 at 10:58
What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.
– artona
Nov 24 '18 at 11:09
add a comment |
If I want to make a database connection in an include, I get the error:if not self._cnx: ReferenceError: weakly-referenced object no longer exists
– Vreesie
Nov 24 '18 at 10:58
What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.
– artona
Nov 24 '18 at 11:09
If I want to make a database connection in an include, I get the error:
if not self._cnx: ReferenceError: weakly-referenced object no longer exists – Vreesie
Nov 24 '18 at 10:58
If I want to make a database connection in an include, I get the error:
if not self._cnx: ReferenceError: weakly-referenced object no longer exists – Vreesie
Nov 24 '18 at 10:58
What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.
– artona
Nov 24 '18 at 11:09
What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.
– artona
Nov 24 '18 at 11:09
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%2f53457258%2fpython-include-another-python-script%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
you can use
import– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:35
Thats for modules, or does it also work with scripts?
– Vreesie
Nov 24 '18 at 10:35
modules are just fancy scripts.
import xyruns all code inxy.py, including but not limited to: creating methods, creating variables, method calls, etc– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:37
so I have to use for example:
import test_script– Vreesie
Nov 24 '18 at 10:38
1
aw, arbazz was faster than me with answering :p
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:38