小编hoe*_*ing的帖子

$ pytest 和 $ python -m pytest 测试之间的区别

我想了解运行之间的区别:

\n
$ pytest\n
Run Code Online (Sandbox Code Playgroud)\n

\n
$ python -m pytest tests\n
Run Code Online (Sandbox Code Playgroud)\n

我正在 Mac 上的虚拟环境中工作。\n第一个解决方案给了我一个错误:

\n
ModuleNotFoundError: No module named 'app'  \n
Run Code Online (Sandbox Code Playgroud)\n

第二种解决方案效果很好。

\n

为什么第二个解决方案有效而第一个解决方案无效?\n我怎样才能让它只使用$ pytest

\n

我的文件夹结构:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.py   \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 requirements.txt  \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 templates  \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tests  \n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 conftest.py  \n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 functional  \n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py  \n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_s.py   \n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 unit   \n\xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py   \n\xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_app.py   \n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 venv   \n
Run Code Online (Sandbox Code Playgroud)\n

在 test_app.py 中,此行失败:

\n
from app import app\n
Run Code Online (Sandbox Code Playgroud)\n

python pytest

10
推荐指数
2
解决办法
2203
查看次数

使用 OneHotEncoder 出现错误“预期的二维数组,而是得到一维数组”

我是机器学习的新手,正在尝试解决我在使用 OneHotEncoder 类时遇到的错误。错误是:“预期的二维数组,而是得到一维数组”。因此,当我想到一维数组时,它类似于:[1,4,5,6]二维数组将是[[2,3], [3,4], [5,6]],但我仍然无法弄清楚为什么会失败。在这条线上失败了:

X[:, 0] = onehotencoder1.fit_transform(X[:, 0]).toarray()
Run Code Online (Sandbox Code Playgroud)

这是我的整个代码:

# Import Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Import Dataset
dataset = pd.read_csv('Data2.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 5].values
df_X = pd.DataFrame(X)
df_y = pd.DataFrame(y)

# Replace Missing Values
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 3:5 ])
X[:, 3:5] = imputer.transform(X[:, 3:5])


# …
Run Code Online (Sandbox Code Playgroud)

python machine-learning python-3.x scikit-learn

9
推荐指数
2
解决办法
2万
查看次数

Angular 4 - 如何在输入类型中使用货币管道

我有一个HTML输入:

<input [(ngModel)]="item.value" name="inputField" type="text" />
Run Code Online (Sandbox Code Playgroud)

我想格式化它的值并使用现有的管道:

.... [(ngModel)]="item.value | currency:'USD':true" .....
Run Code Online (Sandbox Code Playgroud)

此外,我试图以下面的方式使用它,但它第一次给我理想的输出并在更新字段时显示错误:

<input type="text" 
   [ngModel]="item.value | currency:'USD':true" 
   (ngModelChange)="item.value=($event)">
Run Code Online (Sandbox Code Playgroud)

上面的代码导致以下错误.

ERROR错误:InvalidPipeArgument:''对于管道'CurrencyPipe'
在invalidPipeArgumentError(common.es5.js:2610)
处于formatPumber.webpackJsonp
.../../../common /的formatNumber(common.es5.js:3176)LandingPageComponent.webpackJsonp上的@ angular/common.es5.js.CurrencyPipe.transform(common.es5.js:3350)
.../../../../../src/app/guest-handling/landing 在handleEvent的object.eval
[as handleEvent](LandingPageComponent.html:38)的-page/landing-page.component.ts.LandingPageComponent.transformAmount(landing-page.component.ts:54)
(core.es5.js:12014) )
在callWedDebugContext(core.es5.js:13475)
的object.debugHandleEvent [as handleEvent](core.es5.js:13063)
at dispatchEvent(core.es5.js:8607)
at core.es5.js:9218

html-input angular2-ngmodel angular-pipe angular

9
推荐指数
2
解决办法
3万
查看次数

无法安装Tensorflow Mac

我检查了我的pip3和python3版本:

  (tensorflow) MacBook-Pro-de-Hector-2:tensorflow hectoresteban$ pip3 -V
    pip 10.0.1 from /Users/hectoresteban/.virtualenvs/tensorflow/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip (python 3.7)

(tensorflow) MacBook-Pro-de-Hector-2:tensorflow hectoresteban$ python3 -V
Python 3.7.0
Run Code Online (Sandbox Code Playgroud)

在我目前正在使用的虚拟环境中:

pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.9.0-py3-none-any.whl
Run Code Online (Sandbox Code Playgroud)

以标准方式pip3 install tensorflow输出以下消息:

could not find a version that satisfies the requirement tensorflow (from versions: )
Run Code Online (Sandbox Code Playgroud)

安装后使用第一种方法解释:

(tensorflow) MacBook-Pro-de-Hector-2:tensorflow hectoresteban$ python3
>>> import tensorflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/hectoresteban/.virtualenvs/tensorflow/lib/python3.7/site-packages/tensorflow/__init__.py", line 22, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/Users/hectoresteban/.virtualenvs/tensorflow/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 49, in <module> …
Run Code Online (Sandbox Code Playgroud)

python macos pip tensorflow python-3.7

9
推荐指数
1
解决办法
4415
查看次数

仅使用logback从一个类显示日志

我试图将日志显示到控制台以及文件但是在文件中我只想要从一个特定类显示的某些日志,我不知道如何做到这一点.下面是我的logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
        </encoder>
    </appender>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file> test.txt </file>
    <append>false</append>
    <encoder>
        <pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
    </encoder>
  </appender>

  <root level="info">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>

