标签归档:python-2.7

如何在pytest中打印到控制台?

问题:如何在pytest中打印到控制台?

我正在尝试将TDD(测试驱动的开发)与 pytestpytest使用时不会print进入控制台print

我正在pytest my_tests.py运行它。

documentation似乎是说,它应该是默认的工作:http://pytest.org/latest/capture.html

但:

import myapplication as tum

class TestBlogger:

    @classmethod
    def setup_class(self):
        self.user = "alice"
        self.b = tum.Blogger(self.user)
        print "This should be printed, but it won't be!"

    def test_inherit(self):
        assert issubclass(tum.Blogger, tum.Site)
        links = self.b.get_links(posts)
        print len(links)   # This won't print either.

什么都没有打印到我的标准输出控制台上(只是正常的进度以及通过/失败的测试数量)。

我正在测试的脚本包含打印:

class Blogger(Site):
    get_links(self, posts):
        print len(posts)   # It won't get printed in the test.

unittest模块中,默认情况下会打印所有内容,这正是我所需要的。但是,我想用pytest出于其他原因。

有谁知道如何使打印报表显示出来?

I’m trying to use TDD (test-driven development) with pytest. pytest will not print to the console when I use print.

I am using pytest my_tests.py to run it.

The documentation seems to say that it should work by default: http://pytest.org/latest/capture.html

But:

import myapplication as tum

class TestBlogger:

    @classmethod
    def setup_class(self):
        self.user = "alice"
        self.b = tum.Blogger(self.user)
        print "This should be printed, but it won't be!"

    def test_inherit(self):
        assert issubclass(tum.Blogger, tum.Site)
        links = self.b.get_links(posts)
        print len(links)   # This won't print either.

Nothing gets printed to my standard output console (just the normal progress and how many many tests passed/failed).

And the script that I’m testing contains print:

class Blogger(Site):
    get_links(self, posts):
        print len(posts)   # It won't get printed in the test.

In unittest module, everything gets printed by default, which is exactly what I need. However, I wish to use pytest for other reasons.

Does anyone know how to make the print statements get shown?


回答 0

默认情况下,py.test捕获标准输出的结果,以便它可以控制其输出结果的方式。如果不这样做,它将喷出大量文本,而没有测试打印该文本的上下文。

但是,如果测试失败,它将在结果报告中包括一部分,以显示在该特定测试中打印出的标准内容。

例如,

def test_good():
    for i in range(1000):
        print(i)

def test_bad():
    print('this should fail!')
    assert False

结果如下:

>>> py.test tmp.py
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2
plugins: cache, cov, pep8, xdist
collected 2 items

tmp.py .F

=================================== FAILURES ===================================
___________________________________ test_bad ___________________________________

    def test_bad():
        print('this should fail!')
>       assert False
E       assert False

tmp.py:7: AssertionError
------------------------------- Captured stdout --------------------------------
this should fail!
====================== 1 failed, 1 passed in 0.04 seconds ======================

注意该Captured stdout部分。

如果您希望print在执行语句时看到它们,可以将-s标志传递给py.test。但是,请注意,有时可能很难解析。

>>> py.test tmp.py -s
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2
plugins: cache, cov, pep8, xdist
collected 2 items

tmp.py 0
1
2
3
... and so on ...
997
998
999
.this should fail!
F

=================================== FAILURES ===================================
___________________________________ test_bad ___________________________________

    def test_bad():
        print('this should fail!')
>       assert False
E       assert False

tmp.py:7: AssertionError
====================== 1 failed, 1 passed in 0.02 seconds ======================

By default, py.test captures the result of standard out so that it can control how it prints it out. If it didn’t do this, it would spew out a lot of text without the context of what test printed that text.

However, if a test fails, it will include a section in the resulting report that shows what was printed to standard out in that particular test.

For example,

def test_good():
    for i in range(1000):
        print(i)

def test_bad():
    print('this should fail!')
    assert False

Results in the following output:

>>> py.test tmp.py
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2
plugins: cache, cov, pep8, xdist
collected 2 items

tmp.py .F

=================================== FAILURES ===================================
___________________________________ test_bad ___________________________________

    def test_bad():
        print('this should fail!')
>       assert False
E       assert False

tmp.py:7: AssertionError
------------------------------- Captured stdout --------------------------------
this should fail!
====================== 1 failed, 1 passed in 0.04 seconds ======================

Note the Captured stdout section.

If you would like to see print statements as they are executed, you can pass the -s flag to py.test. However, note that this can sometimes be difficult to parse.

>>> py.test tmp.py -s
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2
plugins: cache, cov, pep8, xdist
collected 2 items

tmp.py 0
1
2
3
... and so on ...
997
998
999
.this should fail!
F

=================================== FAILURES ===================================
___________________________________ test_bad ___________________________________

    def test_bad():
        print('this should fail!')
>       assert False
E       assert False

tmp.py:7: AssertionError
====================== 1 failed, 1 passed in 0.02 seconds ======================

回答 1

using -s选项将打印所有功能的输出,可能太多了。

如果您需要特定的输出,则您提到的文档页面提供了一些建议:

  1. assert False, "dumb assert to make PyTest print my stuff"在函数的末尾插入,由于测试失败,您将看到输出。

  2. 您有PyTest传递给您的特殊对象,您可以将输出写入文件中以供日后检查,例如

    def test_good1(capsys):
        for i in range(5):
            print i
        out, err = capsys.readouterr()
        open("err.txt", "w").write(err)
        open("out.txt", "w").write(out)

    您可以在单独的标签中打开outerr文件,然后让编辑器为您自动刷新它,或者执行简单的py.test; cat out.txtshell命令来运行测试。

那是做事的一种骇人听闻的方式,但是可能正是您所需要的东西:毕竟,TDD意味着您会弄乱这些东西,并在准备就绪时保持干净整洁:-)。

Using -s option will print output of all functions, which may be too much.

If you need particular output, the doc page you mentioned offers few suggestions:

  1. Insert assert False, "dumb assert to make PyTest print my stuff" at the end of your function, and you will see your output due to failed test.

  2. You have special object passed to you by PyTest, and you can write the output into a file to inspect it later, like

    def test_good1(capsys):
        for i in range(5):
            print i
        out, err = capsys.readouterr()
        open("err.txt", "w").write(err)
        open("out.txt", "w").write(out)
    

    You can open the out and err files in a separate tab and let editor automatically refresh it for you, or do a simple py.test; cat out.txt shell command to run your test.

That is rather hackish way to do stuff, but may be it is the stuff you need: after all, TDD means you mess with stuff and leave it clean and silent when it’s ready :-).


回答 2

简短答案

使用-s选项:

pytest -s

详细答案

文档

在执行测试期间,将捕获发送到stdoutstderr的所有输出。如果测试或设置方法失败,则通常会显示其相应的捕获输出以及失败回溯。

pytest具有选项--capture=method,其中method是每个测试捕获方法,并且可以是下列之一:fdsysnopytest还具有-s是的快捷方式--capture=no的选项,该选项使您可以在控制台中查看打印语句。

pytest --capture=no     # show print statements in console
pytest -s               # equivalent to previous command

设置捕获方法或禁用捕获

有两种pytest执行捕获的方法:

  1. 文件描述符(FD)级别捕获(默认):将捕获所有对操作系统文件描述符1和2的写操作。

  2. sys级捕获:仅捕获对Python文件sys.stdout和sys.stderr的写入。不捕获对文件描述符的写入。

pytest -s            # disable all capturing
pytest --capture=sys # replace sys.stdout/stderr with in-mem files
pytest --capture=fd  # also point filedescriptors 1 and 2 to temp file

Short Answer

Use the -s option:

pytest -s

Detailed answer

From the docs:

During test execution any output sent to stdout and stderr is captured. If a test or a setup method fails its according captured output will usually be shown along with the failure traceback.

pytest has the option --capture=method in which method is per-test capturing method, and could be one of the following: fd, sys or no. pytest also has the option -s which is a shortcut for --capture=no, and this is the option that will allow you to see your print statements in the console.

pytest --capture=no     # show print statements in console
pytest -s               # equivalent to previous command

Setting capturing methods or disabling capturing

There are two ways in which pytest can perform capturing:

  1. file descriptor (FD) level capturing (default): All writes going to the operating system file descriptors 1 and 2 will be captured.

  2. sys level capturing: Only writes to Python files sys.stdout and sys.stderr will be captured. No capturing of writes to filedescriptors is performed.

pytest -s            # disable all capturing
pytest --capture=sys # replace sys.stdout/stderr with in-mem files
pytest --capture=fd  # also point filedescriptors 1 and 2 to temp file

回答 3

PyTest确实需要在忽略所有内容时打印有关跳过测试的重要警告。

我不想通过测试发送信号失败,所以我做了如下的修改:

def test_2_YellAboutBrokenAndMutedTests():
    import atexit
    def report():
        print C_patch.tidy_text("""
In silent mode PyTest breaks low level stream structure I work with, so
I cannot test if my functionality work fine. I skipped corresponding tests.
Run `py.test -s` to make sure everything is tested.""")
    if sys.stdout != sys.__stdout__:
        atexit.register(report)

atexit模块允许我 PyTest释放输出流打印内容。输出如下:

============================= test session starts ==============================
platform linux2 -- Python 2.7.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /media/Storage/henaro/smyth/Alchemist2-git/sources/C_patch, inifile: 
collected 15 items 

test_C_patch.py .....ssss....s.

===================== 10 passed, 5 skipped in 0.15 seconds =====================
In silent mode PyTest breaks low level stream structure I work with, so
I cannot test if my functionality work fine. I skipped corresponding tests.
Run `py.test -s` to make sure everything is tested.
~/.../sources/C_patch$

即使PyTest在静默模式下,消息也会被打印,如果您使用来运行东西,则消息不会被打印py.test -s,因此一切都已经过了很好的测试。

I needed to print important warning about skipped tests exactly when PyTest muted literally everything.

I didn’t want to fail a test to send a signal, so I did a hack as follow:

def test_2_YellAboutBrokenAndMutedTests():
    import atexit
    def report():
        print C_patch.tidy_text("""
In silent mode PyTest breaks low level stream structure I work with, so
I cannot test if my functionality work fine. I skipped corresponding tests.
Run `py.test -s` to make sure everything is tested.""")
    if sys.stdout != sys.__stdout__:
        atexit.register(report)

The atexit module allows me to print stuff after PyTest released the output streams. The output looks as follow:

============================= test session starts ==============================
platform linux2 -- Python 2.7.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /media/Storage/henaro/smyth/Alchemist2-git/sources/C_patch, inifile: 
collected 15 items 

test_C_patch.py .....ssss....s.

===================== 10 passed, 5 skipped in 0.15 seconds =====================
In silent mode PyTest breaks low level stream structure I work with, so
I cannot test if my functionality work fine. I skipped corresponding tests.
Run `py.test -s` to make sure everything is tested.
~/.../sources/C_patch$

Message is printed even when PyTest is in silent mode, and is not printed if you run stuff with py.test -s, so everything is tested nicely already.


回答 4

根据pytest docspytest --capture=sys应该可以工作。如果要在测试中捕获标准,请参考capsys装置。

According to the pytest docs, pytest --capture=sys should work. If you want to capture standard out inside a test, refer to the capsys fixture.


回答 5

我最初是来这里寻找如何PyTest在VSCode的控制台中运行/调试单元测试的同时进行打印的。这可以通过以下launch.json配置完成。给定.venv虚拟环境文件夹。

    "version": "0.2.0",
    "configurations": [
        {
            "name": "PyTest",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "module": "pytest",
            "args": [
                "-sv"
            ],
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.venv",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }
    ]
}

I originally came in here to find how to make PyTest print in VSCode’s console while running/debugging the unit test from there. This can be done with the following launch.json configuration. Given .venv the virtual environment folder.

    "version": "0.2.0",
    "configurations": [
        {
            "name": "PyTest",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "module": "pytest",
            "args": [
                "-sv"
            ],
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.venv",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }
    ]
}

json.load()和json.loads()函数有什么区别

问题:json.load()和json.loads()函数有什么区别

在Python中,json.load()和之间有什么区别json.loads()

我猜想load()函数必须与文件对象一起使用(因此,我需要使用上下文管理器),而load()函数将文件路径作为字符串。这有点令人困惑。

字母“ sjson.loads()代表字符串吗?

非常感谢你的回答!

In Python, what is the difference between json.load() and json.loads()?

I guess that the load() function must be used with a file object (I need thus to use a context manager) while the loads() function take the path to the file as a string. It is a bit confusing.

Does the letter “s” in json.loads() stand for string?

Thanks a lot for your answers!


回答 0

是的,s代表字符串。该json.loads函数不采用文件路径,而是将文件内容作为字符串。查看位于https://docs.python.org/2/library/json.html的文档!

Yes, s stands for string. The json.loads function does not take the file path, but the file contents as a string. Look at the documentation at https://docs.python.org/2/library/json.html!


回答 1

只是在每个人的解释中添加一个简单的例子,

json.load()

json.load可以反序列化文件本身,即它接受一个file对象,例如,

# open a json file for reading and print content using json.load
with open("/xyz/json_data.json", "r") as content:
  print(json.load(content))

将输出

{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}

如果我改用json.loads打开文件,

# you cannot use json.loads on file object
with open("json_data.json", "r") as content:
  print(json.loads(content))

我会收到此错误:

TypeError:预期的字符串或缓冲区

json.loads()

json.loads() 反串化字符串。

因此,要使用json.loads该文件read(),我将不得不使用函数传递文件的内容,例如,

content.read()json.loads()文件的返回内容一起使用,

with open("json_data.json", "r") as content:
  print(json.loads(content.read()))

输出,

{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}

那是因为类型content.read()是字符串,即<type 'str'>

如果json.load()与配合使用content.read(),则会出现错误,

with open("json_data.json", "r") as content:
  print(json.load(content.read()))

给,

AttributeError:’str’对象没有属性’read’

因此,现在您知道json.load反序列化文件并json.loads反序列化一个字符串。

另一个例子,

sys.stdin返回file对象,所以如果我这样做print(json.load(sys.stdin)),我将获得实际的json数据,

cat json_data.json | ./test.py

{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}

如果要使用json.loads(),我会print(json.loads(sys.stdin.read()))改为使用。

Just going to add a simple example to what everyone has explained,

json.load()

json.load can deserialize a file itself i.e. it accepts a file object, for example,

# open a json file for reading and print content using json.load
with open("/xyz/json_data.json", "r") as content:
  print(json.load(content))

will output,

{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}

If I use json.loads to open a file instead,

# you cannot use json.loads on file object
with open("json_data.json", "r") as content:
  print(json.loads(content))

I would get this error:

TypeError: expected string or buffer

json.loads()

json.loads() deserialize string.

So in order to use json.loads I will have to pass the content of the file using read() function, for example,

using content.read() with json.loads() return content of the file,

with open("json_data.json", "r") as content:
  print(json.loads(content.read()))

Output,

{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}

That’s because type of content.read() is string, i.e. <type 'str'>

If I use json.load() with content.read(), I will get error,

with open("json_data.json", "r") as content:
  print(json.load(content.read()))

Gives,

AttributeError: ‘str’ object has no attribute ‘read’

