是否可以选择在Windows中启动进程时使用的清单?(Is it possible to choose a manifest to use when starting a process in windows?)
我试图提供一个COM对象,可以用来替换我无法控制的可执行文件中的对象。 我已经使用所有相同的接口创建了这个COM对象的实现。 我希望能够偶尔使用这个替换,所以我不希望它的注册替换原始对象。
我可以通过修改可执行文件的清单来查看com对象而不是通过注册表查找原始文件,从而使用reg-free COM来获取可执行文件以加载我的COM对象而不是原始对象。
到现在为止还挺好。 但这样做需要修改可执行文件中的清单文件。 我不希望此更改是永久性的,并且由于可执行文件通常安装在“程序文件”中,因此还需要管理员权限才能更改此文件。
理想情况下,我希望保持原始可执行文件不变。 然后在需要时,我创建的单独的启动程序将使用重定向到我的com对象而不是可执行文件中的原始清单文件的清单来加载所请求的可执行文件。
这样的事情可能吗? 或者也许是一种更好的方式来做我想做的事情?
注意:我可能无法制作可执行文件的副本,并且在我具有写入权限的位置更改了清单,因为我事先并不知道此可执行文件的所有依赖项将是什么(它可能需要来自相同的目录,或子目录中的数据文件)。 我可以将修改后的可执行文件的工作目录设置为原始可执行文件的目录,但如果可执行文件根据其文件位置执行任何操作,则会中断。
I am trying to provide a COM object that can be used as a replacement for an object in an executable that I do not have control of. I have created an implementation of this COM object with all the same interfaces. I want to be able to use this replacement only occasionally so I do not want its registration to replace the original object.
I can get the executable to load my COM object instead of the original using reg-free COM by modifying the manifest of the executable to look in my dll to find the com object instead of finding to original through the registry.
So far so good. But doing this requires modifying the manifest file in the executable. I do not want this change to be permanent, and since the executable is usually installed in "program files" it would also require administrator privileges to change this file.
Ideally what I would like is to leave the original executable unchanged. Then when needed a separate launcher program I created would load the requested executable using the manifest that redirects to my com object instead of the original manifest file in the executable.
Is something like this possible? Or maybe a better way of doing what I am trying to do?
Note: I probably can't make a duplicate copy of the executable with the manifest changed in a location I have write access to because I do not know ahead of time what all the dependencies of this executable will be (it may need dlls from the same directory, or data files from a sub-directory). I could set the working directory of the modified executable to be the directory of the original executable but if the executable does anything based off of its file location it would break.
原文:https://stackoverflow.com/questions/18671147
最满意答案
问题是这一行:
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'
当你的脚本在
myproj/
之外时,你的PYTHON_PATH在myproj/
之外启动,因此myproj
是一个有效的包。当你的脚本在
myproj/
,你的PYTHON_PATH在myproj/
启动,因此myproj
不再是一个有效的包。修复方法是从脚本中明确设置PYTHON_PATH。
然而
你应该做的是将你的脚本变成一个管理命令,这样你就可以运行
python manage.py myscript
并确保Django 在运行你的代码之前已经完成了所有必要的初始化和检查。The problem is this line:
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'
When your script is outside
myproj/
, then your PYTHON_PATH starts outsidemyproj/
, and hencemyproj
is a valid package.When your script is inside
myproj/
, then your PYTHON_PATH starts insidemyproj/
, and hencemyproj
is no longer a valid package.The fix would be to explicitly setup your PYTHON_PATH from inside the script.
HOWEVER
What you should do instead is turn your script into a management command so that you can run
python manage.py myscript
and be sure that Django has done all the necessary initialisation and checking before it runs your code.
相关问答
更多-
newaly生产django + apache + mod_wsgi上的ImportError(ImportError on newaly production django + apache + mod_wsgi)[2021-11-02]
让它工作,需要添加项目名称作为前缀。 from projectname.profile.forms importgot it to work, needed to add project name as prefix. from projectname.profile.forms import -
django外部脚本ImportError:没有名为Utils的模块(django external script ImportError: No module named Utils)[2022-07-18]
email.Utils是一个python标准库模块。 你有一个名为email的模块与它有冲突吗? 进入shell并输入 import email print email.__file__ # where is this "email" module? # make sure it's not one of yours... email.Utils is a python standard library module. Do you have a module called email that's c ... -
您是否已在INSTALLED_APPS下的settings.py中注册了应用“列表”? 还可以尝试使用迁移模型 python manage.py migrate 这种方法似乎是正确的,它是您使用csv模块的标准方法。 您可以使用追溯编辑问题,以便详细了解您的问题。 First, load your django settings in a similar manner as in your manage.py file. In this scenario, "lists" is the project na ...
-
更新: 正如rentgeeen已经发现的那样,问题出在django-rosetta 0.7.4版本上。 见下面的评论。 看起来你没有安装django-rosetta。 跑 pip install django-rosetta 从控制台。 那应该安装django-rosetta和它的依赖(六,请求和polib)。 在你安装django-rosetta的远程机会,但不是polib,运行 pip install polib 作为旁注,你可能想看看virtualenv教程(对于Webfaction或其他: ht ...
-
问题是这一行: os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings' 当你的脚本在myproj/之外时,你的PYTHON_PATH在myproj/之外启动,因此myproj是一个有效的包。 当你的脚本在myproj/ ,你的PYTHON_PATH在myproj/启动,因此myproj不再是一个有效的包。 修复方法是从脚本中明确设置PYTHON_PATH。 然而 你应该做的是将你的脚本变成一个管理命令,这样你就可以运行python manage.py ...
-
Django在脚本中执行类(Django execute class in script)[2022-07-14]
您的脚本必须位于应用程序的文件夹管理/命令/中。 脚本示例: # -*- coding: utf-8 -*- # example.py from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **options): print "Hello word!!" 要运行脚本: ./manage.py example 这里有文档http ... -
为了使Django项目能够将设置作为tango_with_django_project.settings , wad2需要位于Python路径中。 最简单的解决方法是将populate_rango.py从wad2/tango_with_django_project/到wad2 。 或者,您可以将父目录添加到populate_rango.py的python路径 import sys sys.path.append('..') import os os.environ.setdefault('DJANGO_S ...
-
Django / Python无法从脚本导入模块,但可以从IDE导入(Django/Python cannot import module from script, but can import from IDE)[2023-10-29]
好吧,看起来你的模块安排得很奇怪,所以你需要在sys.paths中添加更多的路径。 sys.path.append('/tools/envs/networktools/nettools/scripts/python/') 这一切都取决于你从哪个目录开始翻译。 我假设您正在从我正在添加到上面的sys.path的python目录中打开IDE中的解释器,这将把pingsweep模块直接放在您的路径上。 您的脚本是从nettools目录开始的。 如果你想从那里访问pingsweep ,你需要说: from scr ... -
会发生什么是每个目录下的人不知道其他目录。 导入无法在目录层次结构中查找。 你可以做的是教Python在哪里寻找库 export PYTHONPATH=$PYTHONPATH:/path/to/project 不过,这是“低级别”的方式。 应该有适当的设置来解决这个问题。 What happens is that the guys under each directory don't know about the other directories. Importing cannot look up in ...
-
尝试使用 #!/usr/bin/env python 作为shebang。 如果这不起作用,请尝试查看which python的输出。 Try using #!/usr/bin/env python as the shebang. If that does not work, try seeing what is the output of which python.