</configuration>
Run Code Online (Sandbox Code Playgroud)

logback

8
推荐指数
1
解决办法
3814
查看次数

如何将 pytest 固定装置与 Unittest 方法一起使用

class MyTestCase(unittest.Testcase):
    def setUp(self):
        self.something = True

    @pytest.fixture(autouse=True)
    def MyTestMethod(self, frozentime):
        fn(self.something)  # self.something is NOT defined
Run Code Online (Sandbox Code Playgroud)

如果我使用,@pytest.fixture(autouse=True)我最终会从 PyTest 中得到一些奇怪的行为。setUpPyTest 没有在测试方法之前调用我的方法,而是跳过setUp和 调用MyTestMethod,就好像它是一个 PyTest MyTestFunction,这当然不能很好地工作。

如何MyTestMethodfrozentime不忽略setUp应该首先调用的方法的情况下使用夹具。

class MyTestCase(unittest.Testcase):
    def setUp(self):
        self.something = True

    #@pytest.fixture(autouse=True)
    def MyTestMethod(self, frozentime): # Fails on call, because it needs too many arguments.
        fn(self.something)  
Run Code Online (Sandbox Code Playgroud)

python pytest

8
推荐指数
1
解决办法
2088
查看次数

python3中pytest的布局和导入

我无法从pytest函数导入模块。我知道有100万个问题,但是我已经读了一堆,但仍然难以理解。

$ tree
.
??? code
    ??? eight_puzzle.py
    ??? missionaries_and_cannibals.py
    ??? node.py
    ??? search.py
    ??? test
        ??? test_eight_puzzle.py
        ??? test_search.py

2 directories, 6 files
$
$ grep import code/test/test_search.py
import sys
import pytest
import code.search
$
$ pytest
...
ImportError while importing test module '~/Documents/code/test/test_search.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
code/test/test_search.py:14: in <module>
    import code.search
E   ModuleNotFoundError: No module named 'code.search'; 'code' is not a package
...
Run Code Online (Sandbox Code Playgroud)

我希望那能奏效。“代码”是一个包,对吗?Python 3中的软件包是其中包含.py文件的任何目录。

我也尝试了相对导入-- from .. import search …

python pytest python-3.x conda

8
推荐指数
1
解决办法
1226
查看次数

启动服务器时Django中的_DeadlockError

很多时候我在启动python服务器时遇到此错误。当我更改任何文件并且服务器再次启动时,也会发生这种情况。

我尝试删除环境并重新创建环境,但是仍然存在相同的错误。

Linux上的Python 3.5.2(默认值,2018年11月12日,13:43:14)[GCC 5.4.0 20160609]

的Django:2.2.1

/home/satyam/Code/myproject/myproject_apps/invoice/services/excel_service.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/satyam/Code/environments/ebapps/lib/python3.5/site-packages/django/template/utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/satyam/Code/environments/ebapps/lib/python3.5/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/satyam/Code/environments/ebapps/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 117, in …
Run Code Online (Sandbox Code Playgroud)

python django python-3.x

8
推荐指数
1
解决办法
739
查看次数

类型检查动态添加的属性

在编写特定于项目的pytest插件时,我经常发现该Config对象对于附加我自己的属性很有用。例子:

from _pytest.config import Config


def pytest_configure(config: Config) -> None:
    config.fizz = "buzz"

def pytest_unconfigure(config: Config) -> None:
    print(config.fizz)
Run Code Online (Sandbox Code Playgroud)

显然,类中没有fizz属性_pytest.config.Config,所以运行mypy上面的代码片段会产生

conftest.py:5: error: "Config" has no attribute "fizz"
conftest.py:8: error: "Config" has no attribute "fizz"
Run Code Online (Sandbox Code Playgroud)

(请注意,pytest目前还没有带有类型提示的版本,因此如果您想在本地实际重现错误,请按照此评论中的步骤安装一个 fork )。

有时重新定义用于类型检查的类可以提供快速帮助:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from _pytest.config import Config as _Config

    class Config(_Config):
        fizz: str

else:
    from _pytest.config import Config



def pytest_configure(config: Config) -> None:
    config.fizz = …
Run Code Online (Sandbox Code Playgroud)

python typechecking mypy

8
推荐指数
1
解决办法
1016
查看次数

如何在MAC OS中将python 2.7与最新的openssl版本链接?

当我跑步时:

$ openssl version -a
Run Code Online (Sandbox Code Playgroud)

我得到1.0.2k作为版本:

OpenSSL 1.0.2k  26 Jan 2017
built on: reproducible build, date unspecified
platform: darwin64-x86_64-cc
options:  bn(64,64) rc4(ptr,int) des(idx,cisc,16,int) idea(int) blowfish(idx) 
compiler: cc -I. -I.. -I../include  -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -arch x86_64 -O3 -DL_ENDIAN -Wall -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM -DECP_NISTZ256_ASM
OPENSSLDIR: "/usr/local/php5/ssl"
Run Code Online (Sandbox Code Playgroud)

但是当我用python检查版本时:

OpenSSL 1.0.2k  26 Jan 2017
built on: reproducible build, date unspecified
platform: darwin64-x86_64-cc
options:  bn(64,64) rc4(ptr,int) des(idx,cisc,16,int) idea(int) blowfish(idx) 
compiler: cc -I. -I.. -I../include …
Run Code Online (Sandbox Code Playgroud)

python macos openssl python-2.7

7
推荐指数
1
解决办法
337
查看次数