So, now you know json.load deserialze file and json.loads deserialize a string.

Another example,

sys.stdin return file object, so if i do print(json.load(sys.stdin)), I will get actual json data,

cat json_data.json | ./test.py

{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}

If I want to use json.loads(), I would do print(json.loads(sys.stdin.read())) instead.


回答 2

文档非常清晰:https//docs.python.org/2/library/json.html

json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

使用此转换表将fp(支持.read()的包含JSON文档的类似文件的对象)反序列化为Python对象。

json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

使用此转换表将s(包含JSON文档的str或unicode实例)反序列化为Python对象。

所以load是一个文件,loads一个string

Documentation is quite clear: https://docs.python.org/2/library/json.html

json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.

json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

Deserialize s (a str or unicode instance containing a JSON document) to a Python object using this conversion table.

So load is for a file, loads for a string


回答 3

快速解答(非常简化!)

json.load()需要一个文件

json.load()需要一个文件(文件对象),例如,您在文件路径(如)给定之前打开的文件'files/example.json'


json.loads()需要一个STRING

json.loads()需要一个(有效)JSON字符串-即 {"foo": "bar"}


例子

假设您有一个文件example.json,其内容如下:{“ key_1”:1,1,“ key_2”:“ foo”,“ Key_3”:null}

>>> import json
>>> file = open("example.json")

>>> type(file)
<class '_io.TextIOWrapper'>

>>> file
<_io.TextIOWrapper name='example.json' mode='r' encoding='UTF-8'>

>>> json.load(file)
{'key_1': 1, 'key_2': 'foo', 'Key_3': None}

>>> json.loads(file)
Traceback (most recent call last):
  File "/usr/local/python/Versions/3.7/lib/python3.7/json/__init__.py", line 341, in loads
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper


>>> string = '{"foo": "bar"}'

>>> type(string)
<class 'str'>

>>> string
'{"foo": "bar"}'

>>> json.loads(string)
{'foo': 'bar'}

>>> json.load(string)
Traceback (most recent call last):
  File "/usr/local/python/Versions/3.7/lib/python3.7/json/__init__.py", line 293, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

QUICK ANSWER (very simplified!)

json.load() takes a FILE

json.load() expects a file (file object) – e.g. a file you opened before given by filepath like 'files/example.json'.


json.loads() takes a STRING

json.loads() expects a (valid) JSON string – i.e. {"foo": "bar"}


EXAMPLES

Assuming you have a file example.json with this content: { “key_1”: 1, “key_2”: “foo”, “Key_3”: null }

>>> import json
>>> file = open("example.json")

>>> type(file)
<class '_io.TextIOWrapper'>

>>> file
<_io.TextIOWrapper name='example.json' mode='r' encoding='UTF-8'>

>>> json.load(file)
{'key_1': 1, 'key_2': 'foo', 'Key_3': None}

>>> json.loads(file)
Traceback (most recent call last):
  File "/usr/local/python/Versions/3.7/lib/python3.7/json/__init__.py", line 341, in loads
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper


>>> string = '{"foo": "bar"}'

>>> type(string)
<class 'str'>

>>> string
'{"foo": "bar"}'

>>> json.loads(string)
{'foo': 'bar'}

>>> json.load(string)
Traceback (most recent call last):
  File "/usr/local/python/Versions/3.7/lib/python3.7/json/__init__.py", line 293, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

回答 4

所述json.load()方法(无“S”中的“负荷”)可直接读取的文件:

import json
with open('strings.json') as f:
    d = json.load(f)
    print(d)

json.loads()方法,仅用于字符串参数。

import json

person = '{"name": "Bob", "languages": ["English", "Fench"]}'
print(type(person))
# Output : <type 'str'>

person_dict = json.loads(person)
print( person_dict)
# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}

print(type(person_dict))
# Output : <type 'dict'>

在这里,我们可以看到在使用load()将字符串(type(str))作为输入并返回字典之后

The json.load() method (without “s” in “load”) can read a file directly:

import json
with open('strings.json') as f:
    d = json.load(f)
    print(d)

json.loads() method, which is used for string arguments only.

import json

person = '{"name": "Bob", "languages": ["English", "Fench"]}'
print(type(person))
# Output : <type 'str'>

person_dict = json.loads(person)
print( person_dict)
# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}

print(type(person_dict))
# Output : <type 'dict'>

Here , we can see after using loads() takes a string ( type(str) ) as a input and return dictionary.


回答 5

在python3.7.7中,根据cpython源代码,json.load的定义如下:

def load(fp, *, cls=None, object_hook=None, parse_float=None,
        parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):

    return loads(fp.read(),
        cls=cls, object_hook=object_hook,
        parse_float=parse_float, parse_int=parse_int,
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)

json.load实际上调用json.loads并fp.read()用作第一个参数。

因此,如果您的代码是:

with open (file) as fp:
    s = fp.read()
    json.loads(s)

这样做是一样的:

with open (file) as fp:
    json.load(fp)

但是,如果您需要指定从文件中读取的字节,例如,fp.read(10)或者您要反序列化的字符串/字节不是从文件中读取,则应使用json.loads()

至于json.loads(),它不仅反序列化字符串,而且还反序列化字节。如果s为bytes或bytearray,则将其首先解码为字符串。您也可以在源代码中找到它。

def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
        parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
    """Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
    containing a JSON document) to a Python object.

    ...

    """
    if isinstance(s, str):
        if s.startswith('\ufeff'):
            raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
                                  s, 0)
    else:
        if not isinstance(s, (bytes, bytearray)):
            raise TypeError(f'the JSON object must be str, bytes or bytearray, '
                            f'not {s.__class__.__name__}')
        s = s.decode(detect_encoding(s), 'surrogatepass')

In python3.7.7, the definition of json.load is as below according to cpython source code:

def load(fp, *, cls=None, object_hook=None, parse_float=None,
        parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):

    return loads(fp.read(),
        cls=cls, object_hook=object_hook,
        parse_float=parse_float, parse_int=parse_int,
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)

json.load actually calls json.loads and use fp.read() as the first argument.

So if your code is:

with open (file) as fp:
    s = fp.read()
    json.loads(s)

It’s the same to do this:

with open (file) as fp:
    json.load(fp)

But if you need to specify the bytes reading from the file as like fp.read(10) or the string/bytes you want to deserialize is not from file, you should use json.loads()

As for json.loads(), it not only deserialize string but also bytes. If s is bytes or bytearray, it will be decoded to string first. You can also find it in the source code.

def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
        parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
    """Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
    containing a JSON document) to a Python object.

    ...

    """
    if isinstance(s, str):
        if s.startswith('\ufeff'):
            raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
                                  s, 0)
    else:
        if not isinstance(s, (bytes, bytearray)):
            raise TypeError(f'the JSON object must be str, bytes or bytearray, '
                            f'not {s.__class__.__name__}')
        s = s.decode(detect_encoding(s), 'surrogatepass')


我如何避免“ self.x = x; self.y = y; __init__中的self.z = z”模式?

问题:我如何避免“ self.x = x; self.y = y; __init__中的self.z = z”模式?

我看到像

def __init__(self, x, y, z):
    ...
    self.x = x
    self.y = y
    self.z = z
    ...

非常频繁,通常带有更多参数。是否有避免这种乏味重复的好方法?该类应该继承namedtuple吗?

I see patterns like

def __init__(self, x, y, z):
    ...
    self.x = x
    self.y = y
    self.z = z
    ...

quite frequently, often with a lot more parameters. Is there a good way to avoid this type of tedious repetitiveness? Should the class inherit from namedtuple instead?


回答 0

编辑:如果您有python 3.7+,只需使用数据类

保留签名的装饰器解决方案:

import decorator
import inspect
import sys


@decorator.decorator
def simple_init(func, self, *args, **kws):
    """
    @simple_init
    def __init__(self,a,b,...,z)
        dosomething()

    behaves like

    def __init__(self,a,b,...,z)
        self.a = a
        self.b = b
        ...
        self.z = z
        dosomething()
    """

    #init_argumentnames_without_self = ['a','b',...,'z']
    if sys.version_info.major == 2:
        init_argumentnames_without_self = inspect.getargspec(func).args[1:]
    else:
        init_argumentnames_without_self = tuple(inspect.signature(func).parameters.keys())[1:]

    positional_values = args
    keyword_values_in_correct_order = tuple(kws[key] for key in init_argumentnames_without_self if key in kws)
    attribute_values = positional_values + keyword_values_in_correct_order

    for attribute_name,attribute_value in zip(init_argumentnames_without_self,attribute_values):
        setattr(self,attribute_name,attribute_value)

    # call the original __init__
    func(self, *args, **kws)


class Test():
    @simple_init
    def __init__(self,a,b,c,d=4):
        print(self.a,self.b,self.c,self.d)

#prints 1 3 2 4
t = Test(1,c=2,b=3)
#keeps signature
#prints ['self', 'a', 'b', 'c', 'd']
if sys.version_info.major == 2:
    print(inspect.getargspec(Test.__init__).args)
else:
    print(inspect.signature(Test.__init__))

Edit: If you have python 3.7+ just use dataclasses

A decorator solution that keeps the signature:

import decorator
import inspect
import sys


@decorator.decorator
def simple_init(func, self, *args, **kws):
    """
    @simple_init
    def __init__(self,a,b,...,z)
        dosomething()

    behaves like

    def __init__(self,a,b,...,z)
        self.a = a
        self.b = b
        ...
        self.z = z
        dosomething()
    """

    #init_argumentnames_without_self = ['a','b',...,'z']
    if sys.version_info.major == 2:
        init_argumentnames_without_self = inspect.getargspec(func).args[1:]
    else:
        init_argumentnames_without_self = tuple(inspect.signature(func).parameters.keys())[1:]

    positional_values = args
    keyword_values_in_correct_order = tuple(kws[key] for key in init_argumentnames_without_self if key in kws)
    attribute_values = positional_values + keyword_values_in_correct_order

    for attribute_name,attribute_value in zip(init_argumentnames_without_self,attribute_values):
        setattr(self,attribute_name,attribute_value)

    # call the original __init__
    func(self, *args, **kws)


class Test():
    @simple_init
    def __init__(self,a,b,c,d=4):
        print(self.a,self.b,self.c,self.d)

#prints 1 3 2 4
t = Test(1,c=2,b=3)
#keeps signature
#prints ['self', 'a', 'b', 'c', 'd']
if sys.version_info.major == 2:
    print(inspect.getargspec(Test.__init__).args)
else:
    print(inspect.signature(Test.__init__))

回答 1

免责声明:似乎有些人担心提出此解决方案,因此我将提供一个非常明确的免责声明。您不应该使用此解决方案。我仅将其作为信息提供,因此您知道该语言可以做到这一点。剩下的答案只是显示语言功能,而不是认可以这种方式使用它们。


明确地将参数复制到属性中并没有什么错。如果ctor中的参数太多,有时会被认为是代码异味,也许您应该将这些参数分组到更少的对象中。在其他时候,这是必要的,没有错。无论如何,明确地做到这一点是必须的。

但是,由于您要问如何完成(而不是是否应该这样做),因此一种解决方案是:

class A:
    def __init__(self, **kwargs):
        for key in kwargs:
          setattr(self, key, kwargs[key])

a = A(l=1, d=2)
a.l # will return 1
a.d # will return 2

Disclaimer: It seems that several people are concerned about presenting this solution, so I will provide a very clear disclaimer. You should not use this solution. I only provide it as information, so you know that the language is capable of this. The rest of the answer is just showing language capabilities, not endorsing using them in this way.


There isn’t really anything wrong with explicitly copying parameters into attributes. If you have too many parameters in the ctor, it is sometimes considered a code smell and maybe you should group these params into a fewer objects. Other times, it is necessary and there is nothing wrong with it. Anyway, doing it explicitly is the way to go.

However, since you are asking HOW it can be done (and not whether it should be done), then one solution is this:

class A:
    def __init__(self, **kwargs):
        for key in kwargs:
          setattr(self, key, kwargs[key])

a = A(l=1, d=2)
a.l # will return 1
a.d # will return 2

回答 2

正如其他人所提到的,重复并不坏,但在某些情况下,命名元组可能非常适合此类问题。这样可以避免使用locals()或kwargs,这通常不是一个好主意。

from collections import namedtuple
# declare a new object type with three properties; x y z
# the first arg of namedtuple is a typename
# the second arg is comma-separated or space-separated property names
XYZ = namedtuple("XYZ", "x, y, z")

# create an object of type XYZ. properties are in order
abc = XYZ("one", "two", 3)
print abc.x
print abc.y
print abc.z

我发现它的用途有限,但是您可以像其他任何对象一样继承一个namedtuple(示例继续):

class MySuperXYZ(XYZ):
    """ I add a helper function which returns the original properties """
    def properties(self):
        return self.x, self.y, self.z

abc2 = MySuperXYZ(4, "five", "six")
print abc2.x
print abc2.y
print abc2.z
print abc2.properties()

As others have mentioned, the repetition isn’t bad, but in some cases a namedtuple can be a great fit for this type of issue. This avoids using locals() or kwargs, which are usually a bad idea.

from collections import namedtuple
# declare a new object type with three properties; x y z
# the first arg of namedtuple is a typename
# the second arg is comma-separated or space-separated property names
XYZ = namedtuple("XYZ", "x, y, z")

# create an object of type XYZ. properties are in order
abc = XYZ("one", "two", 3)
print abc.x
print abc.y
print abc.z

I’ve found limited use for it, but you can inherit a namedtuple as with any other object (example continued):

class MySuperXYZ(XYZ):
    """ I add a helper function which returns the original properties """
    def properties(self):
        return self.x, self.y, self.z

abc2 = MySuperXYZ(4, "five", "six")
print abc2.x
print abc2.y
print abc2.z
print abc2.properties()

回答 3

显式比隐式更好…因此,请确保您可以使其更简洁:

def __init__(self,a,b,c):
    for k,v in locals().items():
        if k != "self":
             setattr(self,k,v)

更好的问题是您?

…这就是说,如果您想要一个命名元组,我建议您使用namedtuple(记住元组具有某些附加条件)…也许您想要一个有序的字典甚至是一个字典…

explicit is better than implicit … so sure you could make it more concise:

def __init__(self,a,b,c):
    for k,v in locals().items():
        if k != "self":
             setattr(self,k,v)

The better question is should you?

… that said if you want a named tuple I would recommend using a namedtuple (remember tuples have certain conditions attached to them) … perhaps you want an ordereddict or even just a dict …


回答 4

为了扩展gruszczys的答案,我使用了类似的模式:

class X:
    x = None
    y = None
    z = None
    def __init__(self, **kwargs):
        for (k, v) in kwargs.items():
            if hasattr(self, k):
                setattr(self, k, v)
            else:
                raise TypeError('Unknown keyword argument: {:s}'.format(k))

我喜欢这种方法,因为它:

  • 避免重复
  • 构造对象时可以抵抗拼写错误
  • 可以很好地与子类化(只需super().__init(...)
  • 允许在类级别(它们所属的地方)而不是在 X.__init__

在Python 3.6之前,这无法控制属性的设置顺序,如果某些属性是带有访问其他属性的设置器的属性,则可能会出现问题。

可能会有所改善,但是我是我自己的代码的唯一用户,因此我不担心任何形式的输入卫生。也许AttributeError更合适。

To expand on gruszczys answer, I have used a pattern like:

class X:
    x = None
    y = None
    z = None
    def __init__(self, **kwargs):
        for (k, v) in kwargs.items():
            if hasattr(self, k):
                setattr(self, k, v)
            else:
                raise TypeError('Unknown keyword argument: {:s}'.format(k))

I like this method because it:

  • avoids repetition
  • is resistant against typos when constructing an object
  • works well with subclassing (can just super().__init(...))
  • allows for documentation of the attributes on a class-level (where they belong) rather than in X.__init__

Prior to Python 3.6, this gives no control over the order in which the attributes are set, which could be a problem if some attributes are properties with setters that access other attributes.

It could probably be improved upon a bit, but I’m the only user of my own code so I am not worried about any form of input sanitation. Perhaps an AttributeError would be more appropriate.


回答 5

您也可以这样做:

locs = locals()
for arg in inspect.getargspec(self.__init__)[0][1:]:
    setattr(self, arg, locs[arg])

当然,您将必须导入inspect模块。

You could also do:

locs = locals()
for arg in inspect.getargspec(self.__init__)[0][1:]:
    setattr(self, arg, locs[arg])

Of course, you would have to import the inspect module.


回答 6

这是一个无需任何其他导入的解决方案。

辅助功能

一个小的辅助函数使它更加方便和可重复使用:

def auto_init(local_name_space):
    """Set instance attributes from arguments.
    """
    self = local_name_space.pop('self')
    for name, value in local_name_space.items():
        setattr(self, name, value)

应用

您需要使用以下命令调用它locals()

class A:
    def __init__(self, x, y, z):
        auto_init(locals())

测试

a = A(1, 2, 3)
print(a.__dict__)

输出:

{'y': 2, 'z': 3, 'x': 1}

不变 locals()

如果您不想更改,请locals()使用以下版本:

def auto_init(local_name_space):
    """Set instance attributes from arguments.
    """
    for name, value in local_name_space.items():
        if name != 'self': 
            setattr(local_name_space['self'], name, value)

This is a solution without any additional imports.

Helper function

A small helper function makes it more convenient and re-usable:

def auto_init(local_name_space):
    """Set instance attributes from arguments.
    """
    self = local_name_space.pop('self')
    for name, value in local_name_space.items():
        setattr(self, name, value)

Application

You need to call it with locals():

class A:
    def __init__(self, x, y, z):
        auto_init(locals())

Test

a = A(1, 2, 3)
print(a.__dict__)

Output:

{'y': 2, 'z': 3, 'x': 1}

Without changing locals()

If you don’t like to change locals() use this version:

def auto_init(local_name_space):
    """Set instance attributes from arguments.
    """
    for name, value in local_name_space.items():
        if name != 'self': 
            setattr(local_name_space['self'], name, value)

回答 7

一个有趣的库可以处理这个问题(并避免很多其他样板文件)是attrs。例如,您的示例可以简化为以下示例(假设该类称为MyClass):

import attr

@attr.s
class MyClass:
    x = attr.ib()
    y = attr.ib()
    z = attr.ib()

您甚至不需要任何__init__方法,除非它也执行其他操作。这是Glyph Lefkowitz的精彩介绍

An interesting library that handles this (and avoids a lot of other boilerplate) is attrs. Your example, for instance, could be reduced to this (assume the class is called MyClass):

import attr

@attr.s
class MyClass:
    x = attr.ib()
    y = attr.ib()
    z = attr.ib()

You don’t even need an __init__ method anymore, unless it does other stuff as well. Here’s a nice introduction by Glyph Lefkowitz.


回答 8

我的0.02 $。它与Joran Beasley的答案非常接近,但更为优雅:

def __init__(self, a, b, c, d, e, f):
    vars(self).update((k, v) for k, v in locals().items() if v is not self)

此外,可以使用以下技术来减少MikeMüller的答案(最适合我的口味):

def auto_init(ns):
    self = ns.pop('self')
    vars(self).update(ns)

auto_init(locals())您的来话__init__

My 0.02$. It is very close to Joran Beasley answer, but more elegant:

def __init__(self, a, b, c, d, e, f):
    vars(self).update((k, v) for k, v in locals().items() if v is not self)

Additionally, Mike Müller’s answer (the best one to my taste) can be reduced with this technique:

def auto_init(ns):
    self = ns.pop('self')
    vars(self).update(ns)

And the just call auto_init(locals()) from your __init__


回答 9

这是用Python做事的自然方法。不要尝试发明更聪明的东西,它会导致代码太聪明,而团队中没人会理解。如果您想成为团队合作者,然后继续以这种方式编写。

It’s a natural way to do things in Python. Don’t try to invent something more clever, it will lead to overly clever code that no one on your team will understand. If you want to be a team player and then keep writing it this way.


回答 10

Python 3.7以上

在Python 3.7中,您可以(ab)使用模块dataclass提供的装饰器dataclasses。从文档中:

该模块提供了一个装饰器和一些函数,用于自动将生成的特殊方法(例如__init__()和)添加__repr__()到用户定义的类中。它最初在PEP 557中进行了描述。

这些生成的方法中使用的成员变量是使用PEP 526类型注释定义的。例如此代码:

@dataclass
class InventoryItem:
    '''Class for keeping track of an item in inventory.'''
    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

除其他外,将添加__init__()如下所示的:

def __init__(self, name: str, unit_price: float, quantity_on_hand: int=0):
      self.name = name
      self.unit_price = unit_price
      self.quantity_on_hand = quantity_on_hand

请注意,此方法会自动添加到类中:上面显示的InventoryItem定义中未直接指定此方法。

如果您的类又大又复杂,那么使用可能是不合适的dataclass。我在Python 3.7.0发行之日就在写这篇文章,因此用法模式尚未很好地建立。

Python 3.7 onwards

In Python 3.7, you may (ab)use the dataclass decorator, available from the dataclasses module. From the documentation:

This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes. It was originally described in PEP 557.

The member variables to use in these generated methods are defined using PEP 526 type annotations. For example this code:

@dataclass
class InventoryItem:
    '''Class for keeping track of an item in inventory.'''
    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

Will add, among other things, a __init__() that looks like:

def __init__(self, name: str, unit_price: float, quantity_on_hand: int=0):
      self.name = name
      self.unit_price = unit_price
      self.quantity_on_hand = quantity_on_hand

Note that this method is automatically added to the class: it is not directly specified in the InventoryItem definition shown above.

If your class is large and complex, it may be inappropriate to use a dataclass. I’m writing this on the day of release of Python 3.7.0, so usage patterns are not yet well established.


在Windows 7上添加Python路径

问题:在Windows 7上添加Python路径

我一直在尝试在Windows 7上将Python路径添加到命令行中,但是无论我尝试哪种方法,似乎都没有任何效果。我使用了该set命令,尝试通过“编辑环境”变量提示符等添加它。

此外,如果我在命令行上运行set命令,它将列出此内容

python = c:\python27

但是它仍然无法识别Python命令。

阅读文档以及其他各种资源似乎无济于事。

编辑:为了进一步说明,我在编辑环境提示中将Python可执行文件的路径附加到PATH。似乎不起作用。

I’ve been trying to add the Python path to the command line on Windows 7, yet no matter the method I try, nothing seems to work. I’ve used the set command, I’ve tried adding it through the Edit Environment variables prompt etc.

Further more if I run the set command on the command line it lists this

python = c:\python27

Yet it still doesn’t recognize the Python command.

Reading the documentation, and various other sources hasn’t seemed to help.

Edit: Just to clarify further, I’ve appended the path of the Python executable to PATH in edit environment prompt. Doesn’t seem to work.


回答 0

  1. 按住Win并按Pause
  2. 单击高级系统设置。
  3. 单击环境变量。
  4. 追加;C:\python27Path变量。
  5. 重新启动命令提示符。
  1. Hold Win and press Pause.
  2. Click Advanced System Settings.
  3. Click Environment Variables.
  4. Append ;C:\python27 to the Path variable.
  5. Restart Command Prompt.

回答 1

在Windows中设置环境变量时,我在很多情况下都出错了。我认为我应该在这里分享我过去的一些错误,希望对别人有所帮助。(这些适用于所有环境变量,不仅适用于设置Python路径)

当心这些可能的错误:

  1. 杀死并重新打开您的Shell窗口:对环境变量进行更改后,您必须重新启动要对其进行测试的窗口。
  2. 设置变量时没有空间。请确保您添加;C:\Python27无任何空格。(通常C:\SomeOther; C:\Python27在分号后尝试使用空格(␣)是不正确的。)
  3. 阐明完整路径时,请使用向后斜线。尝试时会看到正斜杠,echo $PATH但只有反斜杠对我有用。
  4. 不要添加最后的反斜杠。只有C:\Python27C:\Python27\

希望这对某人有帮助。

When setting Environmental Variables in Windows, I have gone wrong on many, many occasions. I thought I should share a few of my past mistakes here hoping that it might help someone. (These apply to all Environmental Variables, not just when setting Python Path)

Watch out for these possible mistakes:

  1. Kill and reopen your shell window: Once you make a change to the ENVIRONMENTAL Variables, you have to restart the window you are testing it on.
  2. NO SPACES when setting the Variables. Make sure that you are adding the ;C:\Python27 WITHOUT any spaces. (It is common to try C:\SomeOther; C:\Python27 That space (␣) after the semicolon is not okay.)
  3. USE A BACKWARD SLASH when spelling out your full path. You will see forward slashes when you try echo $PATH but only backward slashes have worked for me.
  4. DO NOT ADD a final backslash. Only C:\Python27 NOT C:\Python27\

Hope this helps someone.


回答 2

以管理员权限打开cmd .exe(右键单击应用程序)。然后输入:

setx路径“%path%; C:\ Python27;”

请记住以分号结尾,并且不要包含斜杠。

Open cmd.exe with administrator privileges (right click on app). Then type:

setx path “%path%;C:\Python27;”

Remember to end with a semi-colon and don’t include a trailing slash.


回答 3

我已经很久没有这个问题了。我以各种方式将其添加到我的路径中,但这终于对我有用:

  1. 右键单击“我的电脑”
  2. 点击“属性”
  3. 点击侧面板中的“高级系统设置”
  4. 点击“环境变量”
  5. 点击系统变量下面的“新建”
  6. 输入名称pythonexe(或您想要的任何名称
  7. 在值输入路径你的Python(例如:C:\Python32\
  8. 现在,编辑Path变量(在系统部分),然后添加%pythonexe%;到已经存在的末尾

IDK为什么这样做有效,但对我有用。

然后尝试在命令行中输入“ python”,它应该可以工作!


编辑:

最近,我一直在使用该程序,它似乎运行良好。还有一个看起来也不错,尽管我从未尝试过。

I’ve had a problem with this for a LONG time. I added it to my path in every way I could think of but here’s what finally worked for me:

  1. Right click on “My computer”
  2. Click “Properties”
  3. Click “Advanced system settings” in the side panel
  4. Click “Environment Variables”
  5. Click the “New” below system variables
  6. in name enter pythonexe (or anything you want)
  7. in value enter the path to your python (example: C:\Python32\)
  8. Now edit the Path variable (in the system part) and add %pythonexe%; to the end of what’s already there

IDK why this works but it did for me.

then try typing “python” into your command line and it should work!


Edit:

Lately I’ve been using this program which seems to work pretty well. There’s also this one which looks pretty good too, although I’ve never tried it.


回答 4

尝试将此python.bat文件添加到System32文件夹,然后在键入时命令行将运行pythonpython

python.bat

@C:\Python27\python.exe %*

资源:

https://github.com/KartikTalwar/dotfiles/blob/master/bat/python.bat

Try adding this python.bat file to System32 folder and the command line will now run python when you type in python

python.bat

@C:\Python27\python.exe %*

Source:

https://github.com/KartikTalwar/dotfiles/blob/master/bat/python.bat


回答 5

您可以使用命令从当前cmd窗口设置路径PATH =。那只会为当前的cmd实例添加它。如果要永久添加,则应将其添加到系统变量。(计算机>高级系统设置>环境变量)

您将转到您的cmd实例,然后放入PATH=C:/Python27/;%PATH%

You can set the path from the current cmd window using the PATH = command. That will only add it for the current cmd instance. if you want to add it permanently, you should add it to system variables. (Computer > Advanced System Settings > Environment Variables)

You would goto your cmd instance, and put in PATH=C:/Python27/;%PATH%.


回答 6

确保您没有在新目录之前添加空格。

好:旧;旧;旧;新

不好:老;老;老;新

Make sure you don’t add a space before the new directory.

Good: old;old;old;new

Bad: old;old;old; new


回答 7

Python带有一个小型工具,可以执行此操作。从命令行运行:

c:\python27\tools\scripts\win_add2path.py

确保关闭命令窗口(使用exit或关闭按钮),然后再次打开它。

Python comes with a small utility that does just this. From the command line run:

c:\python27\tools\scripts\win_add2path.py

Make sure you close the command window (with exit or the close button) and open it again.


回答 8

以下程序将python可执行路径和subdir脚本(例如pip和easy_install的安装位置)添加到您的环境中。它从绑定.py扩展名的注册表项中找到python可执行文件的路径。它将删除您环境中的旧python路径。也适用于XP(可能还有Vista)。它仅使用基本Windows安装程序随附的模块。

# coding: utf-8

import sys
import os
import time
import _winreg
import ctypes

def find_python():
    """
    retrieves the commandline for .py extensions from the registry
    """
    hKey = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
                           r'Python.File\shell\open\command')
    # get the default value
    value, typ = _winreg.QueryValueEx (hKey, None)
    program = value.split('"')[1]
    if not program.lower().endswith(r'\python.exe'):
        return None
    return os.path.dirname(program)

def extend_path(pypath, remove=False, verbose=0, remove_old=True,
                script=False):
    """
    extend(pypath) adds pypath to the PATH env. variable as defined in the
    registry, and then notifies applications (e.g. the desktop) of this change.
    !!! Already opened DOS-Command prompts are not updated. !!!
    Newly opened prompts will have the new path (inherited from the 
    updated windows explorer desktop)
    options:
    remove (default unset), remove from PATH instead of extend PATH
    remove_old (default set), removes any (old) python paths first
    script (default unset), try to add/remove the Scripts subdirectory 
        of pypath (pip, easy_install) as well
    """
    _sd = 'Scripts' # scripts subdir
    hKey = _winreg.OpenKey (_winreg.HKEY_LOCAL_MACHINE,
               r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
               0, _winreg.KEY_READ | _winreg.KEY_SET_VALUE)

    value, typ = _winreg.QueryValueEx (hKey, "PATH")
    vals = value.split(';')
    assert isinstance(vals, list)
    if not remove and remove_old:
        new_vals = []
        for v in vals:
            pyexe = os.path.join(v, 'python.exe')
            if v != pypath and os.path.exists(pyexe):
                if verbose > 0:
                    print 'removing from PATH:', v
                continue
            if script and v != os.path.join(pypath, _sd) and \
               os.path.exists(v.replace(_sd, pyexe)):
                if verbose > 0:
                    print 'removing from PATH:', v
                continue
            new_vals.append(v)
        vals = new_vals
    if remove:
        try:
            vals.remove(pypath)
        except ValueError:
            if verbose > 0:
                print 'path element', pypath, 'not found'
            return
        if script:
            try:
                vals.remove(os.path.join(pypath, _sd))
            except ValueError:
                pass
            print 'removing from PATH:', pypath
    else:
        if pypath in vals:
            if verbose > 0:
                print 'path element', pypath, 'already in PATH'
            return
        vals.append(pypath)
        if verbose > 1:
            print 'adding to PATH:', pypath
        if script:
            if not pypath + '\\Scripts' in vals:
                vals.append(pypath + '\\Scripts')
            if verbose > 1:
                print 'adding to PATH:', pypath + '\\Scripts'
    _winreg.SetValueEx(hKey, "PATH", 0, typ, ';'.join(vals) )
    _winreg.SetValueEx(hKey, "OLDPATH", 0, typ, value )
    _winreg.FlushKey(hKey)
    # notify other programs
    SendMessage = ctypes.windll.user32.SendMessageW
    HWND_BROADCAST = 0xFFFF
    WM_SETTINGCHANGE = 0x1A
    SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment')
    if verbose > 1:
        print 'Do not forget to restart any command prompts'

if __name__ == '__main__':
    remove = '--remove' in sys.argv
    script = '--noscripts' not in sys.argv
    extend_path(find_python(), verbose=2, remove=remove, script=script)

The following program will add the python executable path and the subdir Scripts (which is where e.g. pip and easy_install are installed) to your environment. It finds the path to the python executable from the registry key binding the .py extension. It will remove old python paths in your environment. Works with XP (and probably Vista) as well. It only uses modules that come with the basic windows installer.

# coding: utf-8

import sys
import os
import time
import _winreg
import ctypes

def find_python():
    """
    retrieves the commandline for .py extensions from the registry
    """
    hKey = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
                           r'Python.File\shell\open\command')
    # get the default value
    value, typ = _winreg.QueryValueEx (hKey, None)
    program = value.split('"')[1]
    if not program.lower().endswith(r'\python.exe'):
        return None
    return os.path.dirname(program)

def extend_path(pypath, remove=False, verbose=0, remove_old=True,
                script=False):
    """
    extend(pypath) adds pypath to the PATH env. variable as defined in the
    registry, and then notifies applications (e.g. the desktop) of this change.
    !!! Already opened DOS-Command prompts are not updated. !!!
    Newly opened prompts will have the new path (inherited from the 
    updated windows explorer desktop)
    options:
    remove (default unset), remove from PATH instead of extend PATH
    remove_old (default set), removes any (old) python paths first
    script (default unset), try to add/remove the Scripts subdirectory 
        of pypath (pip, easy_install) as well
    """
    _sd = 'Scripts' # scripts subdir
    hKey = _winreg.OpenKey (_winreg.HKEY_LOCAL_MACHINE,
               r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
               0, _winreg.KEY_READ | _winreg.KEY_SET_VALUE)

    value, typ = _winreg.QueryValueEx (hKey, "PATH")
    vals = value.split(';')
    assert isinstance(vals, list)
    if not remove and remove_old:
        new_vals = []
        for v in vals:
            pyexe = os.path.join(v, 'python.exe')
            if v != pypath and os.path.exists(pyexe):
                if verbose > 0:
                    print 'removing from PATH:', v
                continue
            if script and v != os.path.join(pypath, _sd) and \
               os.path.exists(v.replace(_sd, pyexe)):
                if verbose > 0:
                    print 'removing from PATH:', v
                continue
            new_vals.append(v)
        vals = new_vals
    if remove:
        try:
            vals.remove(pypath)
        except ValueError:
            if verbose > 0:
                print 'path element', pypath, 'not found'
            return
        if script:
            try:
                vals.remove(os.path.join(pypath, _sd))
            except ValueError:
                pass
            print 'removing from PATH:', pypath
    else:
        if pypath in vals:
            if verbose > 0:
                print 'path element', pypath, 'already in PATH'
            return
        vals.append(pypath)
        if verbose > 1:
            print 'adding to PATH:', pypath
        if script:
            if not pypath + '\\Scripts' in vals:
                vals.append(pypath + '\\Scripts')
            if verbose > 1:
                print 'adding to PATH:', pypath + '\\Scripts'
    _winreg.SetValueEx(hKey, "PATH", 0, typ, ';'.join(vals) )
    _winreg.SetValueEx(hKey, "OLDPATH", 0, typ, value )
    _winreg.FlushKey(hKey)
    # notify other programs
    SendMessage = ctypes.windll.user32.SendMessageW
    HWND_BROADCAST = 0xFFFF
    WM_SETTINGCHANGE = 0x1A
    SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment')
    if verbose > 1:
        print 'Do not forget to restart any command prompts'

if __name__ == '__main__':
    remove = '--remove' in sys.argv
    script = '--noscripts' not in sys.argv
    extend_path(find_python(), verbose=2, remove=remove, script=script)

回答 9

我知道这篇文章很旧,但我想补充一点,该解决方案假设使用admin privs。如果您没有这些,可以:

转到控制面板,键入路径(此为Windows 7,现在在搜索框中),然后单击“为您的帐户编辑环境变量”。现在,您将看到环境变量对话框,其顶部为“用户变量”,而其下方为“系统变量”。

您可以作为用户单击顶部的“新建”按钮并添加:

变量名称:PATH
变量值:C:\ Python27

(任何地方都没有空格),然后单击“确定”。重新启动命令提示符后,User变量中的所有PATH都将附加到系统路径的末尾。它不会以任何其他方式替换PATH。

如果要设置特定的完整路径,最好创建一个像这样的批处理文件:

@echo off
PATH C:\User\Me\Programs\mingw\bin;C:\User\Me\Programs;C:\Windows\system32
title Compiler Environment - %Username%@%Computername%
cmd

将其命名为“ compiler.bat”或双击以启动它。或链接到它。或固定等等…

I know this post is old but I’d like to add that the solutions assume admin privs. If you don’t have those you can:

Go to control panel, type path (this is Windows 7 now so that’s in the Search box) and click “Edit Environment variables for your account”. You’ll now see the Environment Variable dialog with “User variables” on the top and “System variables” below.

You can, as a user, click the top “New” button and add:

Variable name: PATH
Variable value: C:\Python27

(no spaces anywhere) and click OK. Once your command prompt is restarted, any PATH in the User variables is appended to the end of the System Path. It doesn’t replace the PATH in any other way.

If you want a specific full path set up, you’re better off creating a batch file like this little one:

@echo off
PATH C:\User\Me\Programs\mingw\bin;C:\User\Me\Programs;C:\Windows\system32
title Compiler Environment - %Username%@%Computername%
cmd

Call it “compiler.bat” or whatever and double click to start it. Or link to it. Or pin it etc…


回答 10

您需要在系统变量中进行更改
-右键单击“我的电脑”
-单击“属性”
-单击侧面板中的“高级系统设置”
-单击环境变量-您将分为两部分变量和系统变量
-在系统变量部分下搜索变量’Path’单击编辑并添加
"C:\Python27;"(不带引号)将其保存
-现在打开命令行类型’path’ 并按Enter,您将看到路径变量已被修改
-现在输入python --version您将看到python版本

完成了

You need to make changes in your system variable
— Right click on “My computer”
— Click “Properties”
— Click “Advanced system settings” in the side panel
— Click on Environment Variable — You will two sections of user variable and system variable
— Under system variable section search for the variable ‘Path’ click on edit and add
"C:\Python27;" (without quotes) save it
— Now open command line type ‘path’ hit enter you will see path variable has been modified
— Now type python --version you will see the python version

And it is done


回答 11

对于尝试使用Python 3.3+实现此目标的任何人,Windows安装程序现在都提供了一个将python.exe添加到系统搜索路径的选项。在docs中阅读更多内容。

For anyone trying to achieve this with Python 3.3+, the Windows installer now includes an option to add python.exe to the system search path. Read more in the docs.


回答 12

使用Windows环境变量始终是一种可怕的经历。最近,我发现了一个令人称奇的工具,称为Rapid Environment Editor,它提供了非常简单的GUI来管理它们。

如果您使用Chocolatey,则可以使用安装它choco install rapidee。否则,请访问http://www.rapidee.com/cn/download

重新阅读此内容,听起来像是有偿的,但是我发誓我不是!一段时间以来,它一直是我工具箱中最有用的实用程序之一,但令我惊讶的是似乎没人知道它。

Working with Windows environment variables is always a horrible experience. Recently, I found an amazing tool called Rapid Environment Editor, which gives an awesomely simple GUI for managing them.

If you use chocolatey, you can install it using choco install rapidee. Otherwise, take a look at http://www.rapidee.com/en/download

Re-reading this, it sounds like a paid shill, but I swear I’m not! It’s just been one of the most useful utilities in my toolkit for a while and I’m surprised no one seems to know about it.


回答 13

如果Python与其他程序一起安装,例如我的ArcGIS 10.1,则还必须在环境变量中包含指向python.exe路径的所有其他文件夹。

所以我的环境变量看起来像这样:

系统变量>路径>添加 ;C:\Python27\ArcGIS10.1

If Python was installed with another program, such as ArcGIS 10.1 in my case, then you also must include any extra folders that path to the python.exe in your Environment Variables.

So my Environment Variables looks like this:

System variables > Path > add ;C:\Python27\ArcGIS10.1


回答 14

这个问题已经很老了,但是我遇到了一个类似的问题,这里没有列出我的特定解决方案:

确保您的PATH中没有不存在的文件夹。

就我而言,我有一堆默认文件夹(Windows,Powershell,Sql Server等),然后是C:\bin我通常使用的自定义,然后进行了其他各种调整,例如c:\python17等等。事实证明,cmd处理器发现c:\bin没有不存在,然后停止处理其余变量。

另外,我不知道如果没有PATH manager,我是否会注意到这一点。它很好地强调了该项目无效的事实。

This question is pretty old, but I just ran into a similar problem and my particular solution wasn’t listed here:

Make sure you don’t have a folder in your PATH that doesn’t exist.

In my case, I had a bunch of default folders (Windows, Powershell, Sql Server, etc) and then a custom C:\bin that I typically use, and then various other tweaks like c:\python17, etc. It turns out that the cmd processor was finding that c:\bin didn’t exist and then stopped processing the rest of the variable.

Also, I don’t know that I ever would have noticed this without PATH manager. It nicely highlighted the fact that that item was invalid.


回答 15

我刚刚使用选项“将python添加到PATH”在Windows 7上安装了Python 3.3。

在PATH变量中,安装程序会自动添加最后一个反斜杠C:\Python33\ ,因此在命令提示符下不起作用(我尝试多次关闭/打开提示符)

我删除了最后的反斜杠,然后它起作用了:C:\Python33

感谢Ram Narasimhan给您的提示4!

I just installed Python 3.3 on Windows 7 using the option “add python to PATH”.

In PATH variable, the installer automatically added a final backslash: C:\Python33\ and so it did not work on command prompt (i tried closing/opening the prompt several times)

I removed the final backslash and then it worked: C:\Python33

Thanks Ram Narasimhan for your tip #4 !


回答 16

我使用cmd在Win7 64位下组织了这样的python环境变量。

PYTHONPATH通过Windows的环境变量菜单设置变量,并将其添加%PYTHONPATH%PATH变量中:

...;%PYTHONPATH%

cmd shell将变量正确扩展为:

C:\>echo %PYTHONPATH%
C:\python27;c:\python27\lib;C:\python27\scripts

不要忘记在更改PATH之后重新启动cmd shell。

I organized my python environment variable like this under Win7 64-bit using cmd.

I set the variable PYTHONPATH via environment variable menue of windows and added %PYTHONPATH% to the PATH variable:

...;%PYTHONPATH%

The cmd shell expands the variable correctly to this:

C:\>echo %PYTHONPATH%
C:\python27;c:\python27\lib;C:\python27\scripts

Do not forget to restart cmd shell after changing PATH.


回答 17

将其写在命令提示符上:

set Path=%path%

用Python文件夹示例的路径替换%path%:

set Path=C:/Python27

write that on your Command Prompt:

set Path=%path%

Replace %path% by the Path of your Python Folder Example:

set Path=C:/Python27

回答 18

如果您对设置python的路径感到沮丧,只需下载新版本的python并卸载旧版本的python,并在安装新版本时询问是否设置路径标记并安装

这是最好的方法

If you have got frustrated by setting the path for the python just download the new version of python uninstall the older version of the python and while installing the new version it will ask whether to set path mark that and install

its the best way


如何在Python中逐行读取文件?

问题:如何在Python中逐行读取文件?

在史前时代(Python 1.4)中,我们做到了:

fp = open('filename.txt')
while 1:
    line = fp.readline()
    if not line:
        break
    print line

在Python 2.1之后,我们做到了:

for line in open('filename.txt').xreadlines():
    print line

在Python 2.3中获得便捷的迭代器协议之前,它可以做到:

for line in open('filename.txt'):
    print line

我看过一些使用更详细的示例:

with open('filename.txt') as fp:
    for line in fp:
        print line

这是首选的方法吗?

[edit]我知道with语句可以确保关闭文件…但是为什么文件对象的迭代器协议中没有包含该语句呢?

In pre-historic times (Python 1.4) we did:

fp = open('filename.txt')
while 1:
    line = fp.readline()
    if not line:
        break
    print line

after Python 2.1, we did:

for line in open('filename.txt').xreadlines():
    print line

before we got the convenient iterator protocol in Python 2.3, and could do:

for line in open('filename.txt'):
    print line

I’ve seen some examples using the more verbose:

with open('filename.txt') as fp:
    for line in fp:
        print line

is this the preferred method going forwards?

[edit] I get that the with statement ensures closing of the file… but why isn’t that included in the iterator protocol for file objects?


回答 0

首选以下原因正是有一个原因:

with open('filename.txt') as fp:
    for line in fp:
        print line

CPython的相对确定性的引用计数方案对垃圾回收来说,我们都被宠坏了。如果其他假设的Python实现with使用某种其他方案来回收内存,则它们在没有该块的情况下不一定会“足够快地”关闭文件。

在这样的实现中,如果您的代码打开文件的速度比垃圾收集器调用孤立文件句柄上的终结器的速度快,则可能会从OS收到“打开太多文件”错误。通常的解决方法是立即触发GC,但这是一个讨厌的技巧,必须由可能遇到错误的每个函数(包括库中的函数)来完成。什么样的恶梦。

或者,您可以只使用该with块。

奖金问题

(如果仅对问题的客观方面感兴趣,请立即停止阅读。)

为什么文件对象的迭代器协议中未包含该代码?

这是有关API设计的主观问题,因此我有两个部分的主观答案。

从直觉上讲,这是错的,因为它使迭代器协议执行两项单独的操作(遍历行关闭文件句柄),并且使外观简单的函数执行两项操作通常不是一个好主意。在这种情况下,感觉特别糟糕,因为迭代器以准功能,基于值的方式与文件内容相关联,但是管理文件句柄是完全独立的任务。对于阅读代码的人来说,将两者无形地压成一个动作是令人惊讶的,这使得推理程序行为变得更加困难。

其他语言基本上得出了相同的结论。Haskell简要调情了所谓的“惰性IO”,它允许您遍历文件并在到达流末尾时自动将其关闭,但是如今,在Haskell和Haskell中几乎普遍不建议使用惰性IO。用户大多转向更明确的资源管理,例如Conduit,其行为更像withPython中的块。

从技术上讲,您可能需要对Python中的文件句柄进行某些操作,如果迭代关闭了文件句柄,这些操作将无法正常工作。例如,假设我需要遍历文件两次:

with open('filename.txt') as fp:
    for line in fp:
        ...
    fp.seek(0)
    for line in fp:
        ...

虽然这是一种不太常见的用例,但请考虑以下事实:我可能刚刚将底部的三行代码添加到了原来具有前三行的现有代码库中。如果迭代关闭了该文件,我将无法执行该操作。因此,将迭代和资源管理分开保持可以更轻松地将代码块组合成一个更大的,可运行的Python程序。

可组合性是语言或API最重要的可用性功能之一。

There is exactly one reason why the following is preferred:

with open('filename.txt') as fp:
    for line in fp:
        print line

We are all spoiled by CPython’s relatively deterministic reference-counting scheme for garbage collection. Other, hypothetical implementations of Python will not necessarily close the file “quickly enough” without the with block if they use some other scheme to reclaim memory.

In such an implementation, you might get a “too many files open” error from the OS if your code opens files faster than the garbage collector calls finalizers on orphaned file handles. The usual workaround is to trigger the GC immediately, but this is a nasty hack and it has to be done by every function that could encounter the error, including those in libraries. What a nightmare.

Or you could just use the with block.

Bonus Question

(Stop reading now if are only interested in the objective aspects of the question.)

Why isn’t that included in the iterator protocol for file objects?

This is a subjective question about API design, so I have a subjective answer in two parts.

On a gut level, this feels wrong, because it makes iterator protocol do two separate things—iterate over lines and close the file handle—and it’s often a bad idea to make a simple-looking function do two actions. In this case, it feels especially bad because iterators relate in a quasi-functional, value-based way to the contents of a file, but managing file handles is a completely separate task. Squashing both, invisibly, into one action, is surprising to humans who read the code and makes it more difficult to reason about program behavior.

Other languages have essentially come to the same conclusion. Haskell briefly flirted with so-called “lazy IO” which allows you to iterate over a file and have it automatically closed when you get to the end of the stream, but it’s almost universally discouraged to use lazy IO in Haskell these days, and Haskell users have mostly moved to more explicit resource management like Conduit which behaves more like the with block in Python.

On a technical level, there are some things you may want to do with a file handle in Python which would not work as well if iteration closed the file handle. For example, suppose I need to iterate over the file twice:

with open('filename.txt') as fp:
    for line in fp:
        ...
    fp.seek(0)
    for line in fp:
        ...

While this is a less common use case, consider the fact that I might have just added the three lines of code at the bottom to an existing code base which originally had the top three lines. If iteration closed the file, I wouldn’t be able to do that. So keeping iteration and resource management separate makes it easier to compose chunks of code into a larger, working Python program.

Composability is one of the most important usability features of a language or API.


回答 1

是,

with open('filename.txt') as fp:
    for line in fp:
        print line

是要走的路。

它并不冗长。更安全。

Yes,

with open('filename.txt') as fp:
    for line in fp:
        print line

is the way to go.

It is not more verbose. It is more safe.


回答 2

如果您被多余的行关闭,则可以使用包装函数,如下所示:

def with_iter(iterable):
    with iterable as iter:
        for item in iter:
            yield item

for line in with_iter(open('...')):
    ...

在Python 3.3中,该yield from语句会使此操作更短:

def with_iter(iterable):
    with iterable as iter:
        yield from iter

if you’re turned off by the extra line, you can use a wrapper function like so:

def with_iter(iterable):
    with iterable as iter:
        for item in iter:
            yield item

for line in with_iter(open('...')):
    ...

in Python 3.3, the yield from statement would make this even shorter:

def with_iter(iterable):
    with iterable as iter:
        yield from iter

回答 3

f = open('test.txt','r')
for line in f.xreadlines():
    print line
f.close()
f = open('test.txt','r')
for line in f.xreadlines():
    print line
f.close()

什么是Python缓冲区类型?

问题:什么是Python缓冲区类型?

bufferpython中有一个类型,但是我不知道如何使用它。

Python文档中,描述为:

buffer(object[, offset[, size]])

object参数必须是支持缓冲区调用接口的对象(例如字符串,数组和缓冲区)。将创建一个引用该对象参数的新缓冲区对象。缓冲区对象将从对象的开头(或指定的偏移量)开始是一个切片。切片将延伸到对象的末尾(或具有由size参数指定的长度)。

There is a buffer type in python, but I don’t know how can I use it.

In the Python doc the description is:

buffer(object[, offset[, size]])

The object argument must be an object that supports the buffer call interface (such as strings, arrays, and buffers). A new buffer object will be created which references the object argument. The buffer object will be a slice from the beginning of object (or from the specified offset). The slice will extend to the end of object (or will have a length given by the size argument).


回答 0

用法示例:

>>> s = 'Hello world'
>>> t = buffer(s, 6, 5)
>>> t
<read-only buffer for 0x10064a4b0, size 5, offset 6 at 0x100634ab0>
>>> print t
world

在这种情况下,缓冲区是一个子字符串,从位置6开始,长度为5,并且不占用额外的存储空间-它引用字符串的一部分。

这对于像这样的短字符串不是很有用,但是在使用大量数据时可能是必需的。这个例子使用了可变的bytearray

>>> s = bytearray(1000000)   # a million zeroed bytes
>>> t = buffer(s, 1)         # slice cuts off the first byte
>>> s[1] = 5                 # set the second element in s
>>> t[0]                     # which is now also the first element in t!
'\x05'

如果您想对数据有多个视图并且不想(或不能)在内存中保存多个副本,这将非常有用。

请注意,尽管您可以在Python 2.7中使用它,但是buffer已经被memoryviewPython 3中的更好名称所代替。

还要注意,如果不深入研究C API,就不能为自己的对象实现缓冲区接口,即,不能在纯Python中做到这一点。

An example usage:

>>> s = 'Hello world'
>>> t = buffer(s, 6, 5)
>>> t
<read-only buffer for 0x10064a4b0, size 5, offset 6 at 0x100634ab0>
>>> print t
world

The buffer in this case is a sub-string, starting at position 6 with length 5, and it doesn’t take extra storage space – it references a slice of the string.

This isn’t very useful for short strings like this, but it can be necessary when using large amounts of data. This example uses a mutable bytearray:

>>> s = bytearray(1000000)   # a million zeroed bytes
>>> t = buffer(s, 1)         # slice cuts off the first byte
>>> s[1] = 5                 # set the second element in s
>>> t[0]                     # which is now also the first element in t!
'\x05'

This can be very helpful if you want to have more than one view on the data and don’t want to (or can’t) hold multiple copies in memory.

Note that buffer has been replaced by the better named memoryview in Python 3, though you can use either in Python 2.7.

Note also that you can’t implement a buffer interface for your own objects without delving into the C API, i.e. you can’t do it in pure Python.


回答 1

我认为在将python与本机库接口时,缓冲区非常有用。(Guido van Rossum buffer此邮件列表帖子中进行了解释)。

例如,numpy似乎使用缓冲区进行有效的数据存储:

import numpy
a = numpy.ndarray(1000000)

a.data是:

<read-write buffer for 0x1d7b410, size 8000000, offset 0 at 0x1e353b0>

I think buffers are e.g. useful when interfacing python to native libraries. (Guido van Rossum explains buffer in this mailinglist post).

For example, numpy seems to use buffer for efficient data storage:

import numpy
a = numpy.ndarray(1000000)

the a.data is a:

<read-write buffer for 0x1d7b410, size 8000000, offset 0 at 0x1e353b0>

如何更新Python?

问题:如何更新Python?

我从2012年初开始安装了2.7版。对于在安装最新版本之前是否应该完全卸载并擦除此版本,我无法达成共识。

“软”删除旧版本?硬删除/清除旧版本?安装在顶部?

我在某处看到了一个特殊的安装/升级过程,该过程使用Python安装的“分段”方法,将不同的版本分开并保持功能。不知道这是否是事实上的标准方法。

我还想知道Revo是否太过热情,是否可能导致清除仍然需要的残留物(例如环境/ PATH变量)而引起问题。

(Win7 x64,32位Python)

I have version 2.7 installed from early 2012. I can’t find any consensus on whether I should completely uninstall and wipe this version before putting on the latest version.

“Soft”-removing old versions? Hard-removing/wiping old versions? Installing over top?

I’ve seen somewhere a special install/upgrade process using a “segmenting” method of Python installations, keeping different versions separate and apart, but functional. Not sure if this is the standard, de facto way.

I also wonder if Revo gets too overzealous and may cause issues with wiping out still-needed remnants, like environment/PATH variables.

(Win7 x64, 32-bit Python)


回答 0

更新日期:2018-07-06

这个帖子现在已经快5年了!2020年,Python-2.7将停止从python.org接收官方更新。此外,还发布了Python-3.7。查看Python-Future,了解如何使您的Python-2代码与Python-3兼容。为了更新conda,文档现在建议conda update --all在您的每个conda环境中使用更新该版本的所有软件包和Python可执行文件。另外,由于它们将名称更改为Anaconda,所以我不知道Windows注册表项是否仍然相同。

更新日期:2017-03-24

自2015年6月以来,没有对Python(x,y)进行任何更新,因此我认为可以断定它已被放弃。

更新:2016-11-11

正如下面的@cxw注释所示,这些答案适用于相同的位版本,按位版本,我的意思是64位与32位。例如,这些答案将适用于从64位Python-2.7.10更新到64位Python-2.7.11,相同的位版本。虽然可以将两个不同的Python版本一起安装,但这需要一些技巧,因此我将为读者保存该练习。如果您不想黑客,我建议如果切换位版本,请先删除其他位版本。

更新日期:2016-05-16
  • 通过禁用更改Windows 和注册表的选项,AnacondaMiniConda可以与现有的Python安装一起使用PATH。解压后,conda在您的binPyPI中创建符号链接到或安装conda。然后创建另一个名为符号链接conda-activateactivate在巨蟒/ Miniconda根bin文件夹。现在,Anaconda / Miniconda就像Ruby RVM。仅用于conda-activate root启用Anaconda / Miniconda。
  • 便携式Python已不再开发或维护。

TL; DR

  • 使用Anaconda或miniconda,然后执行conda update --all以保持每个conda环境的更新,
  • 同样重要的版本官方的Python比如 2.7.5),只需安装过旧的( 2.7.4),
  • 官方Python的不同主要版本 3.3),与老,设置路径/联装并排方点到显性的( 2.7),快捷方式等(在bash $ ln /c/Python33/python.exe python3)。

