我想了解运行之间的区别:
\n$ pytest\nRun Code Online (Sandbox Code Playgroud)\n和
\n$ python -m pytest tests\nRun Code Online (Sandbox Code Playgroud)\n我正在 Mac 上的虚拟环境中工作。\n第一个解决方案给了我一个错误:
\nModuleNotFoundError: No module named 'app' \nRun Code Online (Sandbox Code Playgroud)\n第二种解决方案效果很好。
\n为什么第二个解决方案有效而第一个解决方案无效?\n我怎样才能让它只使用$ pytest?
我的文件夹结构:
\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 \nRun Code Online (Sandbox Code Playgroud)\n在 test_app.py 中,此行失败:
\nfrom app import app\nRun Code Online (Sandbox Code Playgroud)\n 我是机器学习的新手,正在尝试解决我在使用 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) 我有一个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
我检查了我的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) 我试图将日志显示到控制台以及文件但是在文件中我只想要从一个特定类显示的某些日志,我不知道如何做到这一点.下面是我的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) 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,这当然不能很好地工作。
如何MyTestMethod在frozentime不忽略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) 我无法从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服务器时遇到此错误。当我更改任何文件并且服务器再次启动时,也会发生这种情况。
我尝试删除环境并重新创建环境,但是仍然存在相同的错误。
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) 在编写特定于项目的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) 当我跑步时:
$ 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 ×8
pytest ×3
python-3.x ×3
macos ×2
angular ×1
angular-pipe ×1
conda ×1
django ×1
html-input ×1
logback ×1
mypy ×1
openssl ×1
pip ×1
python-2.7 ×1
python-3.7 ×1
scikit-learn ×1
tensorflow ×1
typechecking ×1