来自pip install --help
:
--user Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on
Windows. (See the Python documentation for site.USER_BASE for full details.)
Run Code Online (Sandbox Code Playgroud)
site.USER_BASE的文档是一个令人讨厌的虫洞,有趣的*NIX主题,我不明白.
--user
简明英语的目的是什么?为什么要将包裹装入~/.local/
物质?为什么不在我的$ PATH中放置一个可执行文件?
我在Windows 10上运行Python2.7,使用Anaconda进行env和大多数pkg管理.升级了许多软件包之后,我的ipython控制台现在无法在任何IDE或控制台中启动.当我尝试在控制台上运行它时,我收到此错误:
Traceback (most recent call last):
File "C:\Anaconda3\Scripts\ipython-script.py", line 3, in <module>
import IPython
File "C:\Anaconda3\lib\site-packages\IPython\__init__.py", line 48, in <module>
from .core.application import Application
File "C:\Anaconda3\lib\site-packages\IPython\core\application.py", line 24, in <module>
from IPython.core import release, crashhandler
File "C:\Anaconda3\lib\site-packages\IPython\core\crashhandler.py", line 28, in <module>
from IPython.core import ultratb
File "C:\Anaconda3\lib\site-packages\IPython\core\ultratb.py", line 121, in <module>
from IPython.utils.terminal import get_terminal_size
File "C:\Anaconda3\lib\site-packages\IPython\utils\terminal.py", line 27, in <module>
import backports.shutil_get_terminal_size
ImportError: No module named backports.shutil_get_terminal_size
Run Code Online (Sandbox Code Playgroud)
我试图做的第一件事是:
pip install --upgrade backports.shutil_get_terminal_size
Run Code Online (Sandbox Code Playgroud)
输出:
Requirement already up-to-date: backports.shutil_get_terminal_size in …
Run Code Online (Sandbox Code Playgroud) 好的,所以我打算在一个名为 的分支上工作directory-layout
,但事实证明我正在一个名为master
. 这是个问题。
我还没有表演git add .
也没有git commit -m "I've made a horrendus mistake I'm sorry"
我该怎么做才能将我的更改添加到另一个(或新的)分支,为什么?
我可能需要帮助更好地表达这个问题。我正在通过 python3.7 和一个类(称为Worker()
)编写一个异步 api 接口。Worker
有一些我想使用运行的阻塞方法loop.run_in_executor()
。
我想构建一个装饰器,我可以async
在所有非方法之上添加Worker
,但我不断遇到问题。
有人告诉我我需要await
wraps()
在下面的装饰器中:
def run_method_in_executor(func, *, loop=None):
async def wraps(*args):
_loop = loop if loop is not None else asyncio.get_event_loop()
return await _loop.run_in_executor(executor=None, func=func, *args)
return wraps
Run Code Online (Sandbox Code Playgroud)
这会抛出:
RuntimeWarning: coroutine 'run_method_in_executor.<locals>.wraps' was never awaited
我不知道如何正确地执行,await
wraps()
因为包含函数和修饰函数不是异步的。不确定这是由于误解asyncio
还是装饰器的误解。
任何帮助(或帮助澄清)将不胜感激!
yield from
我怀疑这与 b/w &的差异有关await
。然而,除了新对象被指定为 an 之外async_generator
,我还不清楚它和 a 之间的差异的后果coroutine
。(除了我在标题中提出的问题之外,我不知道还能如何问这个问题......)
import asyncio
async def async_generator_spits_out_letters():
yield 'a'
yield 'b'
yield 'c'
yield 'd'
await asyncio.sleep(0)
async def coroutine_prints_messages():
while True:
print('hi')
await asyncio.sleep(2)
def test_it():
print(type(async_generator_spits_out_letters))
print(type(coroutine_prints_messages))
# This is how I choose to do newlines....it's easier for me to read. :[
print(); print()
print(type(async_generator_spits_out_letters()))
print(type(coroutine_prints_messages()))
Run Code Online (Sandbox Code Playgroud)
这给出:
<class 'async_generator'>
<class 'coroutine'>
<class 'function'>
<class 'function'>
Run Code Online (Sandbox Code Playgroud)
我无法理解这一点......
我正在尝试克服自开始以来我一直避免使用的最终基本 Python 功能之一:装饰器。我不像使用 list-comps 那样对它进行操作,而且我不明白装饰器声明中的内部函数是如何工作的。
这是我的意思的一个例子。鉴于此块代码:
def outer(func):
def inner(*args, **kwargs):
print('Hi my name is ')
return func(*args, **kwargs)
return inner
@outer
def decorated(name):
print(name)
decorated('Bob')
Run Code Online (Sandbox Code Playgroud)
我知道这将打印
def outer(func):
def inner(*args, **kwargs):
print('Hi my name is ')
return func(*args, **kwargs)
return inner
@outer
def decorated(name):
print(name)
decorated('Bob')
Run Code Online (Sandbox Code Playgroud)
但我不明白的是如何inner
获得任何*args
或**kwargs
从decorated()
我的理解是
@outer
def decorated(name):
print(name)
decorated("Bob")
Run Code Online (Sandbox Code Playgroud)
相当于outer(decorated("Bob"))
。如果是这种情况,如何inner()
能够访问name
参数?撇开语法问题不谈,我希望内部声明看起来像def inner(func.args, func.kwargs):
这里发生了什么?我有什么误解?
我有一个字典中充满了字符串值的kv对,我希望每个==>
字符都用&分隔,然后打印到换行符.
我使用数组来调解转换,这似乎是不必要的和不优雅的.如果可能的话,我想在一行中做到这一点.
这就是我所拥有的:
foo_dict = {
'a' : '1',
'b' : '2',
'c' : '3',
}
foo_list = []
for k, v in foo_dict.items():
foo_list.append('{} ==> {}'.format(k, v))
foo_text = '\n'.join(foo_list)
Run Code Online (Sandbox Code Playgroud)
这将打印:
b ==> 2
c ==> 3
a ==> 1
Run Code Online (Sandbox Code Playgroud)
顺序无关紧要(否则我只是使用OrderedDict
.)重要的是所有kv对都以正确的格式打印.
python ×6
python-3.x ×3
asynchronous ×2
decorator ×2
anaconda ×1
branch ×1
coroutine ×1
dictionary ×1
generator ×1
git ×1
github ×1
ipython ×1
pip ×1
python-2.7 ×1
string ×1
terminal ×1
virtualenv ×1