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;
}







1















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 ?










share|improve this question

























  • 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


















1















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 ?










share|improve this question

























  • 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














1












1








1








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 ?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















0














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.






share|improve this answer


























  • 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











  • 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



















2














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





share|improve this answer
























  • 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












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
});


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer


























  • 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











  • 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
















0














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.






share|improve this answer


























  • 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











  • 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














0












0








0







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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











  • 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













  • 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













2














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





share|improve this answer
























  • 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
















2














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





share|improve this answer
























  • 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














2












2








2







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 29 '18 at 6:02









Chuck GChuck G

14419




14419













  • 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



















  • 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

















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)