python - Using sys.path.insert without explicit absolute path -


new using sys.path enable module import directory, i'm sure noob question but:

is possible not have use full/explicit or absolute file path when using sys.path access python script in directory, instead, provide directory path that's local module file structure?

currently, have following directory structure:

mymodule/      name/          __init__.py      bin/          userinfo.py          __init__.py      docs/      setup.py      tests/          name_tests.py          __init__.py 

within setup.py file, have import userinfo.py script, asks users info during installation. in setup.py file, lines call userinfo.py script this:

import sys sys.path.insert(0, '/users/malvin/pythondev/projects/mymodule/bin') import userinfo  

this works fine, because know entire file path userinfo.py file is, doesn't work who's trying install module because (obviously) there's no way anticipate file path on user's system.

my question: there method wherein can import userinfo.py file (which lives in /bin folder) without having entire system file path (that goes way /users) ? in other words, i'd have like:

 import sys  sys.path.insert(0, 'mymodule/bin')  import userinfo 

but know doesn't work.

you can use dot (./) notation current working directory establish relative path.

for example:

(syspathinsert)macbook:syspathinsert joeyoung$ pwd /users/joeyoung/web/stackoverflow/syspathinsert (syspathinsert)macbook:syspathinsert joeyoung$ tree . ├── mymodule.py └── bin     ├── userinfo.py     └── userinfo.pyc  1 directory, 3 files (syspathinsert)macbook:syspathinsert joeyoung$ cat ./bin/userinfo.py def say_hello():     print "hello there"  (syspathinsert)macbook:syspathinsert joeyoung$ cat mymodule.py  import sys sys.path.insert(0, './bin') import userinfo  userinfo.say_hello() (syspathinsert)macbook:syspathinsert joeyoung$ python mymodule.py  hello there 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -