简体   繁体   中英

How to use own python modules without rewriting all paths in Django

I am making a website with django that uses my python codes. my directory is like this:

\mysite
    \myApp
    \myCodes
        \common
        \deploy
        \src
           misc.py
              def Args():
                  # commands


    \mySite

my codes in /myCodes/src have a lines at the beginning like:

import sys

sys.path.append('../deploy')
sys.path.append('../common')

some codes in /common and /deploy also have the same lines and they work fine but when I use my codes in from the view.py they can't find each other, I added \myCodes as an app in settings.py and when I manually change the import dir ( import myCodes.common.myFunction ) it is fine but it seams to be unnecessary to manually change all of the imports, is there away I can execute the codes as they are?

Ps: I also use directory as input and open and write files in my functions that don't work (the are all in \myCodes directory)

I used os.path.dirname(__file__) +'/rest of the dir/' to give full path and it worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM