How to import a class into a python file from an outside directory?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm fairly new to python and currently struggling to do the simple thing of importing a file from an outside directory.
Let's consider two files :
// src/class/Rider ....
class Rider(object) :
def __init__(self, firstname, lastname, emailaddress, phonenumber, pickuplocation, dropofflocation, pickuptime, dropofftime):
self.FirstName = firstname
self.LastName = lastname
self.EmailAddress = emailaddress
self.PhoneNumber = phonenumber
self.PickUpLocation = pickuplocation
self.DropOffLocation = dropofflocation
self.PickUpTime = pickuptime
self.DropOffTime = dropofftime
def set_rider_name(self):
first = self.FirstName
last = self.LastName
return first + last
and
//src/method/rider
# importing Rider class in here ...
I'm struggling to import the Rider
class from src/class/Rider
into the src/method/rider
file so I can actually use that class.
Coming from node.js and being used to the import/export
of es6
, I'm still learning python 2.7
.
my root directory is something like : ../Golf-cart/src
, where I have class
and method
subdirectories inside src/
How can I import my class definition into my method file so I can actually use it there ?
python
add a comment |
I'm fairly new to python and currently struggling to do the simple thing of importing a file from an outside directory.
Let's consider two files :
// src/class/Rider ....
class Rider(object) :
def __init__(self, firstname, lastname, emailaddress, phonenumber, pickuplocation, dropofflocation, pickuptime, dropofftime):
self.FirstName = firstname
self.LastName = lastname
self.EmailAddress = emailaddress
self.PhoneNumber = phonenumber
self.PickUpLocation = pickuplocation
self.DropOffLocation = dropofflocation
self.PickUpTime = pickuptime
self.DropOffTime = dropofftime
def set_rider_name(self):
first = self.FirstName
last = self.LastName
return first + last
and
//src/method/rider
# importing Rider class in here ...
I'm struggling to import the Rider
class from src/class/Rider
into the src/method/rider
file so I can actually use that class.
Coming from node.js and being used to the import/export
of es6
, I'm still learning python 2.7
.
my root directory is something like : ../Golf-cart/src
, where I have class
and method
subdirectories inside src/
How can I import my class definition into my method file so I can actually use it there ?
python
Possible duplicate of Sibling package imports
– Thomas Lang
Nov 29 '18 at 5:32
This topic didn't really solved my problem since it not working ...
– Cobako
Nov 29 '18 at 5:55
Don't edit answers into the questions, as they invalidate the answers below. Instead, add an answer below, and mark it as accepted if it solves your issue.
– FrankerZ
Nov 29 '18 at 7:26
add a comment |
I'm fairly new to python and currently struggling to do the simple thing of importing a file from an outside directory.
Let's consider two files :
// src/class/Rider ....
class Rider(object) :
def __init__(self, firstname, lastname, emailaddress, phonenumber, pickuplocation, dropofflocation, pickuptime, dropofftime):
self.FirstName = firstname
self.LastName = lastname
self.EmailAddress = emailaddress
self.PhoneNumber = phonenumber
self.PickUpLocation = pickuplocation
self.DropOffLocation = dropofflocation
self.PickUpTime = pickuptime
self.DropOffTime = dropofftime
def set_rider_name(self):
first = self.FirstName
last = self.LastName
return first + last
and
//src/method/rider
# importing Rider class in here ...
I'm struggling to import the Rider
class from src/class/Rider
into the src/method/rider
file so I can actually use that class.
Coming from node.js and being used to the import/export
of es6
, I'm still learning python 2.7
.
my root directory is something like : ../Golf-cart/src
, where I have class
and method
subdirectories inside src/
How can I import my class definition into my method file so I can actually use it there ?
python
I'm fairly new to python and currently struggling to do the simple thing of importing a file from an outside directory.
Let's consider two files :
// src/class/Rider ....
class Rider(object) :
def __init__(self, firstname, lastname, emailaddress, phonenumber, pickuplocation, dropofflocation, pickuptime, dropofftime):
self.FirstName = firstname
self.LastName = lastname
self.EmailAddress = emailaddress
self.PhoneNumber = phonenumber
self.PickUpLocation = pickuplocation
self.DropOffLocation = dropofflocation
self.PickUpTime = pickuptime
self.DropOffTime = dropofftime
def set_rider_name(self):
first = self.FirstName
last = self.LastName
return first + last
and
//src/method/rider
# importing Rider class in here ...
I'm struggling to import the Rider
class from src/class/Rider
into the src/method/rider
file so I can actually use that class.
Coming from node.js and being used to the import/export
of es6
, I'm still learning python 2.7
.
my root directory is something like : ../Golf-cart/src
, where I have class
and method
subdirectories inside src/
How can I import my class definition into my method file so I can actually use it there ?
python
python
edited Nov 29 '18 at 7:25
FrankerZ
17.9k73067
17.9k73067
asked Nov 29 '18 at 5:31
CobakoCobako
226
226
Possible duplicate of Sibling package imports
– Thomas Lang
Nov 29 '18 at 5:32
This topic didn't really solved my problem since it not working ...
– Cobako
Nov 29 '18 at 5:55
Don't edit answers into the questions, as they invalidate the answers below. Instead, add an answer below, and mark it as accepted if it solves your issue.
– FrankerZ
Nov 29 '18 at 7:26
add a comment |
Possible duplicate of Sibling package imports
– Thomas Lang
Nov 29 '18 at 5:32
This topic didn't really solved my problem since it not working ...
– Cobako
Nov 29 '18 at 5:55
Don't edit answers into the questions, as they invalidate the answers below. Instead, add an answer below, and mark it as accepted if it solves your issue.
– FrankerZ
Nov 29 '18 at 7:26
Possible duplicate of Sibling package imports
– Thomas Lang
Nov 29 '18 at 5:32
Possible duplicate of Sibling package imports
– Thomas Lang
Nov 29 '18 at 5:32
This topic didn't really solved my problem since it not working ...
– Cobako
Nov 29 '18 at 5:55
This topic didn't really solved my problem since it not working ...
– Cobako
Nov 29 '18 at 5:55
Don't edit answers into the questions, as they invalidate the answers below. Instead, add an answer below, and mark it as accepted if it solves your issue.
– FrankerZ
Nov 29 '18 at 7:26
Don't edit answers into the questions, as they invalidate the answers below. Instead, add an answer below, and mark it as accepted if it solves your issue.
– FrankerZ
Nov 29 '18 at 7:26
add a comment |
2 Answers
2
active
oldest
votes
You can create the empty file __init__.py
in both directories /src/class/__init__.py
and /src/method/__init__.py
.
Then you just import like
#/src/method/rider.py
import class.Rider
...
That's it. Hope it help.
so where should I putimport class.Rider
? in/src/method/
? That where I need the class to be called.
– Cobako
Nov 29 '18 at 6:14
Yes,/src/method/rider.py
as your mention.
– Lê Tư Thành
Nov 29 '18 at 6:16
and what should I put int__init__
?
– Cobako
Nov 29 '18 at 6:16
Nothing, just create the blank file.
– Lê Tư Thành
Nov 29 '18 at 6:17
@Cobako, is that work now?
– Lê Tư Thành
Nov 29 '18 at 6:31
|
show 2 more comments
You might want to use sys
to add the directory to the system path, then you can import the file directly.
import sys
sys.path.insert(0, 'system/path/to/Rider')
import Rider
I still haveno module to Rider ...
– Cobako
Nov 29 '18 at 6:08
how did you type the system path to insert?
– Chuck G
Nov 29 '18 at 6:12
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%2f53532443%2fhow-to-import-a-class-into-a-python-file-from-an-outside-directory%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
You can create the empty file __init__.py
in both directories /src/class/__init__.py
and /src/method/__init__.py
.
Then you just import like
#/src/method/rider.py
import class.Rider
...
That's it. Hope it help.
so where should I putimport class.Rider
? in/src/method/
? That where I need the class to be called.
– Cobako
Nov 29 '18 at 6:14
Yes,/src/method/rider.py
as your mention.
– Lê Tư Thành
Nov 29 '18 at 6:16
and what should I put int__init__
?
– Cobako
Nov 29 '18 at 6:16
Nothing, just create the blank file.
– Lê Tư Thành
Nov 29 '18 at 6:17
@Cobako, is that work now?
– Lê Tư Thành
Nov 29 '18 at 6:31
|
show 2 more comments
You can create the empty file __init__.py
in both directories /src/class/__init__.py
and /src/method/__init__.py
.
Then you just import like
#/src/method/rider.py
import class.Rider
...
That's it. Hope it help.
so where should I putimport class.Rider
? in/src/method/
? That where I need the class to be called.
– Cobako
Nov 29 '18 at 6:14
Yes,/src/method/rider.py
as your mention.
– Lê Tư Thành
Nov 29 '18 at 6:16
and what should I put int__init__
?
– Cobako
Nov 29 '18 at 6:16
Nothing, just create the blank file.
– Lê Tư Thành
Nov 29 '18 at 6:17
@Cobako, is that work now?
– Lê Tư Thành
Nov 29 '18 at 6:31
|
show 2 more comments
You can create the empty file __init__.py
in both directories /src/class/__init__.py
and /src/method/__init__.py
.
Then you just import like
#/src/method/rider.py
import class.Rider
...
That's it. Hope it help.
You can create the empty file __init__.py
in both directories /src/class/__init__.py
and /src/method/__init__.py
.
Then you just import like
#/src/method/rider.py
import class.Rider
...
That's it. Hope it help.
edited Nov 29 '18 at 6:23
answered Nov 29 '18 at 6:07
Lê Tư ThànhLê Tư Thành
86111
86111
so where should I putimport class.Rider
? in/src/method/
? That where I need the class to be called.
– Cobako
Nov 29 '18 at 6:14
Yes,/src/method/rider.py
as your mention.
– Lê Tư Thành
Nov 29 '18 at 6:16
and what should I put int__init__
?
– Cobako
Nov 29 '18 at 6:16
Nothing, just create the blank file.
– Lê Tư Thành
Nov 29 '18 at 6:17
@Cobako, is that work now?
– Lê Tư Thành
Nov 29 '18 at 6:31
|
show 2 more comments
so where should I putimport class.Rider
? in/src/method/
? That where I need the class to be called.
– Cobako
Nov 29 '18 at 6:14
Yes,/src/method/rider.py
as your mention.
– Lê Tư Thành
Nov 29 '18 at 6:16
and what should I put int__init__
?
– Cobako
Nov 29 '18 at 6:16
Nothing, just create the blank file.
– Lê Tư Thành
Nov 29 '18 at 6:17
@Cobako, is that work now?
– Lê Tư Thành
Nov 29 '18 at 6:31
so where should I put
import class.Rider
? in /src/method/
? That where I need the class to be called.– Cobako
Nov 29 '18 at 6:14
so where should I put
import class.Rider
? in /src/method/
? That where I need the class to be called.– Cobako
Nov 29 '18 at 6:14
Yes,
/src/method/rider.py
as your mention.– Lê Tư Thành
Nov 29 '18 at 6:16
Yes,
/src/method/rider.py
as your mention.– Lê Tư Thành
Nov 29 '18 at 6:16
and what should I put int
__init__
?– Cobako
Nov 29 '18 at 6:16
and what should I put int
__init__
?– Cobako
Nov 29 '18 at 6:16
Nothing, just create the blank file.
– Lê Tư Thành
Nov 29 '18 at 6:17
Nothing, just create the blank file.
– Lê Tư Thành
Nov 29 '18 at 6:17
@Cobako, is that work now?
– Lê Tư Thành
Nov 29 '18 at 6:31
@Cobako, is that work now?
– Lê Tư Thành
Nov 29 '18 at 6:31
|
show 2 more comments
You might want to use sys
to add the directory to the system path, then you can import the file directly.
import sys
sys.path.insert(0, 'system/path/to/Rider')
import Rider
I still haveno module to Rider ...
– Cobako
Nov 29 '18 at 6:08
how did you type the system path to insert?
– Chuck G
Nov 29 '18 at 6:12
add a comment |
You might want to use sys
to add the directory to the system path, then you can import the file directly.
import sys
sys.path.insert(0, 'system/path/to/Rider')
import Rider
I still haveno module to Rider ...
– Cobako
Nov 29 '18 at 6:08
how did you type the system path to insert?
– Chuck G
Nov 29 '18 at 6:12
add a comment |
You might want to use sys
to add the directory to the system path, then you can import the file directly.
import sys
sys.path.insert(0, 'system/path/to/Rider')
import Rider
You might want to use sys
to add the directory to the system path, then you can import the file directly.
import sys
sys.path.insert(0, 'system/path/to/Rider')
import Rider
answered Nov 29 '18 at 6:02
Chuck GChuck G
14419
14419
I still haveno module to Rider ...
– Cobako
Nov 29 '18 at 6:08
how did you type the system path to insert?
– Chuck G
Nov 29 '18 at 6:12
add a comment |
I still haveno module to Rider ...
– Cobako
Nov 29 '18 at 6:08
how did you type the system path to insert?
– Chuck G
Nov 29 '18 at 6:12
I still have
no module to Rider ...
– Cobako
Nov 29 '18 at 6:08
I still have
no module to Rider ...
– Cobako
Nov 29 '18 at 6:08
how did you type the system path to insert?
– Chuck G
Nov 29 '18 at 6:12
how did you type the system path to insert?
– Chuck G
Nov 29 '18 at 6:12
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%2f53532443%2fhow-to-import-a-class-into-a-python-file-from-an-outside-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
Possible duplicate of Sibling package imports
– Thomas Lang
Nov 29 '18 at 5:32
This topic didn't really solved my problem since it not working ...
– Cobako
Nov 29 '18 at 5:55
Don't edit answers into the questions, as they invalidate the answers below. Instead, add an answer below, and mark it as accepted if it solves your issue.
– FrankerZ
Nov 29 '18 at 7:26