简体   繁体   English

dist-packages和sys.path

[英]dist-packages and sys.path

I have some code inside myapplication that look for some files inside myapplications' directories. 我在myapplication中有一些代码可以查找myapplications目录中的一些文件。 I'm working with AptanaStudio, and I see that my code run OK, but when I create the debian package and I install it in another computer the search is unsuccessful because sys.path look like a different list. 我正在使用AptanaStudio,我看到我的代码运行正常,但是当我创建debian软件包并将其安装在另一台计算机上时,搜索不成功,因为sys.path看起来像一个不同的列表。

From Aptana execution sys.path includes the path to the executable directory (/mysvncopy/myapplication) and I believe my code finds the files this way. 从Aptana执行sys.path包含可执行目录(/ mysvncopy / myapplication)的路径,我相信我的代码以这种方式查找文件。

The application's installation leave this files at /usr/share/pyshared/myapplication and I thought this directory is automatically accessible regards to /usr/local/lib/python2.6/dist-packages in sys.path , but something is wrong, /usr/local/lib/python2.6/dist-packages IS in sys.path , of course, but the application do not find anything inside /usr/share/pyshared/myapplication . 应用程序的安装将此文件保留在/ usr / share / pyshared / myapplication中 ,我认为这个目录可以自动访问sys.path中的 /usr/local/lib/python2.6/dist-packages ,但是有些错误, /当然,usr / local / lib / python2.6 / dist-packages IS在sys.path中,但应用程序在/ usr / share / pyshared / myapplication中找不到任何内容。

How can I assure the application know to look for inside /usr/share/pyshared/myapplication and equivalent inside windows and mac?. 我怎样才能确保应用程序知道在/ usr / share / pyshared / myapplication和windows和mac中等效的内部?

If I include in my code: 如果我在我的代码中包含:

<sys.path.append('/usr/share/pyshared/myapplication')

the search is succesfull, but this code is SO dependent. 搜索是成功的,但此代码依赖于SO。

I can paste setup.py if necessary. 如有必要,我可以粘贴setup.py.

Thanks 谢谢

I'm not familiar with using setup.py so this might not be useful, but.. 我不熟悉使用setup.py,所以这可能没用,但..

If you import both sys and os in the top of your installer you can do something like this: 如果您在安装程序的顶部导入sys和os,则可以执行以下操作:

if sys.getwindowsversion():
    <install to Windows dir>
elif os.system('uname -a'):
    ostest = os.popen('uname -a').split(' ')
    if str('Linux') in ostest:
        <install to Linux dir>

and so on. 等等。 I don't have a Mac so I don't know if uname -a returns anything in one, however if it does you can parse the ostest for something like OSX (which will most likely be in there somewhere). 我没有Mac所以我不知道uname -a是否会返回任何一个,但是如果它确实可以解析像OSX这样的东西(很可能会在某处)。 And have a separate file or set of files for each OS. 并为每个操作系统提供单独的文件或文件集。 Or even easier, after they install have some string in your original files called 'OSREPLACE' for example and then use something like this code continuing from the above portion after finding the os and assigning it as a variable: 或者甚至更容易,安装后在原始文件中有一些名为'OSREPLACE'的字符串,然后在找到操作系统并将其指定为变量后,继续使用上述部分中的代码:

...    
if os == 'linux':
    NEWSTRING = 'linuxpaths'
elif os == 'mac':
    NEWSTRING = 'macpaths'
elif os == 'windows':
    NEWSTRING = 'windowspaths'

for file in files:
        with open(str(file), 'r') as f:
            data = f.read()
            data = data.replace('OSREPLACE', 'NEWSTRING')
        with open(str(file), 'w') as f:
            f.write(data)       

If you're creating a debian package containing an application written in python, you should probably package it with pex ( https://github.com/pantsbuild/pex ) or cx_Freeze ( http://cx-freeze.sourceforge.net/ ) first. 如果你正在创建一个包含用python编写的应用程序的debian软件包,你应该用pex( https://github.com/pantsbuild/pex )或cx_Freeze( http://cx-freeze.sourceforge.net/ )打包它。 )首先。 That way, your application always runs :) 那样,你的应用程序总是运行:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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