V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
rephaslife
V2EX  ›  Python

[Python]处于环境变量中的文件怎样import到非环境变量里的文件?

  •  
  •   rephaslife ·
    rephaslife · Mar 18, 2013 · 4365 views
    This topic created in 4799 days ago, the information mentioned may be changed or developed.
    正在写一项目,用setup.py安装后会有一新命令。

    而安装后实际的代码文件储存在/usr/local/lib/python2.7/dist-packages/下面。我要import本项目的文件的时候很正常。但是要import“[命令执行时]实际的工作位置”下的文件该怎么办?

    就像这样:
    /usr/local/lib/python2.7/dist-packages/xxoo.py 要import /home/xxoo/xxoo.py
    (后者不在环境变量里)

    我试过直接`import 文件名`但是不行,请问怎么解决这个问题?

    谢了!
    7 replies    1970-01-01 08:00:00 +08:00
    phuslu
        1
    phuslu  
       Mar 18, 2013
    rephaslife
        2
    rephaslife  
    OP
       Mar 18, 2013
    @phuslu 感觉为了import几个文件而特意弄出个服务器很浪费资源。不过还是谢谢XD
    phuslu
        3
    phuslu  
       Mar 18, 2013
    @rephaslife 汗,把urllib2 改成 with open(filename) as fp 也就是一行的事情。。。
    rephaslife
        4
    rephaslife  
    OP
       Mar 18, 2013
    @phuslu 原谅我一时头脑发烧。。
    lerry
        5
    lerry  
       Mar 18, 2013
    import sys
    sys.path.insert(0, '/home/xxoo')
    import xxoo
    rephaslife
        6
    rephaslife  
    OP
       Mar 18, 2013
    @lerry 谢谢,我最后用的是sys.path.append('/home/xxoo')
    lerry
        7
    lerry  
       Mar 18, 2013
    恩,一样的 @rephaslife
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4972 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 03:59 · PVG 11:59 · LAX 20:59 · JFK 23:59
    ♥ Do have faith in what you're doing.