开发者

How can I call python module inside versioned package folder?

开发者 https://www.devze.com 2022-12-27 05:53 出处:网络
I need write python codes which run inside a host application. The python codes should be deployed under a specific folder of the host application. I must put my entry python module under the root of

I need write python codes which run inside a host application. The python codes should be deployed under a specific folder of the host application. I must put my entry python module under the root of the specific folder. And I want put all my other python codes and c/c++ dll under a sub folder, I prefer to name the sub folder like XXX-1.0, the number is the version of my pytho开发者_如何学Gon codes. The entry python module is just simple call a python module under the sub-folder.

By this way different version python codes can be deployed together without collision. May I know it is possible or not? Thanks.


I am not sure that I understand your question correctly, but here is a simple way to have several package versions without collisions.

A directory structure:

C:\tmp\eggs>dir /B /S
C:\tmp\eggs\libs
C:\tmp\eggs\test.py
C:\tmp\eggs\libs\foo-1.0.egg
C:\tmp\eggs\libs\foo-2.0.egg
C:\tmp\eggs\libs\foo-1.0.egg\foo.py
C:\tmp\eggs\libs\foo-2.0.egg\foo.py

Now the contents of files:

# contents of C:\tmp\eggs\libs\foo-1.0.egg\foo.py
version=(1,0)

# contents of C:\tmp\eggs\libs\foo-2.0.egg\foo.py
version=(2,0)

#contents of C:\tmp\eggs\test.py:

import sys
sys.path.insert(1, 'libs')
from pkg_resources import require

require('foo<1.5')
import foo
print foo.version
# will output (1,0)

If you change 'foo<1.5' to 'foo>1.5', or 'foo' output will change to (2,0)

Details you will find in setuptools documentation.


If you created a .pth file, eg., X.pth and put XXX-1.0 inside as content

XXX-1.0\
        - xxx.py
X.pth

Then, you could import xxx

Note: only tested on site-packages folder, I am not sure you could put your sub folder anywhere.

Edit: For example, wxPython do that way, since it can have multiple version on same machine.

wx-2.8-msw-unicode \
                    wx
                      \
                       more stuff
wx.pth (wx-2.8-msw-unicode)


Here's how I've done it:

tools
  |-- packageA
        |-- packageA-1.0
              |-- packageA
                    |-- modules
        |-- packageA-2.0
              |-- packageA
                    |-- modules
  |-- packageB
        ...

This way, when you want to upgrade, just add /packageA/packageA-2.0 to the PYTHONPATH and you can still do import packageA.

0

精彩评论

暂无评论...
验证码 换一张
取 消