答案取决于:

  1. 如果OP具有2.7.x,并且要安装较新的2.7.x,则

    • 如果使用MSI安装程序Python官方网站上,只要安装了旧版本,安装程序会发出警告,它会删除并替换旧版本; 前后检查“控制面板”中的“已安装程序”,以确认旧版本已被新版本替换;2.7.x的较新版本向后兼容,因此这是完全安全的,因此,IMHO 2.7.x的多个版本永远不需要。
    • 如果是从源代码构建的,那么您可能应该构建在一个全新的,干净的目录中,然后在通过所有测试并且确信它已成功构建后,将路径指向新的构建,但是您可能希望保留旧的进行构建,因为从源构建可能偶尔会遇到问题。请参阅我的指南,以在带有SDK 7.0的Windows 7上构建Python x64
    • 如果从诸如Python(x,y)之类的发行版进行安装,请访问其网站。Python(x,y)已被放弃。 我相信可以使用其包管理器在Python(x,y)内处理更新,但是更新更新也包含在其网站上。我找不到具体的参考,所以也许有人可以对此发表意见。与ActiveState相似,并且可能是有思想的,Python(x,y)明确指出它与Python的其他安装不兼容:

      建议在安装Python(x,y)之前先卸载所有其他Python发行版

    • Enthought Canopy使用MSI,并将分别安装到所有用户中或为所有用户安装,Program Files\Enthoughthome\AppData\Local\Enthought\Canopy\App针对每个用户安装。通过使用内置的更新工具来更新较新的安装。查看他们的文档
    • ActiveState还使用MSI,因此可以在较旧的安装之上安装较新的安装。查看其安装说明

      其他Python 2.7安装在Windows上,ActivePython 2.7无法与其他Python 2.7安装共存(例如,来自python.org的Python 2.7构建)。在安装ActivePython 2.7之前,请卸载其他所有Python 2.7安装。

    • Sage建议您将其安装到虚拟机中,并提供可用于此目的的Oracle VirtualBox映像文件。发出sage -upgrade命令在内部处理升级。
    • 可以使用以下conda命令更新Anaconda

      conda update --all

      Anaconda / Miniconda允许用户创建环境来管理多个Python版本,包括Python- 2.6、2.7、3.3、3.4 和3.5。Anaconda / Miniconda的根安装当前基于Python-2.7或Python-3.5。

      Anaconda可能会破坏其他Python安装。安装使用MSI安装程序。 [ 更新:2016-05-16] Anaconda和Miniconda现在使用.exe安装程序,并提供选项来禁用Windows PATH和注册表更改。

      因此,根据安装方式和安装过程中选择的选项,可以在不中断现有Python安装的情况下安装Anaconda / Miniconda。如果.exe使用安装程序和选项来改变的Windows PATH和注册表都没有禁用,则任何以前的Python的安装将被禁用,但只需卸载Python/ Miniconda安装应恢复原来的Python安装,也许除了Windows注册表Python\PythonCore键。

      Python/ Miniconda使得下面的注册表编辑无论安装选项:HKCU\Software\Python\ContinuumAnalytics\使用下列按键:HelpInstallPathModulesPythonPath– Python官方注册过这些按键,但下Python\PythonCore。还为Anaconda \ Miniconda注册了卸载信息。除非在安装过程中选择“在Windows中注册”选项,否则它不会创建PythonCore,因此像Visual Studio的Python Tools这样的集成不会自动看到Anaconda / Miniconda。如果注册Python/ Miniconda选项激活,那么我认为您现有的Python Windows注册表项将被改变和卸载可能不会恢复它们。

    • 我认为,可以通过WinPython控制面板处理WinPython更新。
    • PortablePython不再被开发它没有更新方法。可能更新可以解压缩到一个新的目录,然后App\lib\site-packagesApp\Scripts可以复制到新安装的,但如果没有工作,然后重新安装所有的包可能是必要的。使用pip list查看包安装了什么,它们的版本。有些是由PortablePython安装的。使用easy_install pip如果未安装它安装点子。
  2. 如果OP具有2.7.x,并且想要安装其他版本,例如 <= 2.6.x或> = 3.xx,则可以并排安装不同版本。您必须选择要与*.py文件关联的Python版本(如果有),以及要在路径中使用的版本,尽管如果使用BASH则应该能够设置具有不同路径的shell 。AFAIK 2.7.x向后兼容2.6.x,因此不需要IMHO并排安装,但是Python-3.xx不向后兼容,因此我的建议是将Python-2.7放在您的路径上并具有通过创建指向可执行文件的快捷方式python3(这是Linux上的常见设置),可以将python-3作为可选版本。Windows上官方的Python默认安装路径是

    • 适用于3.3.x的C:\ Python33(最新2013-07-29)
    • C:\ Python32 for 3.2.x
    • &C。
    • C:\ Python27 for 2.7.x(最新2013-07-29)
    • C:\ Python26 for 2.6.x
    • &C。
  3. 如果OP不是在更新Python,而只是在更新软件包,则他们可能希望研究virtualenv,以使特定于其开发项目的软件包的不同版本分开。Pip还是更新软件包的好工具。如果软件包使用二进制安装程序,则通常在安装新软件包之前先卸载旧软件包。

