【pipが使えない?】Cloud9でtweepyをインストールする方法

【pipが使えない?】Cloud9でtweepyをインストールする方法



TwitterのAPIを利用するライブラリであるTweepyのインストール方法です。Cloud9環境で実施しています。確認できたエラー、それに対する対応についてもまとめています。

✓目次

Twitter API登録

以下の記事が大変参考になります。

customer API keys(API keyとAPI secret key)とAccess token、Access token secretを取得しましょう。

Cloud9でTweepyのインストール

以降はAWSが提供しているIDE環境であるCloud9環境での手順になります。

Cloud9の環境をPython3環境にする

まずは、cloud9の環境をpython3の環境に変更します。

でなければpipのバージョンとpythonのバージョンがあわなくて、tweepyがインストールできません。以下の記事を参考に、環境設定を行いましょう。

Cloud9のターミナルで、python -Vコマンドを実施し、以下の状態になったらOKです。

1
2
3
4
5
ec2-user:~\environment $ python -V
Python 3.6.8
ec2-user:~\environment $ pip -V
pip 9.0.3 from \usr\lib\python3.6\dist-packages (python 3.6)
ec2-user:~\environment $

Tweepyをインストール

tweepyをpip install tweepyコマンドでインストールすると、下記エラーになります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
ec2-user:~\environment $ pip install tweepy

Exception:
Traceback (most recent call last):
File "\usr\lib\python3.6\dist-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "\usr\lib\python3.6\dist-packages\pip\commands\install.py", line 342, in run
prefix=options.prefix_path,
File "\usr\lib\python3.6\dist-packages\pip\req\req_set.py", line 784, in install
**kwargs
File "\usr\lib\python3.6\dist-packages\pip\req\req_install.py", line 851, in install
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File "\usr\lib\python3.6\dist-packages\pip\req\req_install.py", line 1064, in move_wheel_files
isolated=self.isolated,
File "\usr\lib\python3.6\dist-packages\pip\wheel.py", line 345, in move_wheel_files
clobber(source, lib_dir, True)
File "\usr\lib\python3.6\dist-packages\pip\wheel.py", line 316, in clobber
ensure_dir(destdir)
File "\usr\lib\python3.6\dist-packages\pip\utils\__init__.py", line 83, in ensure_dir
os.makedirs(path)
File "\usr\lib64\python3.6\os.py", line 220, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '\usr\lib\python3.6\dist-packages\urllib3'
You are using pip version 9.0.3, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
  • pipをpip install --upgrade pipコマンドで、アップグレードしなさい、というメッセージなので、 以下のコマンドでpipをアップグレード
1
ec2-user:~\environment $ sudo pip install --upgrade pip
  • pipコマンドを実行してみると、なぜかpipが使えなくなってしまいました。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
