在我看来,如果没有该行,文件运行相同.
说我有一个功能:
def NewFunction():
return '£'
Run Code Online (Sandbox Code Playgroud)
我想在它前面打一些带有井号的东西,当我尝试运行这个程序时它会输出错误,显示以下错误信息:
SyntaxError: Non-ASCII character '\xa3' in file 'blah' but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何在返回功能中加入英镑符号?我基本上是在课堂上使用它,它'__str__'包含在包含英镑符号的部分内.
我的python以某种方式在同一目录中找不到任何模块.我究竟做错了什么?(python2.7)
所以我有一个目录'2014_07_13_test',里面有两个文件:
其中hello.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
def hello1():
print 'HelloWorld!'
Run Code Online (Sandbox Code Playgroud)
和test.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
from hello import hello1
hello1()
Run Code Online (Sandbox Code Playgroud)
仍然python给了我
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 4, in <module>
ImportError: No module named hello
Run Code Online (Sandbox Code Playgroud)
怎么了?
我曾经使用过shebang
#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud)
什么时候使用更好
#!/usr/bin/python
Run Code Online (Sandbox Code Playgroud)
它们之间的确切区别是什么?
我通常在我的Python脚本中使用以下shebang声明:
#!/usr/bin/python
Run Code Online (Sandbox Code Playgroud)
最近,我遇到了这个shebang声明:
#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud)
在脚本文档中,注意到使用此表单"更具可移植性".
这个声明是什么意思?为什么路径中间有空间?它实际上是否有助于稳定性?
我的Python脚本beak包含以下shebang:
#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时$ ./beak,我得到了
env: python\r: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我之前从存储库中提取了此脚本.这可能是什么原因?
如果我在CMD中这样做,它可以正常工作,但如果我在Git Bash中尝试它,它不起作用.我喜欢使用Git Bash作为我唯一的控制台,但如果它不适用于Python 3.4,我就不能这样做.
示例如下图所示.这可以很容易地再现.卸载Python和Git(如果已安装),安装Python 3.4,安装Git 2.5.1并获得该结果.
如何让Python解释器在Git Bash中运行?
TL; DR
这是一个示例存储库,如第一个图(下面)中所述设置:https://github.com/Poddster/package_problems
如果你可以在项目组织方面让它看起来像第二个图,并且仍然可以运行以下命令,那么你已经回答了这个问题:
$ git clone https://github.com/Poddster/package_problems.git
$ cd package_problems
<do your magic here>
$ nosetests
$ ./my_tool/my_tool.py
$ ./my_tool/t.py
$ ./my_tool/d.py
(or for the above commands, $ cd ./my_tool/ && ./my_tool.py is also acceptable)
Run Code Online (Sandbox Code Playgroud)
或者:给我一个不同的项目结构,允许我将相关文件('包')组合在一起,单独运行所有文件,将文件导入同一包中的其他文件,并将包/文件导入其他包的文件.
我有一堆python文件.当从命令行调用时,它们中的大多数都是有用的,即它们都使用argparse并if __name__ == "__main__"执行有用的操作.
目前我有这个目录结构,一切正常:
.
??? config.txt
??? docs/
? ??? ...
??? my_tool.py
??? a.py
??? b.py
??? c.py
??? d.py
??? e.py
??? README.md
??? tests
? ??? __init__.py
? ??? a.py
? ??? …Run Code Online (Sandbox Code Playgroud) 我有字符串变量测试,在Python 2.7中这很好用.
test = raw_input("enter the test")
print test
Run Code Online (Sandbox Code Playgroud)
但是在Python 3.3中.我用
test = input("enter the test")
print test
Run Code Online (Sandbox Code Playgroud)
使用输入字符串test,我收到一条错误消息
Traceback(最近一次调用最后一次):
文件"/home/ananiev/PycharmProjects/PigLatin/main.py",
第5行,在test = input("输入测试")
NameError中的文件"",第1行:名称'sdas'未定义
Windows Subsystem for Linux 与 GPS 的串行通信。
Adafruit GPS 连接到 Arduino Nano,后者连接到COM10. 在Windows Subsystem for Linux这相当于/dev/ttyS10
要求: pyserial
我写了一个简单的脚本来从 GPS 模块读取信息:
import serial
def select_sentence():
""" This function sends serial data to the GPS module to display only GPGGA and GPRMC"""
def read_gps():
ser = serial.Serial("/dev/ttyS10", 9600)
while True:
print(ser.readline().decode('utf-8'))
if __name__ == "__main__":
select_sentence()
read_gps()
Run Code Online (Sandbox Code Playgroud)
在我选择的 virtualenv 中Python3,当我执行它时,我得到Permission Error了串行端口,/ttyS10所以我选择sudo chmod 666 /dev/ttyS10在virtualenv.
但是,是否有替代上述方法 …
python ×10
shebang ×4
python-3.x ×2
bash ×1
env ×1
eval ×1
git ×1
import ×1
input ×1
macos ×1
module ×1
packages ×1
portability ×1
pycharm ×1
python-2.7 ×1
shell ×1
string ×1
unicode ×1
virtualenv ×1