我希望这可以消除任何混乱。

UPDATE: 2018-07-06

This post is now nearly 5 years old! Python-2.7 will stop receiving official updates from python.org in 2020. Also, Python-3.7 has been released. Check out Python-Future on how to make your Python-2 code compatible with Python-3. For updating conda, the documentation now recommends using conda update --all in each of your conda environments to update all packages and the Python executable for that version. Also, since they changed their name to Anaconda, I don’t know if the Windows registry keys are still the same.

UPDATE: 2017-03-24

There have been no updates to Python(x,y) since June of 2015, so I think it’s safe to assume it has been abandoned.

UPDATE: 2016-11-11

As @cxw comments below, these answers are for the same bit-versions, and by bit-version I mean 64-bit vs. 32-bit. For example, these answers would apply to updating from 64-bit Python-2.7.10 to 64-bit Python-2.7.11, ie: the same bit-version. While it is possible to install two different bit versions of Python together, it would require some hacking, so I’ll save that exercise for the reader. If you don’t want to hack, I suggest that if switching bit-versions, remove the other bit-version first.

UPDATES: 2016-05-16
  • Anaconda and MiniConda can be used with an existing Python installation by disabling the options to alter the Windows PATH and Registry. After extraction, create a symlink to conda in your bin or install conda from PyPI. Then create another symlink called conda-activate to activate in the Anaconda/Miniconda root bin folder. Now Anaconda/Miniconda is just like Ruby RVM. Just use conda-activate root to enable Anaconda/Miniconda.
  • Portable Python is no longer being developed or maintained.

