LoginSignup
6
7

More than 5 years have passed since last update.

python3でurllibが使えなかった話

Last updated at Posted at 2018-07-06

なぜ書くことにしたか

僕はpython3で色々サービスを作っているのですが、その際にurllibがpython3で動かなかったので、
調べてなおすことにしたからです。

案外urllib3を知らない人が多かったw

状態

mac os High Sierra 10.13.5
pythonの構築はpyenv

Alioth:~ sizumita$ pyenv versions
  system
* 3.5.1 (set by /Users/sizumita/.pyenv/version)

3.6.3をインストールしてみる

Alioth:~ sizumita$ pyenv install 3.6.3
Downloading Python-3.6.3.tar.xz...
...
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

ギャッ

あれっ?

Terminalから実行するとき、pyenv使ってるからpythonとpython3で実行できるよな?

python3でやってみた

"""Alioth:~ sizumita$ python3
Python 3.5.1 (default, Oct 13 2017, 20:28:35) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information."""
>>> import urllib.request
>>> with urllib.request.urlopen('http://python-remrin.hatenadiary.jp/') as f:
...     print(f.read(40))
... 
b'<!DOCTYPE html>\n<html\n  lang="ja"\n\ndata-'
>>> 

できた!

やってみての仮説と結果

・pyenvを使うとpythonとpython3の二つのコマンドでpython3を起動できる
・pythonだとpython2の方のライブラリが読み込まれる(と思ってる)
・コマンドラインからpython3でurllibを使うときは、python3コマンドで入るべし?

この謎を誰かわかるかたご教授ください。

それでは。

追記 urlopenが使えない理由

参考 https://maku77.github.io/python/http-request-with-basic-auth.html
引用
urllib.request モジュールを使って Basic 認証の設定された Web サイトにアクセスすると、デフォルトでは下記のようなエラーが発生します。

ということです。

なぜかpythonコマンドでも使えるようになってた..

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7