我还在学习python以及使用第三方模块的所有不同方法.我已经安装了https://pypi.python.org/pypi/mysqlclient,这里推荐Python 3和MySQL
我相信我正确安装了包
D:\install\python modules>python -m pip install mysqlclient-1.3.6-cp34-none-win_amd64.whl
Unpacking d:\install\python modules\mysqlclient-1.3.6-cp34-none-win_amd64.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient
Cleaning up...
Run Code Online (Sandbox Code Playgroud)
奇怪的是当我尝试导入模块mysqlclient时,我得到以下内容
D:\install\python modules>python
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysqlclient
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'mysqlclient'
Run Code Online (Sandbox Code Playgroud)
我检查了主页https://github.com/PyMySQL/mysqlclient-python,我找不到任何关于如何使用这个模块的例子.我很困惑,我刚刚错过了这里的船吗?
以下是我的管理视图:
@admin.register(AuditStashAwsMasterPolicies)
class AuditPoliciesAdmin(reversion.VersionAdmin):
exclude = ['source_policy_path', 'source_state', 'target_state']
readonly_fields = ['comparison_date', 'source', 'source_policy_name', 'target', 'target_policy_name',
'target_policy_path', 'policy_difference']
def policy_difference(self, instance):
return drift.compare_two_policies(instance.source, instance.source_policy_name, instance.source_policy_path,
instance.target, instance.target_policy_name, instance.target_policy_path)
Run Code Online (Sandbox Code Playgroud)
我想要做的是在我的“policy_difference”只读字段中添加一些帮助文本。从帮助文档中,我只能通过修改模型并在那里创建一个带有帮助文本的只读字段来做到这一点。
问题是我没有在“policy_difference”字段中存储任何值,我只是即时生成它,并希望避免将其存储在模型中。
有什么方法可以在不更改模型 AuditStashAwsMasterPolicies 的情况下将文本添加到“policy_difference”只读字段?
我已经对这个错误进行了大量的谷歌搜索,并将其归结为我正在使用的数据库采用不同的编码.
我正在使用的AIX服务器正在运行
psql 8.2.4
server_encoding | LATIN1 | | Client Connection Defaults / Locale and Formatting | Sets the server (database) character set encoding.
Run Code Online (Sandbox Code Playgroud)
我正在使用的Windows 2008 R2服务器正在运行
psql(9.3.4)
CREATE DATABASE postgres
WITH OWNER = postgres
ENCODING = 'UTF8'
TABLESPACE = pg_default
LC_COLLATE = 'English_Australia.1252'
LC_CTYPE = 'English_Australia.1252'
CONNECTION LIMIT = -1;
COMMENT ON DATABASE postgres
IS 'default administrative connection database';
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试执行我的下面的python脚本时,我得到了这个错误
Traceback (most recent call last):
File "datamain.py", line 39, in <module>
sys.exit(main())
File "datamain.py", line 33, in main
write_file_to_table("cms_jobdef.txt", …
Run Code Online (Sandbox Code Playgroud) 我在网上找到了很多关于如何将本地文件附加到电子邮件的例子.我想要做的是将像对象这样的文件附加到电子邮件中.你为什么问?所以我不必处理清理文件.下面是我的代码和我的错误.经过大量的谷歌搜索后,我仍然没有设法让它工作,任何帮助将不胜感激:)
def email_sup_teams(team_name, contact_list, file_attachemnt):
message_list = []
for jobs in file_attachemnt:
for k, v in jobs.items():
message_list.append(v + ',')
attachment_text = "\n".join(message_list)
print(type(attachment_text))
msg = MIMEText(' Failed jobs list. Please see attachment')
msg['Subject'] = 'Not run Jobs for ' + team_name
msg['From'] = 'a@b.com'
msg['To'] = 'c@d.com'
f = io.StringIO(attachment_text)
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename='test_attach')
msg.attach(attachment)
s = smtplib.SMTP('smlsmtp')
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
print('\n' + team_name + ' Email Sent')
Run Code Online (Sandbox Code Playgroud)
错误:
<class 'str'>
Traceback (most recent call last): …
Run Code Online (Sandbox Code Playgroud) 我正在尝试找到一个非常好的 python 习惯用法,以最“pythonic”的方式使用 aws boto3 分页器。以下是我能想到的最好的,但我仍然不满意。关于如何使分页更简单的任何想法,可能不使用while True:
?
import boto3
client = boto3.client('acm', region_name='ap-southeast-2')
paginator = client.get_paginator('list_certificates')
response_iterator = paginator.paginate()
while True:
for certificates in response_iterator:
for certificate in certificates['CertificateSummaryList']:
print(certificate)
if response_iterator.resume_token:
response_iterator = paginator.paginate(
PaginationConfig={
'StartingToken': response_iterator.resume_token
})
else:
break
Run Code Online (Sandbox Code Playgroud) 我已经对该错误进行了一些研究,无法真正了解发生的情况。据我了解,我基本上会遇到问题,因为我正在从一种编码类型转换为另一种编码类型。
def write_table_to_file(table, connection):
db_table = io.StringIO()
cur = connection.cursor()
#pdb.set_trace()
cur.copy_to(db_table, table)
cur.close()
return db_tabl
Run Code Online (Sandbox Code Playgroud)
这是给我头疼的方法。当我运行此方法时,输出以下错误
[u350932@config5290vm0 python3]$ python3 datamain.py
Traceback (most recent call last):
File "datamain.py", line 48, in <module>
sys.exit(main())
File "datamain.py", line 40, in main
t = write_table_to_file("cms_jobdef", con_tctmsv64)
File "datamain.py", line 19, in write_table_to_file
cur.copy_to(db_table, table)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 40: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
从中检索表的数据库上的客户端编码为
tctmsv64=> SHOW CLIENT_ENCODING;
client_encoding
-----------------
sql_ascii
(1 row)
Run Code Online (Sandbox Code Playgroud)
数据库编码为LATIN1
我输入的数据库的编码是
S104838=# SHOW CLIENT_ENCODING;
client_encoding …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Python 在我的服务器上进行一些基本的模块设置。这有点困难,因为我无法访问互联网。
这是我的代码
import sys
import os
from subprocess import CalledProcessError, STDOUT, check_output
def run_in_path(command, dir_path, env_var=''):
env_var = os.environ["PATH"] = os.environ["PATH"] + env_var
print(env_var)
try:
p = check_output(command, cwd=dir_path, stderr=STDOUT)
except CalledProcessError as e:
sys.stderr.write(e.output.decode("utf-8"))
sys.stderr.flush()
return e.returncode
else:
return 0
def main():
requests_install = run_in_path('python setup.py build',
'D:\installed_software\python modules\kennethreitz-requests-e95e173')
SQL_install = run_in_path('python setup.py install', # install SQL module pypyodbc
'D:\installed_software\python modules\pypyodbc-1.3.3\pypyodbc-1.3.3')
setup_tools = run_in_path('python setup.py install', # install setup tools
'D:\installed_software\python modules\setuptools-17.1.1')
psycopg2 = run_in_path('easy_install psycopg2-2.6.1.win-amd64-py3.3-pg9.4.4-release', # …
Run Code Online (Sandbox Code Playgroud) 我无法终生弄清楚为什么我无法捕获此异常。
在这里查看本指南。
def get_team_names(get_team_id_url, team_id):
print(get_team_id_url + team_id)
try:
response = urllib.request.urlopen(get_team_id_url + team_id)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read())
except urllib.error.URLError as e:
print(e.code)
print(e.read())
Run Code Online (Sandbox Code Playgroud)
例外:
Traceback (most recent call last):
File "queue_cleaner_main.py", line 60, in <module>
sys.exit(main())
File "queue_cleaner_main.py", line 57, in main
team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL, team[2])
File "D:\oppssup\old_job\queue_cleaner_functions.py", line 132, in get_team_names
response = urllib.request.urlopen(get_team_id_url + team_id)
File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 455, in open
response = …
Run Code Online (Sandbox Code Playgroud) 我有这个方法
def do_sh_shell_command(string_command, env_variables=None):
cmd = shlex.split(string_command)
try:
p = subprocess.check_output(string_command, shell=True,
env=env_variables) # shell=True means sh shell used
except subprocess.CalledProcessError as e:
print 'Error running command: ' + '"' + e.cmd + '"' + ' see above shell error'
print 'Return code: ' + str(e.returncode)
return e.returncode, e.cmd
return 0, p
Run Code Online (Sandbox Code Playgroud)
哪个工作但由于某种原因不返回specfici命令的错误输出
def hold_ajf_job(job_order_id):
#print 'ctmpsm -UPDATEAJF ' + job_order_id + ' HOLD'
return do_sh_shell_command('ctmpsm -UPDATEAJF ' + job_order_id + ' HOLD')
hold_ajf_job('0e4ba')
do_sh_shell_command('lsl')
Run Code Online (Sandbox Code Playgroud)
输出:
ctmpsm -UPDATEAJF 0e4ba HOLD …
Run Code Online (Sandbox Code Playgroud) 我试图计算一个elb中的实例.这是我的Ansible剧本:
- name: Get elb facts
local_action:
module: ec2_elb_facts
name: "{{elb}}"
region: "{{ansible_ec2_placement_region}}"
environment: creds
register: elb_facts
- debug:
var: elb_facts
verbosity: 2
- debug:
msg: "Instance: {{ item.instances }}"
with_items: "{{ elb_facts.elbs }}"
Run Code Online (Sandbox Code Playgroud)
和我的输出(敏感数据已删除):
TASK: [debug ] ****************************************************************
ok: [10.0.0.0] => {
"elb_facts": {
"changed": false,
"elbs": [
{
"availability_zones": [
"ap-southeast-2b",
"ap-southeast-2a"
],
"dns_name": "elbname123.ap-southeast-2.elb.amazonaws.com",
"health_check": {
"healthy_threshold": 2,
"interval": 10,
"target": "TCP:0000",
"timeout": 5,
"unhealthy_threshold": 2
},
"instances": [
{
"id": "i-000000000000000",
"state": null
}
],
"name": …
Run Code Online (Sandbox Code Playgroud) 所以下面是我的项目文件结构:
??? main.tf
??? tunnel
? ??? main.tf
? ??? variables.tf
??? variables.tf
Run Code Online (Sandbox Code Playgroud)
我正在尝试按照此处所述在 Terraform 0.15.1 中使用多个提供程序 -> https://www.terraform.io/docs/language/modules/develop/providers.html
按照示例操作后,我无法使其正常工作。我现在已经简化了我的代码,只使用一个提供程序别名(尽可能简单)。我得到的错误是:
?
? Error: Missing required argument
?
? The argument "region" is required, but was not set.
?
Run Code Online (Sandbox Code Playgroud)
我在根目录中的 main.tf 文件:
module "tunnel" {
source = "./tunnel"
providers = {
aws.r = aws.requester
}
}
Run Code Online (Sandbox Code Playgroud)
我在根目录中的 variables.tf:
provider "aws" {
alias = "requester"
region = "ap-southeast-2"
profile = "benchmark"
}
Run Code Online (Sandbox Code Playgroud)
我的隧道/variables.tf 文件:
terraform {
required_providers {
aws = {
source …
Run Code Online (Sandbox Code Playgroud) python ×3
python-3.x ×3
encoding ×2
postgresql ×2
python-2.7 ×2
subprocess ×2
utf-8 ×2
ansible ×1
boto3 ×1
django ×1
django-1.7 ×1
exception ×1
file ×1
latin1 ×1
mysql ×1
pagination ×1
pymysql ×1
python-3.3 ×1
python-3.4 ×1
smtp ×1
terraform ×1
urllib ×1