-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49c99f1
commit 1fe4c47
Showing
17 changed files
with
218 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ALIBABA_CLOUD_ACCESS_KEY_ID="my_id" | ||
ALIBABA_CLOUD_ACCESS_KEY_SECRET="my_secret" | ||
|
||
export ALIBABA_CLOUD_ACCESS_KEY_ID ALIBABA_CLOUD_ACCESS_KEY_SECRET |
22 changes: 22 additions & 0 deletions
22
source/big_data/maxcompute/pyodps/pyodps_startup/odps_select.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import os | ||
from odps import ODPS | ||
|
||
def main(): | ||
# 确保 ALIBABA_CLOUD_ACCESS_KEY_ID 环境变量设置为用户 Access Key ID, | ||
# ALIBABA_CLOUD_ACCESS_KEY_SECRET 环境变量设置为用户 Access Key Secret, | ||
o = ODPS( | ||
os.getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), | ||
os.getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'), | ||
#os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], | ||
#os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'], | ||
project='project', # 项目名 | ||
endpoint='http://service.cn-hangzhou-xxx:80/api' # 官方接口 | ||
) | ||
|
||
result = o.execute_sql('SELECT * FROM my_table LIMIT 3') | ||
with result.open_reader() as reader: | ||
for record in reader: | ||
print(record) | ||
|
||
if __name__ == "__main__": | ||
main() |
2 changes: 2 additions & 0 deletions
2
source/big_data/maxcompute/pyodps/pyodps_startup/odps_select.py_output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
... | ||
TypeError: `access_id` and `secret_access_key` should be provided. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.. _python_dictionary: | ||
|
||
================================= | ||
Python 字典(dictionary) | ||
================================= | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
python_dictionary_startup.rst | ||
|
||
.. only:: subproject and html | ||
|
||
Indices | ||
======= | ||
|
||
* :ref:`genindex` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.. _python_dictionary_startup: | ||
|
||
================================ | ||
Python字典(dictionary)快速起步 | ||
================================ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.. _python_os: | ||
|
||
================================= | ||
Python os 模块 | ||
================================= | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
intro_python_os.rst | ||
python_os.environ_os.getenv.rst | ||
|
||
.. only:: subproject and html | ||
|
||
Indices | ||
======= | ||
|
||
* :ref:`genindex` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.. _intro_python_os: | ||
|
||
=================== | ||
Python os 模块 | ||
=================== | ||
|
||
Python os 模块提供了简便的和操作系统交互的能力,以及控制系统进程。这个os模块提供了跨操作系统的能力,可以工作在 :ref:`windows` , :ref:`macos` 和 :ref:`linux` 。后续我会在实践中逐步补全本文(索引) | ||
|
||
环境变量 | ||
========== | ||
|
||
:ref:`python_os.environ_os.getenv` 可以获取操作系统环境变量,这在很多程序运行时需要判断环境以及获取特定的变量(例如账号密码)非常有用 | ||
|
||
参考 | ||
====== | ||
|
||
- `DigitalOcean Tutorial: Python os module <https://www.digitalocean.com/community/tutorials/python-os-module>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.. _python_os.environ_os.getenv: | ||
|
||
=================================================== | ||
Python ``os.environ`` 对象 和 ``os.getenv()`` 方法 | ||
=================================================== | ||
|
||
``os.environ`` | ||
================= | ||
|
||
在 Python 中 ``os.environ`` 是一个用户环境变量的映射对象(mapping object)。它返回的是一个 :ref:`python_dictionary` ,其中用户的环境变量作为键(key),其值作为值(value)。由于 ``os.environ`` 是一个Python字典,所以可以执行字典的 ``get`` 和 ``set`` | ||
|
||
注意,虽然可以修改 ``os.environ`` ,但是 **任何 os.environ 修改只在当前进程有效,而不会持久化生效** | ||
|
||
- 获取环境变量的代码片段案例: | ||
|
||
.. literalinclude:: python_os.environ_os.getenv/environ_home.py | ||
:language: python | ||
:caption: 打印输出操作系统 **所有** 环境变量 | ||
|
||
环境变量获取空 ``null`` 问题 | ||
=============================== | ||
|
||
我在实践 :ref:`pyodps_startup` 遇到一个小白问题,运行 ``odps`` 测试程序报,调试发现并没有获得指定环境变量,原因就是在使用 ``os.environ`` 或者 ``os.getenv()`` ,一定要明确配置 ``export`` 指定环境变量,否则即使登陆 :ref:`bash` 看上去环境变量生效,实际 Python程序运行还是拿不到环境变量: | ||
|
||
.. literalinclude:: ../../big_data/maxcompute/pyodps/pyodps_startup/odps_env | ||
:caption: ODPS相关环境变量设置 | ||
:emphasize-lines: 4 | ||
|
||
参考 | ||
===== | ||
|
||
- `Python | os.environ object <https://www.geeksforgeeks.org/python-os-environ-object/>`_ | ||
- `Python | os.getenv() method <https://www.geeksforgeeks.org/python-os-getenv-method/>`_ |
14 changes: 14 additions & 0 deletions
14
source/python/os/python_os.environ_os.getenv/environ_home.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Python program to explain os.environ object | ||
|
||
# importing os module | ||
import os | ||
import pprint | ||
|
||
# Get the list of user's | ||
# environment variables | ||
env_var = os.environ | ||
|
||
# Print the list of user's | ||
# environment variables | ||
print("User's Environment variable:") | ||
pprint.pprint(dict(env_var), width = 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters