bash 在 pyinstaller 中使用备用的 /tmp 位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18841334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using an alternate /tmp location with pyinstaller
提问by rahuL
I have a Python script which I then ran through pyinstaller2.0 to generate its binary.
我有一个 Python 脚本,然后我运行 pyinstaller2.0 来生成它的二进制文件。
python pyinstaller -F /path/to/python/script
While running the binary, it uses the /tmp folder by default to save it's temporary files and run the installer. This works fine on normal servers and VPSes. However, when an install is attempted on a server where /tmp is disabled (/tmp noexec
), the installation fails.
在运行二进制文件时,它默认使用 /tmp 文件夹来保存它的临时文件并运行安装程序。这在普通服务器和 VPS 上运行良好。但是,当在禁用 /tmp ( /tmp noexec
)的服务器上尝试安装时,安装会失败。
My questions are as follows:
我的问题如下:
- How would I enable another temporary location when the binary is run? does pyinstaller have such an option?
- If I write the code to create a temp location and export it to the PATH inside the python script, won't it anyway try to use default /tmp to run the python binary anyway?
- Another method I thought of was to write a shell script in bash which creates a temp location, exports its PATH, then calls the python binary and after it has run , remove the location of temp from PATH, and delete the temp folder - but this seems like a roundabout way to get it done. Is there a better solution?
- 运行二进制文件时如何启用另一个临时位置?pyinstaller 有这样的选项吗?
- 如果我编写代码来创建一个临时位置并将其导出到 python 脚本内的 PATH 中,无论如何它不会尝试使用默认的 /tmp 来运行 python 二进制文件吗?
- 我想到的另一种方法是在 bash 中编写一个 shell 脚本,它创建一个临时位置,导出它的 PATH,然后调用 python 二进制文件,并在它运行后,从 PATH 中删除临时的位置,并删除临时文件夹 - 但这似乎是完成它的一种迂回方式。有更好的解决方案吗?
采纳答案by rahuL
The solution as suggested by @devnull was indeed to make changes in pyinstaller's script. The script had the temporary location hardcoded so I made changes there. So here are the steps followed:
@devnull 建议的解决方案确实是在 pyinstaller 的脚本中进行更改。该脚本对临时位置进行了硬编码,因此我在那里进行了更改。所以这里是遵循的步骤:
- Under the pyinstaller folder, look for the
launch.c
file under/path/to/pyinstaller/sources/common
- Look for a function called
int getTempPath(char *buff)
- Under it, delete references to the
static const char *envname[]
(which are, it's declaration and onefor
loop within the same function) - Change the values for
static const char *dirname[]
to to the values which you want.
- 在 pyinstaller 文件夹下,查找下的
launch.c
文件/path/to/pyinstaller/sources/common
- 寻找一个名为
int getTempPath(char *buff)
- 在它下面,删除对
static const char *envname[]
(即,它的声明和for
同一函数中的一个循环)的引用 static const char *dirname[]
将 to 的值更改为您想要的值。
The function thus, looks like so:
因此,该函数看起来像这样:
int getTempPath(char *buff)
{
static const char *dirname[] = {
"/usr/local/src/temp", "/usr/local/src", "/usr/src", 0
};
int i;
char *p;
for ( i=0; dirname[i]; i++ ) {
strcpy(buff, dirname[i]);
if (testTempPath(buff))
return 1;
}
return 0;
}
Recompile the pyinstaller sources
using the following command:
sources
使用以下命令重新编译 pyinstaller :
python ./waf configure --no-lsb build install
python ./waf configure --no-lsb build install
To run this, first install python-devel
packages (yum install python-devel -y
) else it throws and error that Python.h isn't found
要运行它,首先安装python-devel
包 ( yum install python-devel -y
) 否则它会抛出和错误Python.h isn't found
Now when we run the python script through pyinstaller, the new temp local is used. Thanks to @devnull for pointing me in the right direction.
现在,当我们通过 pyinstaller 运行 python 脚本时,将使用新的临时本地。感谢@devnull 为我指明了正确的方向。
回答by CONvid19
Since PyInstaller
V3.3 (2017-09-21)you can use the --runtime-tmpdirargument to change the default extracting path, i.e.:
从PyInstaller
V3.3 (2017-09-21) 开始,您可以使用--runtime-tmpdir参数来更改默认提取路径,即:
--runtime-tmpdir PATH
Where to extract libraries and support files in onefile-mode. If this option is given, the
bootloader
will ignore any temp-folder location defined by the run-time OS. The_MEIxxxxxx-folder
will be created here. Please use this option only if you know what you are doing.
在 onefile-mode 中提取库和支持文件的位置。如果给出此选项,
bootloader
则将忽略运行时操作系统定义的任何临时文件夹位置。该_MEIxxxxxx-folder
会在这里产生。请仅在您知道自己在做什么时才使用此选项。
回答by CantankerousBullMoose
@devnull was on the right track. The missing piece is that those environment variables are resolved when the installer binary is run on the target machine, not when the installer is compiled on the development machine.
@devnull 走在正确的轨道上。缺少的部分是这些环境变量在目标机器上运行安装程序二进制文件时解析,而不是在开发机器上编译安装程序时解析。
A two line shell script that changes the TMPDIR environment variable (or %TEMP% in Windows) to somewhere you can run code from and then calls the binary you compiled should get the job done.
一个两行的 shell 脚本将 TMPDIR 环境变量(或 Windows 中的 %TEMP%)更改为您可以从中运行代码的地方,然后调用您编译的二进制文件应该可以完成工作。
Incidentally this method has the advantage that it also works if you get a pyinstaller generated binary from someone else and don't have access to the source code.
顺便说一句,这种方法的优点是,如果您从其他人那里获得了 pyinstaller 生成的二进制文件并且无法访问源代码,它也可以工作。
回答by ChrisWue
As noted by @Darkerlvy in a comment there is now a command line option --runtime-tmpdir PATH
which you can pass to pyinstaller. No need to modify the source or set an env variable. It was introduced in pyinstaller 3.3
正如@Darkerlvy 在评论中指出的,现在有一个命令行选项--runtime-tmpdir PATH
,您可以将其传递给 pyinstaller。无需修改源代码或设置环境变量。它是在 pyinstaller 3.3中引入的