我创建了一个我想在后台无形运行的应用程序(没有命令/ cmd控制台).我该怎么做呢?
(这适用于Windows,在Windows 7 Pro 64位上测试过)
我知道在unix世界中,如果编辑.profile或.cshrc文件,可以使用source~/.profile或source~/.cshrc来获取当前会话的效果.如果我在Windows上更改了系统变量中的某些内容,如何在不退出命令提示符会话并打开另一个命令提示符会话的情况下使其影响当前的命令提示符会话?
我最近在我的Windows 10 Home(64位)机器上安装了"Anaconda3 for Windows v2.4.0".
(我从https://www.continuum.io/downloads下载了Windows 64位图形安装程序"Anaconda3-2.4.0-Windows-x86_64.exe"(392 MB).)
在命令提示符窗口中,我做了conda"Test Drive",包括"conda update conda"等.最后,我看到以下内容:
C:\Users\Anshul\Downloads\Python>conda update conda
Fetching package metadata: ....
# All requested packages already installed.
# packages in environment at C:\Anaconda3:
#
conda 3.18.6 py35_0 defaults
C:\Users\Anshul\Downloads\Python>conda list matplotlib
# packages in environment at C:\Anaconda3:
#
matplotlib 1.5.0 np110py35_0 defaults
Run Code Online (Sandbox Code Playgroud)
安装似乎已经成功 - 例如:
C:\Users\Anshul\Downloads\Python>python
Python 3.5.0 |Anaconda 2.4.0 (64-bit)| (default, Nov 7 2015, 13:15:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" …Run Code Online (Sandbox Code Playgroud) 最新的Windows 10更新包括对 conhost.exe中的ANSI转义序列的支持.
我已经能够确认在cmd.exe中正确地获取了转义序列,所以我有必要的更新.特别是,我尝试输入prompt $e[?25l,隐藏光标,然后prompt $e[?25h再次显示光标.
但是,如果我启动Python解释器,然后执行以下操作:
>>> import sys
>>> sys.stdout.write("\033[?25l")
Run Code Online (Sandbox Code Playgroud)
好吧,光标没有隐藏.如何以正确的方式设置,以便控制台能够从Python获取转义序列?
我试图使用ctypes从Python(3.2)向C发送2个字符串.这是我的Raspberry Pi上项目的一小部分.为了测试C函数是否正确接收到字符串,我将其中一个放在文本文件中.
Python代码
string1 = "my string 1"
string2 = "my string 2"
# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')
# send strings to c function
my_c_function(ctypes.create_string_buffer(b_string1),
ctypes.create_string_buffer(b_string2))
Run Code Online (Sandbox Code Playgroud)
C代码
void my_c_function(const char* str1, const char* str2)
{
// Test if string is correct
FILE *fp = fopen("//home//pi//Desktop//out.txt", "w");
if (fp != NULL)
{
fputs(str1, fp);
fclose(fp);
}
// Do something with strings..
}
Run Code Online (Sandbox Code Playgroud)
问题
只有字符串的第一个字母出现在文本文件中.
我已经尝试了很多方法来使用ctypes转换Python字符串对象.
通过这些转换,我不断收到错误"错误类型"或"预期的字节或整数地址而不是str实例".
我希望有人可以告诉我哪里出了问题.提前致谢.
所以让我们说IC/C++代码分配一些内存,并返回一个指向它的指针.
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
void Allocate(void **p) {
int N=2048;
*p=malloc(N);
}
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
我明白,释放这块内存是我的责任.现在假设我将其编译成共享库并使用ctypes从Python调用它,但是没有显式释放该内存.
import ctypes
from ctypes import cdll, Structure, byref
external_lib = cdll.LoadLibrary('libtest.so.1.0')
ptr=ctypes.c_void_p(0)
external_lib.Allocate(ctypes.byref(ptr))
Run Code Online (Sandbox Code Playgroud)
如果我用valgrind运行这个脚本,如果我编译test.cpp没有'-O3'标志,我会得到2048字节的内存泄漏.但是如果我使用'-O3'标志编译它,那么我不会得到内存泄漏.
这不是一个真正的问题 - 我会小心翼翼地明确释放我分配的任何内存.但我很好奇这种行为来自哪里.
我在linux中用以下脚本测试了这个.
g++ -Wall -c -fPIC -fno-common test.cpp -o libtest1.o
g++ -shared -Wl,-soname,libtest1.so.1 -o libtest1.so.1.0 libtest1.o
g++ -O3 -Wall -c -fPIC -fno-common test.cpp -o libtest2.o
g++ -shared -Wl,-soname,libtest2.so.1 -o libtest2.so.1.0 libtest2.o
valgrind python test1.py &> report1
valgrind python test2.py &> report2
Run Code Online (Sandbox Code Playgroud)
具有以下输出 …
我在这里遇到了严重的问题.我需要通过C++执行CMD命令行,而不显示控制台窗口.因此我无法使用system(cmd),因为窗口会显示.
我试过了winExec(cmd, SW_HIDE),但这也行不通.CreateProcess是我试过的另一个.但是,这适用于运行程序或批处理文件.
我最终尝试了ShellExecute:
ShellExecute( NULL, "open",
"cmd.exe",
"ipconfig > myfile.txt",
"c:\projects\b",
SW_SHOWNORMAL
);
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到上述代码有什么问题吗?我已经习惯了,SW_SHOWNORMAL直到我知道这是有效的.
我真的需要一些帮助.没有任何事情发生,我已经尝试了很长一段时间.任何人都可以给出的建议会很棒:)
我正在使用Windows 10并安装了Python.新的更新为windows带来了bash,但是当我从bash中调用python时,它指的是随bash一起提供的Python安装,而不是我在Windows上安装的Python.因此,例如,我不能使用我已在Windows上安装的模块,并且必须在bash安装上单独安装它们.
我怎么能(我可以吗?)将bash指向我原来的Windows Python安装?我在/ usr/bin中看到我在其名称中有很多与"python"的链接,但我不确定要更改哪些链接,如果将它们更改为Windows目录甚至会因为不同的可执行格式而起作用.
从Python在Windows上与命名管道进行通信的正确方法是什么?我用谷歌搜索了它,找不到包装这种通信的任何软件包.
有:
我只需连接到现有的命名管道并读/写它.我之前只尝试过使用串口进行通信(使用pySerial),我很惊讶与命名管道相比我能找到的信息很少.对于Python来说,通常有大量的指南用于任何目的.
我会感激任何帮助.
我正在为C库编写一些绑定,并且我不确定如何配置所有这些以进行分发,因此可以使用pip install我的包.
假设我有以下文件:
library.clibrary.hwrapper.py为了使我的包装器库工作,有必要:
ctypesgen上library.h生成ctypes的代码以下是命令:
gcc -Wall -fPIC -c library.cgcc -shared -Wl,-soname,liblibrary.so.1 -o liblibrary.so.1.0 library.octypesgen.py library.h -L ./ -l library -o _library.py运行setup.py还取决于已安装的用户ctypesgen.
我不知道如何设置这一切,以便对图书馆感兴趣的人可以简单地pip install library自动完成所有这些.有人能帮忙吗?
python ×5
windows ×5
cmd ×3
ctypes ×3
python-3.x ×3
bash ×2
c ×1
c++ ×1
console ×1
distutils ×1
go ×1
matplotlib ×1
named-pipes ×1
setuptools ×1
string ×1
tcsh ×1
unix ×1
winapi ×1
windows-10 ×1