开发者

Nested .pth Files or Loading Extra site-dirs from a Network

开发者 https://www.devze.com 2023-03-17 14:23 出处:网络
The goal is to have a universal directory where I can add packages on the network that are automatically added to sys.path without needing to run site.addsitedir or sys.path.append each time I import

The goal is to have a universal directory where I can add packages on the network that are automatically added to sys.path without needing to run site.addsitedir or sys.path.append each time I import said packages. Is there a way to do this?

Background: I have a small network of users who all need access to the same scripts. Every time I want to add a new package for them to use, I add the path to their PYTHONPATH environment variable. The user base has started to grow, and so have the number of packages.

If I could set up a master.pth on the network that is loaded when they start any of the scripts (without requiring extra code in all of them), I'd be very grateful. It appears, though, that you can't nest .pth files, so simply adding a pointer .pth to the master.pth directory doesn't seem to work.

Edit: Regarding Comments from @S.Lott (was a little big for a comment): Take, for example, wxPython. Three objects are placed into the site-packages directory: wxversion.py, a directory called wx-2.8-msw-unicode, and a .pth file. The .pth file points to that directory, which contains all of the importable packages (wx, wxPython, etc.). I currently have three other packages that are structured in a similar wa开发者_JS百科y, one of which has several base modules. I would need to move all of the importable modules into the same directory to get the desired result.

If, however, .pth files could be "nested", I could add all of these directories to master.pth, keeping a relatively clean folder. I hope that makes sense, and thanks for your help!


You have a couple options:

  • modify your site.py file -- look for the PREFIXES list and add your network path to it:
  • Have one custom .pth file in each machine's site-packages folder with the following line:

    import sys; sys.path.append('/network/path/to/modules_and_packages')
    

The downside to using site.py is that when you upgrade to a different python you'll have to find and adjust all the site.pys on all the machines. Using the custom .pth file it's a simple matter of copying into the new 'site-packages' folders.

Both solutions allow you to have the network location set up just like the normal site-packages folder. For example:

m:\python_site_packages\   # network drive and folder
   |
   |- dbf.py  # for accessing dBase III and VFP .dbf files
   |
   |- web\
       |- __init__.py
       |- application.py
       |- (etc)

Just a reminder -- if the server becomes unavailable, so will all the network packages.

0

精彩评论

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