TL;DR

  • Using Anaconda or miniconda, then just execute conda update --all to keep each conda environment updated,
  • same major version of official Python (e.g. 2.7.5), just install over old (e.g. 2.7.4),
  • different major version of official Python (e.g. 3.3), install side-by-side with old, set paths/associations to point to dominant (e.g. 2.7), shortcut to other (e.g. in BASH $ ln /c/Python33/python.exe python3).

The answer depends:

  1. If OP has 2.7.x and wants to install newer version of 2.7.x, then

    • if using MSI installer from the official Python website, just install over old version, installer will issue warning that it will remove and replace the older version; looking in “installed programs” in “control panel” before and after confirms that the old version has been replaced by the new version; newer versions of 2.7.x are backwards compatible so this is completely safe and therefore IMHO multiple versions of 2.7.x should never necessary.
    • if building from source, then you should probably build in a fresh, clean directory, and then point your path to the new build once it passes all tests and you are confident that it has been built successfully, but you may wish to keep the old build around because building from source may occasionally have issues. See my guide for building Python x64 on Windows 7 with SDK 7.0.
    • if installing from a distribution such as Python(x,y), see their website. Python(x,y) has been abandoned. I believe that updates can be handled from within Python(x,y) with their package manager, but updates are also included on their website. I could not find a specific reference so perhaps someone else can speak to this. Similar to ActiveState and probably Enthought, Python (x,y) clearly states it is incompatible with other installations of Python:

      It is recommended to uninstall any other Python distribution before installing Python(x,y)

    • Enthought Canopy uses an MSI and will install either into Program Files\Enthought or home\AppData\Local\Enthought\Canopy\App for all users or per user respectively. Newer installations are updated by using the built in update tool. See their documentation.
    • ActiveState also uses an MSI so newer installations can be installed on top of older ones. See their installation notes.

      Other Python 2.7 Installations On Windows, ActivePython 2.7 cannot coexist with other Python 2.7 installations (for example, a Python 2.7 build from python.org). Uninstall any other Python 2.7 installations before installing ActivePython 2.7.

    • Sage recommends that you install it into a virtual machine, and provides a Oracle VirtualBox image file that can be used for this purpose. Upgrades are handled internally by issuing the sage -upgrade command.
    • Anaconda can be updated by using the conda command:

      conda update --all
      

      Anaconda/Miniconda lets users create environments to manage multiple Python versions including Python-2.6, 2.7, 3.3, 3.4 and 3.5. The root Anaconda/Miniconda installations are currently based on either Python-2.7 or Python-3.5.

      Anaconda will likely disrupt any other Python installations. Installation uses MSI installer. [UPDATE: 2016-05-16] Anaconda and Miniconda now use .exe installers and provide options to disable Windows PATH and Registry alterations.

      Therefore Anaconda/Miniconda can be installed without disrupting existing Python installations depending on how it was installed and the options that were selected during installation. If the .exe installer is used and the options to alter Windows PATH and Registry are not disabled, then any previous Python installations will be disabled, but simply uninstalling the Anaconda/Miniconda installation should restore the original Python installation, except maybe the Windows Registry Python\PythonCore keys.

      Anaconda/Miniconda makes the following registry edits regardless of the installation options: HKCU\Software\Python\ContinuumAnalytics\ with the following keys: Help, InstallPath, Modules and PythonPath – official Python registers these keys too, but under Python\PythonCore. Also uninstallation info is registered for Anaconda\Miniconda. Unless you select the “Register with Windows” option during installation, it doesn’t create PythonCore, so integrations like Python Tools for Visual Studio do not automatically see Anaconda/Miniconda. If the option to register Anaconda/Miniconda is enabled, then I think your existing Python Windows Registry keys will be altered and uninstallation will probably not restore them.

    • WinPython updates, I think, can be handled through the WinPython Control Panel.
    • PortablePython is no longer being developed. It had no update method. Possibly updates could be unzipped into a fresh directory and then App\lib\site-packages and App\Scripts could be copied to the new installation, but if this didn’t work then reinstalling all packages might have been necessary. Use pip list to see what packages were installed and their versions. Some were installed by PortablePython. Use easy_install pip to install pip if it wasn’t installed.
  2. If OP has 2.7.x and wants to install a different version, e.g. <=2.6.x or >=3.x.x, then installing different versions side-by-side is fine. You must choose which version of Python (if any) to associate with *.py files and which you want on your path, although you should be able to set up shells with different paths if you use BASH. AFAIK 2.7.x is backwards compatible with 2.6.x, so IMHO side-by-side installs is not necessary, however Python-3.x.x is not backwards compatible, so my recommendation would be to put Python-2.7 on your path and have Python-3 be an optional version by creating a shortcut to its executable called python3 (this is a common setup on Linux). The official Python default install path on Windows is

    • C:\Python33 for 3.3.x (latest 2013-07-29)
    • C:\Python32 for 3.2.x
    • &c.
    • C:\Python27 for 2.7.x (latest 2013-07-29)
    • C:\Python26 for 2.6.x
    • &c.
  3. If OP is not updating Python, but merely updating packages, they may wish to look into virtualenv to keep the different versions of packages specific to their development projects separate. Pip is also a great tool to update packages. If packages use binary installers I usually uninstall the old package before installing the new one.

I hope this clears up any confusion.


回答 1

最好的解决方案是在多个路径中安装不同的Python版本。

例如。C:\ Python27(适用于2.7)和C:\ Python33(适用于3.3)。

阅读以获取更多信息:如何在Windows上运行多个Python版本

The best solution is to install the different Python versions in multiple paths.

eg. C:\Python27 for 2.7, and C:\Python33 for 3.3.

Read this for more info: How to run multiple Python versions on Windows


回答 2

  • 官方Python .msi安装程序旨在替代:

    • 以前的任何微型发行版(在xyz中z为“微型”),因为可以保证它们是向后兼容和二进制兼容的
    • 任何微型版本的“快照”(从源构建)安装
  • 快照安装程序旨在用较低的微型版本替换任何快照。