ec2-user:~ $ pip
Traceback (most recent call last):
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 658, in _build_master
ws.require(__requires__)
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 972, in require
needed = self.resolve(parse_requirements(requirements))
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 863, in resolve
raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (pip 19.3.1 (\usr\local\lib\python3.6\site-packages), Requirement.parse('pip==9.0.3'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "\usr\bin\pip", line 6, in <module>
from pkg_resources import load_entry_point
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 3049, in <module>
@_call_aside
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 3033, in _call_aside
f(*args, **kwargs)
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 3062, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 660, in _build_master
return cls._build_from_requirements(__requires__)
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 673, in _build_from_requirements
dists = ws.resolve(reqs, Environment())
File "\usr\lib\python3.6\dist-packages\pkg_resources\__init__.py", line 858, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==9.0.3' distribution was not found and is required by the application
  • whichコマンドにallオプションをつけてpipの場所を探します。
1
2
3
ec2-user:~ $ which -a pip
\usr\local\bin\pip
\usr\bin\pip

pipのインストールディレクトリがおかしかったようです。

この場合、python -m pipという風に-mオプションをつければOKです。

1
2
ec2-user:~ $ python -m pip -V
pip 19.3.1 from \usr\local\lib\python3.6\site-packages\pip (python 3.6)

改めてtweepyをインストール。

--userをつけてね、とエラーが出たので以下のコマンドでTweepyをインストールします。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ec2-user:~ $ python -m pip install tweepy --user
Collecting tweepy
Using cached https:\\files.pythonhosted.org\packages\36\1b\2bd38043d22ade352fc3d3902cf30ce0e2f4bf285be3b304a2782a767aec\tweepy-3.8.0-py2.py3-none-any.whl
Requirement already satisfied: six>=1.10.0 in \usr\local\lib\python3.6\site-packages (from tweepy) (1.13.0)
Collecting requests>=2.11.1
Using cached https:\\files.pythonhosted.org\packages\51\bd\23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb\requests-2.22.0-py2.py3-none-any.whl
Collecting PySocks>=1.5.7
Using cached https:\\files.pythonhosted.org\packages\8d\59\b4572118e098ac8e46e399a1dd0f2d85403ce8bbaad9ec79373ed6badaf9\PySocks-1.7.1-py3-none-any.whl
Collecting requests-oauthlib>=0.7.0
Using cached https:\\files.pythonhosted.org\packages\a3\12\b92740d845ab62ea4edf04d2f4164d82532b5a0b03836d4d4e71c6f3d379\requests_oauthlib-1.3.0-py2.py3-none-any.whl
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
Using cached https:\\files.pythonhosted.org\packages\b4\40\a9837291310ee1ccc242ceb6ebfd9eb21539649f193a7c8c86ba15b98539\urllib3-1.25.7-py2.py3-none-any.whl
Collecting idna<2.9,>=2.5
Using cached https:\\files.pythonhosted.org\packages\14\2c\cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9\idna-2.8-py2.py3-none-any.whl
Collecting certifi>=2017.4.17
Using cached https:\\files.pythonhosted.org\packages\b9\63\df50cac98ea0d5b006c55a399c3bf1db9da7b5a24de7890bc9cfd5dd9e99\certifi-2019.11.28-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2
Using cached https:\\files.pythonhosted.org\packages\bc\a9\01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8\chardet-3.0.4-py2.py3-none-any.whl
Collecting oauthlib>=3.0.0
Using cached https:\\files.pythonhosted.org\packages\05\57\ce2e7a8fa7c0afb54a0581b14a65b56e62b5759dbc98e80627142b8a3704\oauthlib-3.1.0-py2.py3-none-any.whl
Installing collected packages: urllib3, idna, certifi, chardet, requests, PySocks, oauthlib, requests-oauthlib, tweepy
Successfully installed PySocks-1.7.1 certifi-2019.11.28 chardet-3.0.4 idna-2.8 oauthlib-3.1.0 requests-2.22.0 requests-oauthlib-1.3.0 tweepy-3.8.0 urllib3-1.25.7

ちゃんとインストールされているか確認します。

インタプリタモードで確認します。

1
2
3
4
5
ec2-user:~ $ python
Python 3.6.8 (default, Oct 14 2019, 21:22:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tweepy

Tweepyのversionも確認してみます。

1
2
ec2-user:~ $ python -m pip list | grep tweepy
tweepy 3.8.0

Tweepyがインストールできていることが確認できました。


APIを活用したアプリケーション講座

  • Rubyでビットコインを自動で売買するプログラムを作成できるようになります。bitFlyerのAPIドキュメントをマスターし、1人で開発ができるようになります。こちらの講座もCloud9を活用しているので、複雑な環境構築などは不要です。

Rubyで作る! ビットコイン自動売買システム

  • 絶対に躓かないオンラインプログラミング講座「SkillHacks」もおすすめです。LINE@による無制限質問サポートで挫折しない仕組みができており、合計94本のしっかりした動画があります。他のプログラミングスクールよりも安くて手頃です。

-Skill Hacks- 動画で学ぶWebアプリ開発講座

コメント