PyInstaller:从EXE程序文件更新json文件的问题(在下一次重新发布时,json文件没有更新)

时间:2016-06-05 08:07:38

标签: python json pyinstaller

我正在测试pyinstaller制作的EXE。 在项目文件夹中有一个名为config的文件夹,其中包含一个json文件,用户可以使用tkinter存储他想要的所有信息。 但最后在重新启动此应用程序并重新打开json文件后,它出现在原始文件中。

我读到了在执行时创建一个新文件夹,我在其中放置了原始json文件。但我对这个解决方案并不满意。

请提供任何帮助

更新

这是项目结构:

/config
      |----config.json
/modules
      |----admin
      |----core
      |----graphwo
init.py

代码执行得很好,除了我希望用户在执行时将其信息保存在config.json文件中。但是因为我使用的PyInstaller是--onefile,它不允许更新config.json文件

更新II:

此外,我还有这个代码,它可以获取每个文件执行时的当前路径 - 图像,数据和json文件 - 应用程序需要:

def getPathFileAtExecution(relative):
    try:
    base_path = sys._MEIPASS
except Exception:
    base_path = os.path.abspath(".")
return os.path.join(base_path, relative)

当我调用json文件进行读写时,我会跟踪程序的任何步骤。但在完成并重新启动之后,之前所做的所有更改都不会反映出来。 梅现在更清楚了吗?

2 个答案:

答案 0 :(得分:0)

首先检查您是否指向正确的路径。某些操作系统对某些系统变量和函数调用的响应不同。

import sys
import os
if getattr(sys,'frozen',False):
    current_path = os.path.dirname(sys.executable)
else:
    current_path = os.path.dirname(os.path.realpath(__file__))

config_json_file_path = os.path.join(current_path, 'config', 'config.json')
print config_json_file_path

答案 1 :(得分:0)

# npm install
npm WARN package.json module-name@10.3.1 No description
npm WARN package.json module-name@10.3.1 No repository field.
npm WARN package.json module-name@10.3.1 No README data
npm WARN package.json module-name@10.3.1 No license field.
npm WARN engine node-stringprep@0.7.3: wanted: {"node":">=0.8 <=0.12"} (current: {"node":"4.4.4","npm":"2.15.1"})
-
> node-stringprep@0.7.3 install /root/tmp/test/node_modules/node-stringprep
> node-gyp rebuild
make: Entering directory `/root/tmp/test/node_modules/node-stringprep/build'
  CXX(target) Release/obj.target/node_stringprep/node-stringprep.o
In file included from ../node-stringprep.cc:1:0:
../node_modules/nan/nan.h:261:25: error: redefinition of 'template<class T> v8::Local<T> _NanEnsureLocal(v8::Local<T>)'
 NAN_INLINE v8::Local<T> _NanEnsureLocal(v8::Local<T> val) {
                         ^
../node_modules/nan/nan.h:256:25: error: 'template<class T> v8::Local<T> _NanEnsureLocal(v8::Handle<T>)' previously declared here
 NAN_INLINE v8::Local<T> _NanEnsureLocal(v8::Handle<T> val) {
                         ^
../node_modules/nan/nan.h:661:13: error: 'node::smalloc' has not been declared
     , node::smalloc::FreeCallback callback
             ^
../node_modules/nan/nan.h:661:35: error: expected ',' or '...' before 'callback'
     , node::smalloc::FreeCallback callback
                                   ^
../node_modules/nan/nan.h: In function 'v8::Local<v8::Object> NanNewBufferHandle(char*, size_t, int)':
../node_modules/nan/nan.h:665:50: error: 'callback' was not declared in this scope
         v8::Isolate::GetCurrent(), data, length, callback, hint);
                                                  ^
../node_modules/nan/nan.h:665:60: error: 'hint' was not declared in this scope
         v8::Isolate::GetCurrent(), data, length, callback, hint);
                                                            ^
../node_modules/nan/nan.h: In function 'v8::Local<v8::Object> NanNewBufferHandle(const char*, uint32_t)':
../node_modules/nan/nan.h:672:67: error: call of overloaded 'New(v8::Isolate*, const char*&, uint32_t&)' is ambiguous
     return node::Buffer::New(v8::Isolate::GetCurrent(), data, size);
                                                                   ^
../node_modules/nan/nan.h:672:67: note: candidates are:
In file included from ../node_modules/nan/nan.h:25:0,
                 from ../node-stringprep.cc:1:
/root/.node-gyp/4.4.4/include/node/node_buffer.h:31:40: note: v8::MaybeLocal<v8::Object> node::Buffer::New(v8::Isolate*, v8::Local<v8::String>, node::encoding) <near match>
 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
                                        ^
/root/.node-gyp/4.4.4/include/node/node_buffer.h:31:40: note:   no known conversion for argument 3 from 'uint32_t {aka unsigned int}' to 'node::encoding'
/root/.node-gyp/4.4.4/include/node/node_buffer.h:43:40: note: v8::MaybeLocal<v8::Object> node::Buffer::New(v8::Isolate*, char*, size_t) <near match>
 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
                                        ^
/root/.node-gyp/4.4.4/include/node/node_buffer.h:43:40: note:   no known conversion for argument 2 from 'const char*' to 'char*'
In file included from ../node-stringprep.cc:1:0:
../node_modules/nan/nan.h: In function 'v8::Local<v8::Object> NanNewBufferHandle(uint32_t)':
../node_modules/nan/nan.h:676:61: error: could not convert 'node::Buffer::New(v8::Isolate::GetCurrent(), ((size_t)size))' from 'v8::MaybeLocal<v8::Object>' to 'v8::Local<v8::Object>'
     return node::Buffer::New(v8::Isolate::GetCurrent(), size);
                                                             ^
../node_modules/nan/nan.h: In function 'v8::Local<v8::Object> NanBufferUse(char*, uint32_t)':
../node_modules/nan/nan.h:683:12: error: 'Use' is not a member of 'node::Buffer'
     return node::Buffer::Use(v8::Isolate::GetCurrent(), data, size);
            ^
make: *** [Release/obj.target/node_stringprep/node-stringprep.o] Error 1
make: Leaving directory `/root/tmp/test/node_modules/node-stringprep/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:276:23)
gyp ERR! stack     at emitTwo (events.js:87:13)
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
gyp ERR! System Linux 2.6.32-573.3.1.el6.x86_64
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /root/tmp/test/node_modules/node-stringprep
gyp ERR! node -v v4.4.4
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok 
npm ERR! Linux 2.6.32-573.3.1.el6.x86_64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v4.4.4
npm ERR! npm  v2.15.1
npm ERR! code ELIFECYCLE
npm ERR! node-stringprep@0.7.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the node-stringprep@0.7.3 install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the node-stringprep package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs node-stringprep
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! 
npm ERR!     npm owner ls node-stringprep
npm ERR! There is likely additional logging output above.

sys._MEIPASS变量是程序运行时运行应用程序的捆绑文件的位置。这与您的应用程序所在的位置不同。为了让您的程序找到并操作该非捆绑的.json文件,我使用import os import sys if getattr(sys, 'frozen', False): # we are running in a |PyInstaller| bundle base_path = sys._MEIPASS extDataDir = os.getcwd() print base_path print extDataDir else: # we are running in a normal Python environment base_path = os.getcwd() extDataDir = os.getcwd() 来获取应用程序所在的文件夹。

os.getcwd()获取可执行文件所在的当前工作目录。然后,如果.json文件位于名为os.getcwd()的文件夹中,并且该文件夹位于exe所在的当前工作目录中从中跑,你会用

config