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
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.
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.
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