Automatically fetching latest version of a file on import

霸气de小男生 提交于 2019-12-08 04:11:22

问题


I have a module that I want to keep up to date, and I'm wondering if this is a bad idea:

Have a module (mod1.py) in the site-packages directory that copies a different module from some other location into the site-packages directory, and then imports * from that module.

import shutil
from distutils.sysconfig import get_python_lib
p_source = r'\\SourceSafeServer\mod1_current.py'
p_local = get_python_lib() + r'\mod1_current.py'
shutil.copyfile(p_source, p_local)
from mod1_current import *

Now I can do this in any module, and it will always be the latest version:

from mod1 import function1

This works.... but is there a better way of doing this?

Update

Here is the current process... there is a project under source-control that has a single module: mod1.py There is also a setup.py Running setup.py copies mod1.py to the site-packages directory.

Developers that use the module must run setup.py to update the module. Sometimes, they don't and not having the latest version causes problems.

I want to be able to just check-in the a new version, and any code that imports that module will automatically grab the latest version every time, without anyone having to run setup.py


回答1:


In some cases, we put .pth files in the Python site-packages directory. The .pth files name our various SVN checkout directories.

No install. No copy.

.pth files are described here.




回答2:


Do you really want to do this? This means you could very easily roll code to a production app simply by committing to source control. I would consider this a nasty side-effect for someone who isn't aware of your setup.

That being said this seems like a pretty good solution - you may want to add some exception-handling around the network file calls as those are prone to failure.




回答3:


The original strategy of having other developers copy mod1.py into their site-packages in order to use the module sounds like it's the real problem. Why aren't they just using the same source control are you are?

This auto-copying will make it hard to do rollbacks, especially if other developers copy your strategy. Imagine this same system used for dozens and dozens of files. And then imagine you actually do want to use a version of mod1.py that is not the latest for something.



来源:https://stackoverflow.com/questions/632171/automatically-fetching-latest-version-of-a-file-on-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!