(见的2.X负责代码为3.X

任何其他版本不一定兼容,因此与现有版本一起安装。如果您希望卸载旧版本,则需要手动进行。并卸载您拥有的所有第三方模块:

  • 如果您从bdist_wininst软件包(Windows .exe)安装了任何模块,请在卸载版本之前先将其卸载,否则如果卸载程序具有自定义逻辑,则卸载程序可能无法正常工作
  • 安装了模块 setuptools /的pip驻留在其中,Lib\site-packages之后可以删除
  • 您为每个用户安装的软件包(如果有)驻留在该软件包中,%APPDATA%/Python/PythonXY/site-packages并且同样可以删除
  • Official Python .msi installers are designed to replace:

    • any previous micro release (in x.y.z, z is “micro”) because they are guaranteed to be backward-compatible and binary-compatible
    • a “snapshot” (built from source) installation with any micro version
  • A snapshot installer is designed to replace any snapshot with a lower micro version.

(See responsible code for 2.x, for 3.x)

Any other versions are not necessarily compatible and are thus installed alongside the existing one. If you wish to uninstall the old version, you’ll need to do that manually. And also uninstall any 3rd-party modules you had for it:

  • If you installed any modules from bdist_wininst packages (Windows .exes), uninstall them before uninstalling the version, or the uninstaller might not work correctly if it has custom logic
  • modules installed with setuptools/pip that reside in Lib\site-packages can just be deleted afterwards
  • packages that you installed per-user, if any, reside in %APPDATA%/Python/PythonXY/site-packages and can likewise be deleted

回答 3

我一直只是将新版本安装在最上面,从来没有任何问题。但是,请确保您的路径已更新为指向新版本。

I have always just installed the new version on top and never had any issues. Do make sure that your path is updated to point to the new version though.


为什么从__future__ import print_function使用会破坏Python2样式的打印?[关闭]

问题:为什么从__future__ import print_function使用会破坏Python2样式的打印?[关闭]

我是使用python编程的新手,但我尝试使用分隔符并结束打印,但这仍然给我带来语法错误。

我正在使用python 2.7。

这是我的代码:

from __future__ import print_function
import sys, os, time

for x in range(0,10):
    print x, sep=' ', end=''
    time.sleep(1)

这是错误:

$ python2 xy.py
  File "xy.py", line 5
    print x, sep=' ', end=''
          ^
SyntaxError: invalid syntax
$

I am new at programming with python, and I am trying to print out with a separator and end but it is still giving me a syntax error.

I am using python 2.7.

Here is my code:

from __future__ import print_function
import sys, os, time

for x in range(0,10):
    print x, sep=' ', end=''
    time.sleep(1)

And here is the error:

$ python2 xy.py
  File "xy.py", line 5
    print x, sep=' ', end=''
          ^
SyntaxError: invalid syntax
$

回答 0

首先,from __future__ import print_function必须是脚本中的第一行代码(除了下面提到的一些exceptions)。第二,正如其他答案所说,您现在必须print用作函数。这就是重点from __future__ import print_function;将print 功能从Python 3带入Python 2.6+。

from __future__ import print_function

import sys, os, time

for x in range(0,10):
    print(x, sep=' ', end='')  # No need for sep here, but okay :)
    time.sleep(1)

__future__语句必须位于文件的顶部,因为它们会更改语言的基本内容,因此编译器需要从一开始就了解它们。从文档中

将来的语句在编译时会得到特殊识别和处理:更改核心结构的语义通常是通过生成不同的代码来实现的。甚至可能是新功能引入了新的不兼容语法(例如新的保留字)的情况,在这种情况下,编译器可能需要以不同的方式解析模块。直到运行时才能推迟此类决策。

该文档还提到,__future__语句之前唯一可以做的事情就是模块文档字符串,注释,空白行和其他将来的语句。

First of all, from __future__ import print_function needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print as a function now. That’s the whole point of from __future__ import print_function; to bring the print function from Python 3 into Python 2.6+.

from __future__ import print_function

import sys, os, time

for x in range(0,10):
    print(x, sep=' ', end='')  # No need for sep here, but okay :)
    time.sleep(1)

__future__ statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning. From the documentation:

A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.

The documentation also mentions that the only things that can precede a __future__ statement are the module docstring, comments, blank lines, and other future statements.


在Alpine Linux上安装Pillow时,没有这样的文件或目录“ limits.h”

问题:在Alpine Linux上安装Pillow时,没有这样的文件或目录“ limits.h”

我在Raspberry Pi 2上运行alpine-linux。我正在尝试通过以下命令安装Pillow:

pip install pillow

这是命令的输出:

Installing collected packages: pillow
Running setup.py install for pillow
    Complete output from command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-gNq0WA/pillow/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-nDKwei-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-armv7l-2.7
    creating build/lib.linux-armv7l-2.7/PIL
    copying PIL/XVThumbImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/XpmImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/XbmImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/WmfImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/WebPImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/WalImageFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/TiffTags.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/TiffImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/TgaImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/TarIO.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/SunImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/SpiderImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/SgiImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PyAccess.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PSDraw.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PsdImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PpmImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PngImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PixarImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PdfImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PcxImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PcfFontFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PcdImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PalmImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PaletteFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/OleFileIO.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/MspImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/MpoImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/MpegImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/MicImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/McIdasImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/JpegPresets.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/JpegImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/Jpeg2KImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/IptcImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImtImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageWin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageTransform.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageTk.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageStat.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageShow.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageSequence.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageQt.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImagePath.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImagePalette.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageOps.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageMorph.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageMode.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageMath.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageGrab.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageFont.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageFilter.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageFileIO.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageEnhance.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageDraw2.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageDraw.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageColor.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageCms.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageChops.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/Image.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/IcoImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/IcnsImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/Hdf5StubImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GribStubImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GimpPaletteFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GimpGradientFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GifImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GdImageFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GbrImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/FpxImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/FontFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/FliImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/FitsStubImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ExifTags.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/EpsImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/DcxImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/CurImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ContainerIO.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/BufrStubImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/BmpImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/BdfFontFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/_util.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/_binary.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/__init__.py -> build/lib.linux-armv7l-2.7/PIL
    running egg_info
    writing Pillow.egg-info/PKG-INFO
    writing top-level names to Pillow.egg-info/top_level.txt
    writing dependency_links to Pillow.egg-info/dependency_links.txt
    warning: manifest_maker: standard file '-c' not found

    reading manifest file 'Pillow.egg-info/SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    warning: no files found matching 'LICENSE' under directory 'docs'
    writing manifest file 'Pillow.egg-info/SOURCES.txt'
    copying PIL/OleFileIO-README.md -> build/lib.linux-armv7l-2.7/PIL
    running build_ext
    building 'PIL._imaging' extension
    creating build/temp.linux-armv7l-2.7/libImaging
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c _imaging.c -o build/temp.linux-armv7l-2.7/_imaging.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c outline.c -o build/temp.linux-armv7l-2.7/outline.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Bands.c -o build/temp.linux-armv7l-2.7/libImaging/Bands.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/ConvertYCbCr.c -o build/temp.linux-armv7l-2.7/libImaging/ConvertYCbCr.o
    In file included from _imaging.c:76:0:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from outline.c:20:0:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/ConvertYCbCr.c:15:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Bands.c:19:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Draw.c -o build/temp.linux-armv7l-2.7/libImaging/Draw.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Filter.c -o build/temp.linux-armv7l-2.7/libImaging/Filter.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/GifEncode.c -o build/temp.linux-armv7l-2.7/libImaging/GifEncode.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/LzwDecode.c -o build/temp.linux-armv7l-2.7/libImaging/LzwDecode.o
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Draw.c:35:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Filter.c:27:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/GifEncode.c:20:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/LzwDecode.c:31:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Offset.c -o build/temp.linux-armv7l-2.7/libImaging/Offset.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Quant.c -o build/temp.linux-armv7l-2.7/libImaging/Quant.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/PcxDecode.c -o build/temp.linux-armv7l-2.7/libImaging/PcxDecode.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/RawEncode.c -o build/temp.linux-armv7l-2.7/libImaging/RawEncode.o
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Offset.c:18:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Quant.c:21:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/PcxDecode.c:17:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/RawEncode.c:21:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/UnpackYCC.c -o build/temp.linux-armv7l-2.7/libImaging/UnpackYCC.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/ZipEncode.c -o build/temp.linux-armv7l-2.7/libImaging/ZipEncode.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/BoxBlur.c -o build/temp.linux-armv7l-2.7/libImaging/BoxBlur.o
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/UnpackYCC.c:17:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/ZipEncode.c:18:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/BoxBlur.c:1:0:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    Building using 4 processes
    gcc -shared -Wl,--as-needed build/temp.linux-armv7l-2.7/_imaging.o build/temp.linux-armv7l-2.7/decode.o build/temp.linux-armv7l-2.7/encode.o build/temp.linux-armv7l-2.7/map.o build/temp.linux-armv7l-2.7/display.o build/temp.linux-armv7l-2.7/outline.o build/temp.linux-armv7l-2.7/path.o build/temp.linux-armv7l-2.7/libImaging/Access.o build/temp.linux-armv7l-2.7/libImaging/AlphaComposite.o build/temp.linux-armv7l-2.7/libImaging/Resample.o build/temp.linux-armv7l-2.7/libImaging/Bands.o build/temp.linux-armv7l-2.7/libImaging/BitDecode.o build/temp.linux-armv7l-2.7/libImaging/Blend.o build/temp.linux-armv7l-2.7/libImaging/Chops.o build/temp.linux-armv7l-2.7/libImaging/Convert.o build/temp.linux-armv7l-2.7/libImaging/ConvertYCbCr.o build/temp.linux-armv7l-2.7/libImaging/Copy.o build/temp.linux-armv7l-2.7/libImaging/Crc32.o build/temp.linux-armv7l-2.7/libImaging/Crop.o build/temp.linux-armv7l-2.7/libImaging/Dib.o build/temp.linux-armv7l-2.7/libImaging/Draw.o build/temp.linux-armv7l-2.7/libImaging/Effects.o build/temp.linux-armv7l-2.7/libImaging/EpsEncode.o build/temp.linux-armv7l-2.7/libImaging/File.o build/temp.linux-armv7l-2.7/libImaging/Fill.o build/temp.linux-armv7l-2.7/libImaging/Filter.o build/temp.linux-armv7l-2.7/libImaging/FliDecode.o build/temp.linux-armv7l-2.7/libImaging/Geometry.o build/temp.linux-armv7l-2.7/libImaging/GetBBox.o build/temp.linux-armv7l-2.7/libImaging/GifDecode.o build/temp.linux-armv7l-2.7/libImaging/GifEncode.o build/temp.linux-armv7l-2.7/libImaging/HexDecode.o build/temp.linux-armv7l-2.7/libImaging/Histo.o build/temp.linux-armv7l-2.7/libImaging/JpegDecode.o build/temp.linux-armv7l-2.7/libImaging/JpegEncode.o build/temp.linux-armv7l-2.7/libImaging/LzwDecode.o build/temp.linux-armv7l-2.7/libImaging/Matrix.o build/temp.linux-armv7l-2.7/libImaging/ModeFilter.o build/temp.linux-armv7l-2.7/libImaging/MspDecode.o build/temp.linux-armv7l-2.7/libImaging/Negative.o build/temp.linux-armv7l-2.7/libImaging/Offset.o build/temp.linux-armv7l-2.7/libImaging/Pack.o build/temp.linux-armv7l-2.7/libImaging/PackDecode.o build/temp.linux-armv7l-2.7/libImaging/Palette.o build/temp.linux-armv7l-2.7/libImaging/Paste.o build/temp.linux-armv7l-2.7/libImaging/Quant.o build/temp.linux-armv7l-2.7/libImaging/QuantOctree.o build/temp.linux-armv7l-2.7/libImaging/QuantHash.o build/temp.linux-armv7l-2.7/libImaging/QuantHeap.o build/temp.linux-armv7l-2.7/libImaging/PcdDecode.o build/temp.linux-armv7l-2.7/libImaging/PcxDecode.o build/temp.linux-armv7l-2.7/libImaging/PcxEncode.o build/temp.linux-armv7l-2.7/libImaging/Point.o build/temp.linux-armv7l-2.7/libImaging/RankFilter.o build/temp.linux-armv7l-2.7/libImaging/RawDecode.o build/temp.linux-armv7l-2.7/libImaging/RawEncode.o build/temp.linux-armv7l-2.7/libImaging/Storage.o build/temp.linux-armv7l-2.7/libImaging/SunRleDecode.o build/temp.linux-armv7l-2.7/libImaging/TgaRleDecode.o build/temp.linux-armv7l-2.7/libImaging/Unpack.o build/temp.linux-armv7l-2.7/libImaging/UnpackYCC.o build/temp.linux-armv7l-2.7/libImaging/UnsharpMask.o build/temp.linux-armv7l-2.7/libImaging/XbmDecode.o build/temp.linux-armv7l-2.7/libImaging/XbmEncode.o build/temp.linux-armv7l-2.7/libImaging/ZipDecode.o build/temp.linux-armv7l-2.7/libImaging/ZipEncode.o build/temp.linux-armv7l-2.7/libImaging/TiffDecode.o build/temp.linux-armv7l-2.7/libImaging/Incremental.o build/temp.linux-armv7l-2.7/libImaging/Jpeg2KDecode.o build/temp.linux-armv7l-2.7/libImaging/Jpeg2KEncode.o build/temp.linux-armv7l-2.7/libImaging/BoxBlur.o -L/usr/lib -L/usr/local/lib -L/usr/lib -ljpeg -lpython2.7 -o build/lib.linux-armv7l-2.7/PIL/_imaging.so
    gcc: error: build/temp.linux-armv7l-2.7/_imaging.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/decode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/encode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/map.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/display.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/outline.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/path.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Access.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/AlphaComposite.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Resample.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Bands.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/BitDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Blend.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Chops.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Convert.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/ConvertYCbCr.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Copy.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Crc32.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Crop.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Dib.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Draw.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Effects.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/EpsEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/File.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Fill.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Filter.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/FliDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Geometry.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/GetBBox.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/GifDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/GifEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/HexDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Histo.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/JpegDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/JpegEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/LzwDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Matrix.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/ModeFilter.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/MspDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Negative.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Offset.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Pack.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/PackDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Palette.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Paste.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Quant.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/QuantOctree.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/QuantHash.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/QuantHeap.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/PcdDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/PcxDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/PcxEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Point.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/RankFilter.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/RawDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/RawEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Storage.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/SunRleDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/TgaRleDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Unpack.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/UnpackYCC.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/UnsharpMask.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/XbmDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/XbmEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/ZipDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/ZipEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/TiffDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Incremental.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Jpeg2KDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Jpeg2KEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/BoxBlur.o: No such file or directory
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python -c "import setup tools, tokenize;__file__='/tmp/pip-build-gNq0WA/pillow/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-nDKwei-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-gNq0WA/pillow

我认为这可能是相关的部分:

In file included from libImaging/BoxBlur.c:1:0:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.

我的研究表明,头文件可能与此有关。我已经安装了这些:

apk add py-configobj libusb py-pip python-dev gcc linux-headers
pip install --upgrade pip
pip install -U setuptools
pip install Cheetah
pip install pyusb

I’m running alpine-linux on a Raspberry Pi 2. I’m trying to install Pillow via this command:

pip install pillow

This is the output from the command:

Installing collected packages: pillow
Running setup.py install for pillow
    Complete output from command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-gNq0WA/pillow/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-nDKwei-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-armv7l-2.7
    creating build/lib.linux-armv7l-2.7/PIL
    copying PIL/XVThumbImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/XpmImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/XbmImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/WmfImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/WebPImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/WalImageFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/TiffTags.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/TiffImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/TgaImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/TarIO.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/SunImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/SpiderImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/SgiImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PyAccess.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PSDraw.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PsdImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PpmImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PngImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PixarImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PdfImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PcxImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PcfFontFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PcdImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PalmImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/PaletteFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/OleFileIO.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/MspImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/MpoImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/MpegImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/MicImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/McIdasImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/JpegPresets.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/JpegImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/Jpeg2KImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/IptcImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImtImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageWin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageTransform.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageTk.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageStat.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageShow.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageSequence.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageQt.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImagePath.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImagePalette.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageOps.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageMorph.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageMode.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageMath.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageGrab.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageFont.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageFilter.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageFileIO.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageEnhance.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageDraw2.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageDraw.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageColor.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageCms.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ImageChops.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/Image.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/IcoImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/IcnsImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/Hdf5StubImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GribStubImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GimpPaletteFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GimpGradientFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GifImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GdImageFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/GbrImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/FpxImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/FontFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/FliImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/FitsStubImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ExifTags.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/EpsImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/DcxImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/CurImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/ContainerIO.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/BufrStubImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/BmpImagePlugin.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/BdfFontFile.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/_util.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/_binary.py -> build/lib.linux-armv7l-2.7/PIL
    copying PIL/__init__.py -> build/lib.linux-armv7l-2.7/PIL
    running egg_info
    writing Pillow.egg-info/PKG-INFO
    writing top-level names to Pillow.egg-info/top_level.txt
    writing dependency_links to Pillow.egg-info/dependency_links.txt
    warning: manifest_maker: standard file '-c' not found

    reading manifest file 'Pillow.egg-info/SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    warning: no files found matching 'LICENSE' under directory 'docs'
    writing manifest file 'Pillow.egg-info/SOURCES.txt'
    copying PIL/OleFileIO-README.md -> build/lib.linux-armv7l-2.7/PIL
    running build_ext
    building 'PIL._imaging' extension
    creating build/temp.linux-armv7l-2.7/libImaging
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c _imaging.c -o build/temp.linux-armv7l-2.7/_imaging.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c outline.c -o build/temp.linux-armv7l-2.7/outline.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Bands.c -o build/temp.linux-armv7l-2.7/libImaging/Bands.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/ConvertYCbCr.c -o build/temp.linux-armv7l-2.7/libImaging/ConvertYCbCr.o
    In file included from _imaging.c:76:0:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from outline.c:20:0:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/ConvertYCbCr.c:15:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Bands.c:19:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Draw.c -o build/temp.linux-armv7l-2.7/libImaging/Draw.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Filter.c -o build/temp.linux-armv7l-2.7/libImaging/Filter.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/GifEncode.c -o build/temp.linux-armv7l-2.7/libImaging/GifEncode.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/LzwDecode.c -o build/temp.linux-armv7l-2.7/libImaging/LzwDecode.o
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Draw.c:35:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Filter.c:27:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/GifEncode.c:20:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/LzwDecode.c:31:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Offset.c -o build/temp.linux-armv7l-2.7/libImaging/Offset.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/Quant.c -o build/temp.linux-armv7l-2.7/libImaging/Quant.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/PcxDecode.c -o build/temp.linux-armv7l-2.7/libImaging/PcxDecode.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/RawEncode.c -o build/temp.linux-armv7l-2.7/libImaging/RawEncode.o
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Offset.c:18:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/Quant.c:21:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/PcxDecode.c:17:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/RawEncode.c:21:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/UnpackYCC.c -o build/temp.linux-armv7l-2.7/libImaging/UnpackYCC.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/ZipEncode.c -o build/temp.linux-armv7l-2.7/libImaging/ZipEncode.o
    gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -DHAVE_LIBJPEG -I/tmp/pip-build-gNq0WA/pillow/libImaging -I/usr/include -I/usr/include/python2.7 -c libImaging/BoxBlur.c -o build/temp.linux-armv7l-2.7/libImaging/BoxBlur.o
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/UnpackYCC.c:17:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/ImPlatform.h:10:0,
                    from libImaging/Imaging.h:14,
                    from libImaging/ZipEncode.c:18:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    In file included from libImaging/BoxBlur.c:1:0:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.
    Building using 4 processes
    gcc -shared -Wl,--as-needed build/temp.linux-armv7l-2.7/_imaging.o build/temp.linux-armv7l-2.7/decode.o build/temp.linux-armv7l-2.7/encode.o build/temp.linux-armv7l-2.7/map.o build/temp.linux-armv7l-2.7/display.o build/temp.linux-armv7l-2.7/outline.o build/temp.linux-armv7l-2.7/path.o build/temp.linux-armv7l-2.7/libImaging/Access.o build/temp.linux-armv7l-2.7/libImaging/AlphaComposite.o build/temp.linux-armv7l-2.7/libImaging/Resample.o build/temp.linux-armv7l-2.7/libImaging/Bands.o build/temp.linux-armv7l-2.7/libImaging/BitDecode.o build/temp.linux-armv7l-2.7/libImaging/Blend.o build/temp.linux-armv7l-2.7/libImaging/Chops.o build/temp.linux-armv7l-2.7/libImaging/Convert.o build/temp.linux-armv7l-2.7/libImaging/ConvertYCbCr.o build/temp.linux-armv7l-2.7/libImaging/Copy.o build/temp.linux-armv7l-2.7/libImaging/Crc32.o build/temp.linux-armv7l-2.7/libImaging/Crop.o build/temp.linux-armv7l-2.7/libImaging/Dib.o build/temp.linux-armv7l-2.7/libImaging/Draw.o build/temp.linux-armv7l-2.7/libImaging/Effects.o build/temp.linux-armv7l-2.7/libImaging/EpsEncode.o build/temp.linux-armv7l-2.7/libImaging/File.o build/temp.linux-armv7l-2.7/libImaging/Fill.o build/temp.linux-armv7l-2.7/libImaging/Filter.o build/temp.linux-armv7l-2.7/libImaging/FliDecode.o build/temp.linux-armv7l-2.7/libImaging/Geometry.o build/temp.linux-armv7l-2.7/libImaging/GetBBox.o build/temp.linux-armv7l-2.7/libImaging/GifDecode.o build/temp.linux-armv7l-2.7/libImaging/GifEncode.o build/temp.linux-armv7l-2.7/libImaging/HexDecode.o build/temp.linux-armv7l-2.7/libImaging/Histo.o build/temp.linux-armv7l-2.7/libImaging/JpegDecode.o build/temp.linux-armv7l-2.7/libImaging/JpegEncode.o build/temp.linux-armv7l-2.7/libImaging/LzwDecode.o build/temp.linux-armv7l-2.7/libImaging/Matrix.o build/temp.linux-armv7l-2.7/libImaging/ModeFilter.o build/temp.linux-armv7l-2.7/libImaging/MspDecode.o build/temp.linux-armv7l-2.7/libImaging/Negative.o build/temp.linux-armv7l-2.7/libImaging/Offset.o build/temp.linux-armv7l-2.7/libImaging/Pack.o build/temp.linux-armv7l-2.7/libImaging/PackDecode.o build/temp.linux-armv7l-2.7/libImaging/Palette.o build/temp.linux-armv7l-2.7/libImaging/Paste.o build/temp.linux-armv7l-2.7/libImaging/Quant.o build/temp.linux-armv7l-2.7/libImaging/QuantOctree.o build/temp.linux-armv7l-2.7/libImaging/QuantHash.o build/temp.linux-armv7l-2.7/libImaging/QuantHeap.o build/temp.linux-armv7l-2.7/libImaging/PcdDecode.o build/temp.linux-armv7l-2.7/libImaging/PcxDecode.o build/temp.linux-armv7l-2.7/libImaging/PcxEncode.o build/temp.linux-armv7l-2.7/libImaging/Point.o build/temp.linux-armv7l-2.7/libImaging/RankFilter.o build/temp.linux-armv7l-2.7/libImaging/RawDecode.o build/temp.linux-armv7l-2.7/libImaging/RawEncode.o build/temp.linux-armv7l-2.7/libImaging/Storage.o build/temp.linux-armv7l-2.7/libImaging/SunRleDecode.o build/temp.linux-armv7l-2.7/libImaging/TgaRleDecode.o build/temp.linux-armv7l-2.7/libImaging/Unpack.o build/temp.linux-armv7l-2.7/libImaging/UnpackYCC.o build/temp.linux-armv7l-2.7/libImaging/UnsharpMask.o build/temp.linux-armv7l-2.7/libImaging/XbmDecode.o build/temp.linux-armv7l-2.7/libImaging/XbmEncode.o build/temp.linux-armv7l-2.7/libImaging/ZipDecode.o build/temp.linux-armv7l-2.7/libImaging/ZipEncode.o build/temp.linux-armv7l-2.7/libImaging/TiffDecode.o build/temp.linux-armv7l-2.7/libImaging/Incremental.o build/temp.linux-armv7l-2.7/libImaging/Jpeg2KDecode.o build/temp.linux-armv7l-2.7/libImaging/Jpeg2KEncode.o build/temp.linux-armv7l-2.7/libImaging/BoxBlur.o -L/usr/lib -L/usr/local/lib -L/usr/lib -ljpeg -lpython2.7 -o build/lib.linux-armv7l-2.7/PIL/_imaging.so
    gcc: error: build/temp.linux-armv7l-2.7/_imaging.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/decode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/encode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/map.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/display.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/outline.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/path.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Access.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/AlphaComposite.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Resample.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Bands.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/BitDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Blend.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Chops.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Convert.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/ConvertYCbCr.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Copy.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Crc32.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Crop.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Dib.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Draw.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Effects.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/EpsEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/File.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Fill.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Filter.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/FliDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Geometry.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/GetBBox.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/GifDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/GifEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/HexDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Histo.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/JpegDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/JpegEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/LzwDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Matrix.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/ModeFilter.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/MspDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Negative.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Offset.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Pack.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/PackDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Palette.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Paste.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Quant.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/QuantOctree.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/QuantHash.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/QuantHeap.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/PcdDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/PcxDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/PcxEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Point.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/RankFilter.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/RawDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/RawEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Storage.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/SunRleDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/TgaRleDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Unpack.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/UnpackYCC.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/UnsharpMask.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/XbmDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/XbmEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/ZipDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/ZipEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/TiffDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Incremental.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Jpeg2KDecode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/Jpeg2KEncode.o: No such file or directory
    gcc: error: build/temp.linux-armv7l-2.7/libImaging/BoxBlur.o: No such file or directory
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python -c "import setup tools, tokenize;__file__='/tmp/pip-build-gNq0WA/pillow/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-nDKwei-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-gNq0WA/pillow

I think this is probably the relevant section:

In file included from libImaging/BoxBlur.c:1:0:
    /usr/include/python2.7/Python.h:19:20: fatal error: limits.h: No such file or directory
    #include <limits.h>
                        ^
    compilation terminated.

My research shows it’s probably something with the header files. I have installed these:

apk add py-configobj libusb py-pip python-dev gcc linux-headers
pip install --upgrade pip
pip install -U setuptools
pip install Cheetah
pip install pyusb

回答 0

高山Linux使用musl libc。您可能需要安装musl-dev

Alpine Linux uses musl libc. You probably need to install musl-dev.


回答 1

@zakaria答案是正确的,但是如果您偶然发现

fatal error: linux/limits.h: No such file or directory

那么你需要的包linux-headers(注意前缀linux之前limits.h

apk add linux-headers

@zakaria answer is correct, but if you stumble upon

fatal error: linux/limits.h: No such file or directory

then you need the package linux-headers (notice the prefix linux before limits.h

apk add linux-headers

回答 2

limits.h位于libc-dev

apk add libc-dev

limits.h is located in libc-dev:

apk add libc-dev

回答 3

我在docker pyhton:3.6-alpine image,Alpine linux> = 3.3中安装python库正则表达式时遇到了非常相似的问题。

pip install regex

我必须添加gcc和musl-dev软件包

apk --no-cache add gcc musl-dev

I had very similar problem with installing python library regex in docker pyhton:3.6-alpine image, Alpine linux >= 3.3.

pip install regex

I had to add gcc and musl-dev packages

apk --no-cache add gcc musl-dev

回答 4

我发现有些python软件包无法通过pip install安装,但是如果您安装了相关的alpine linux软件包,它们可以工作。例如,pip install uwsgi无法抱怨limits.h,但是apk add uwsgi-python可以正常工作。建议尝试apk添加py-pillow而不是pip安装枕头。

I’ve found some python packages fail to install via pip install but work if you install the associated alpine linux package. For example pip install uwsgi fails complaining about limits.h, but apk add uwsgi-python works fine. Suggest trying apk add py-pillow instead of pip install pillow.


对python 2.7的支持终止了吗?

问题:对python 2.7的支持终止了吗?

当不再支持python 2.7而不再支持python 2.7时,是否存在已知的日期/时间框架?

Is there a known date/timeframe when python 2.7 will not be supported any more in favor of python 3?


回答 0

自2014年4月13日起,来自http://hg.python.org/peps/rev/76d43e52d978(PEP 373,Python 2.7发布时间表):

Python 2.7的终止生命日期(停产日期,终止日期)已移至未来五年,即2020年。此决定旨在阐明Python 2.7的状态,并减轻那些尚无法迁移到Python 3的用户的后顾之忧。另请参阅PEP 466

As of 13 Apr 2014, from http://hg.python.org/peps/rev/76d43e52d978 (PEP 373, Python 2.7 Release Schedule):

The End Of Life date (EOL, sunset date) for Python 2.7 has been moved five years into the future, to 2020. This decision was made to clarify the status of Python 2.7 and relieve worries for those users who cannot yet migrate to Python 3. See also PEP 466.


回答 1

2010年5月,神的话语为Python 2.7 PATCHLEVEL版本将可能至少6年内作出

因此,也许是2016年,也许是以后。

编辑:推回2020年。请参阅PEP 373的修订版,并在其他答案中链接。

In May 2010, Word of God was that patchlevel releases for Python 2.7 will probably be made for at least 6 years.

So, maybe 2016, probably later.

Edit: Pushed back to 2020. See the revision to PEP 373, linked to in other answers.


回答 2

最近,该日期已更新为2020年1月1日。

参见https://pythonclock.org/

Recently, that date has been updated to January 1, 2020.

see https://pythonclock.org/


回答 3

您应该仔细阅读以下内容(参考:https : //news.ycombinator.com/item ? id =7582300):

这里没有来自python-dev列表的人有很多评论,他们并不真正理解这种差异的实际含义。核心开发人员无需维持2015年后的2.7,并且大多数人不会参与其中。那部分没有改变。发生的事情是Red Hat准备削减RHEL 7发行版,AFAIK取决于您为他们支付13年支持的费用。因此,他们将需要弄清楚如何至少在2027年之前自己获得2.7的支持。RH完全有权利分叉Python,并为自己和客户保留维护补丁(Python不是copyleft)。但,他们是好人,因此,如果仍有Python项目愿意接受这些更改,也许他们愿意至少暂时进行更改。再次,这是我基于ML讨论的推测,而不是RH实​​际上说过的。可以比喻Rails LTS,它是patio11参与[0]的Rails 2.x的商业分支。不可避免地会有人介入以支持2.7,所以让我们看看如何避免出现这样的情况,即保持2.7唯一运行的唯一方法是订阅RHEL。同时,有一些大公司在Windows上广泛使用2.7(例如Enthought,Anaconda),并且认为可以找到某个人偶尔制作Windows安装程序,前提是假定Python.org仍将托管下载。因此,实际上发生的事情不是很令人兴奋。核心提交者所做的工作与将项目按原计划进行一样没有什么不同。发生的情况是,它们将使源代码控制存储库和FTP服务器中的指示灯保持打开状态,以便捕获有兴趣继续支持2.7的大公司员工的免费劳动力。另一种选择是,RH和其他供应商创建专有且昂贵的Python 2.7分支。无论如何,这种情况最终可能会发生,但是如果二进制文件仍然出现在python.org上,并且您不必要求IT部门设置SCM和错误跟踪器,那么雇主将需要更长的时间才能注意到您应该停止提供补丁。等等 发生的情况是,它们将使源代码控制存储库和FTP服务器中的指示灯保持打开状态,以便捕获有兴趣继续支持2.7的大公司员工的免费劳动力。另一种选择是,RH和其他供应商创建专有且昂贵的Python 2.7分支。无论如何,这种情况最终可能会发生,但是如果二进制文件仍然出现在python.org上,并且您不必要求IT部门设置SCM和错误跟踪器,那么雇主将需要更长的时间才能注意到您应该停止提供补丁。等等 发生的情况是,它们将使源代码控制存储库和FTP服务器中的指示灯保持打开状态,以便捕获有兴趣继续支持2.7的大公司员工的免费劳动力。另一种选择是,RH和其他供应商创建专有且昂贵的Python 2.7分支。无论如何,这种情况最终可能会发生,但是如果二进制文件仍然出现在python.org上,并且您不必要求IT部门设置SCM和错误跟踪器,那么雇主将需要更长的时间才能注意到您应该停止提供补丁。等等

you should read this carefully (ref : https://news.ycombinator.com/item?id=7582300 ):

There are a lot of comments here from people who aren’t on the python-dev list and don’t really understand what this diff actually means. The core developers are not required to maintain 2.7 post-2015, and most of them won’t be involved in it. That part hasn’t changed. What is happening is that Red Hat is preparing to cut a RHEL 7 release, which AFAIK depending on how much you pay them they support for 13 years. So they will need to figure out how to support 2.7 themselves at least through 2027. Here is where I am reading between the lines. RH are well within their right to fork Python and keep their maintenance patches to themselves and their customers (Python’s not copyleft). But, they are nice guys and so maybe they are willing to upstream their changes at least for awhile if there is still a Python project willing to accept them. Again, this is my speculation based on the ML discussion, not what RH has actually said they will do. An analogy can be made to Rails LTS, a commercial fork of Rails 2.x that patio11 was involved in [0]. Inevitably somebody is going to step in to support 2.7, and so let’s see what we can do to avoid a situation where the only way to keep running 2.7 is to subscribe to RHEL. Meanwhile, there are some large companies that use 2.7 extensively on Windows (e.g. Enthought, Anaconda) and the thinking goes that somebody can probably be found to produce a Windows installer once in awhile, assuming that Python.org will still host a download. So really what is happening here is not very exciting. The core committers aren’t doing anything different than leaving the project as originally planned. What is happening is that they will leave the lights on in the source control repository and on the FTP server, so as to capture the free labor from people at large companies who have an interest in continuing to support 2.7. The alternative is that RH and other vendors create proprietary and expensive forks of Python 2.7. That may end up happening anyway, but it will take longer for your employer to notice you should stop contributing your patches back if binaries still appear on python.org and you don’t have to ask IT to set up SCM and a bug tracker, etc.


回答 4

本文说:“当2.7发布时,2.x系列将进入五年的仅漏洞修复模式。”

因此,据我所知,Python 2.7是最后一个添加2.x功能的版本,尽管发现的错误将被修复(一段时间),但新功能仅适用于3.x版本。

This article says: “When 2.7 is released, the 2.x line will move into five years of a bug fix-only mode.”

So, as far as I see, Python 2.7 was the last 2.x feature-adding release, and though found bugs are going to be fixed (for some time), new features only go to 3.x releases.


回答 5

到2020年EOS 倒计时还不错。

There is also a pretty ominous countdown clock to the EOS at 2020.


回答 6

PEP 373(Python 2.7发布时间表)是您要求的那种信息的正式来源。

当前显示“计划的未来发布日期:”

  • 2014年5月2.7.7
  • 2.7.8 2014年11月
  • 2015年5月2.7.9
  • 在此日期之后,根据需要发布

另外,它说:“ Python 2.7的生命终止日期(停产日期,日落日期)已经移到了五年后的2020年。”

根据http://hg.python.org/peps/rev/76d43e52d978在2014年4月编辑

PEP 373 (Python 2.7 Release Schedule) is the official source for the kind of information you asked for.

It currently says “Planned future release dates:”

  • 2.7.7 May 2014
  • 2.7.8 November 2014
  • 2.7.9 May 2015
  • beyond this date, releases as needed

Also, it says “The End Of Life date (EOL, sunset date) for Python 2.7 has been moved five years into the future, to 2020.”

Edited in April 2014, according to http://hg.python.org/peps/rev/76d43e52d978


回答 7

《 Python开发人员指南》列出了从2.6版到当前版本的“ Python分支的状态 ”,包括其当前支持状态以及生命周期终止日期。

当前受支持(错误+安全修复):

  • Python 3.8(当前的master / develop分支)
  • Python 3.7
  • Python 3.6
  • Python 2.7(直到2020-01-01)

仅安全修复程序:

  • Python 3.5
  • Python 3.4

The Python Developer’s Guide lists the “Status of Python branches” from version 2.6 up to the current version, including their current support status with End-of-life dates.

Currently supported (bug + security fixes):

  • Python 3.8 (current master/development branch)
  • Python 3.7
  • Python 3.6
  • Python 2.7 (until 2020-01-01)

Security fixes only:

  • Python 3.5
  • Python 3.4

回答 8

Python 2.7将会永远存在。有太多使用它的旧代码,没有人愿意重写。已经有一个名为Tauthon的分支,但是如果这个毫无意义的截止日期成为现实,我们可能还会看到其他人。

Python 2.7 wil be around forever. There is too much old code that uses it that no one wants to rewrite. There is already a fork called Tauthon, but we may see others if this pointless deadline gets real.