Python imports relative path

后端 未结 3 721
星月不相逢
星月不相逢 2021-02-04 05:10

I\'ve got a project where I would like to use some python classes located in other directories.

Example structure:

/dir
 +../subdirA
 +../subdirB
 +../my         


        
3条回答
  •  时光取名叫无心
    2021-02-04 05:31

    On dir structure as following:

    /dir
     +../subdirA
     +../subdirB
     +../mydir
    

    combining with linux command ln , we can make things a lot simpler:

    1. cd dir/mydir
    2. ln -s ../subdirA ./
    3. ln -s ../subdirB ./
    

    And, now if you want to import some_stuff from file: dir/subdirA/fileA.py into your file: dir/mydir/myfile.py, simply import like this:

    from subdirA.fileA import some_stuff
    

    And, the same applies to 'dir/subdirB'

    What's More, this works for setup.py process, :)

提交回复
热议问题