Python beginners tried it in 3 days from OS installation to running Twitter API

This is the article on the 18th day of dots girls Advent Calendar 2015.

Nice to meet you, this is 373_mina_mi. Qiita's first post suddenly got quite nervous on the Advent calendar. But write something to rllllho! !! I was told, so I will do my best to write it. ~~ It's too sloppy, but it's all because of Mr. rllllho who introduced me! ~~

About this article

With that said, it's a great article for anyone who earnestly raises the points that were caught when Python de amateurs created the development environment and moved the sample code in 3 days (actual work time 10h). I hope the person who gets caught with the same error will be saved ...

Execution environment

Basically Information gathering from Twitter with Python (Environment construction) Gathering information from Twitter with Python (Twitter API) I referred to the page of. Perhaps you can usually do it quickly with just that reference page ...

The procedure that finally worked

The error location and error details are difficult to see, so I summarized them later.

Build Python 2.7.9 from source code

Does OpenSSL not work with Python 2.7.6 when an error occurs? I found a typical sentence, so I put it back in. I tried it later and it worked on 2.7.6, so I don't think it's necessary here.


Beforehand


#Install OpenSSL
$sudo apt-get install libssl-dev

#install zlib
$ sudo apt-get install zlib1g-dev

Build from the source file with the necessary items in

$sudo wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
$tar xvzf Python-2.7.9.tgz
$cd Python-2.7.9
$./configure --prefix=/opt/python2.7.9 --enable-unicode=ucs4 --with-zlib --with-ensurepip=install
$make 
$sudo make install

This completes the installation.

$ /opt/python2.7.9/bin/python -V
Python 2.7.9

Also, I confirmed that Python 2.7.9 was installed properly!

Create an environment by using different Python versions with virtualenv

Install virtualenv

sudo apt-get install python-dev python-virtualenv

This time, I created a twi-py directory directly under my home directory and built a Python 2.7.9 environment. If you execute it obediently as it is, an error will occur.

$sudo easy_install virtualenv==1.10.1

After running

$virtualenv --python=/opt/python2.7.9/bin/python twi-py

To switch the Python environment, move to the created twi-py and then

$ source bin/activate

Now if the prompt says (twi-py), it's OK. If you check the Python version in this state, it should be 2.7.9!

Put Python library for OAuth authentication with pip install

With the above module installed Gathering information from Twitter with Python (Twitter API) I will omit it because it goes quickly on the street. If you don't enter, the error festival will be at the bottom ...

Executing sample code

Gathering information from Twitter with Python (Twitter API) Because it goes quickly on the street, ry I searched for `` dots. Women's Club''

(twi-py)***@***-VirtualBox:~/twi-py$ python twi-sample1.py 
tweet_id: 676300237133090817
created_at: Mon Dec 14 07:18:36 +0000 2015
user_id: 1703590964
user_desc: 「dots.Is an engineer study session information service that allows you to search for study sessions with the content you are interested in whenever and wherever you want to know.
screen_name: eventdots
user_name: eventdots

For the time being, dots. Seems to have been caught. I'd like to understand the parameters properly and try various things again, but this time I'm here for the time being! This was the limit to make it in time for the Advent Calender _ (: 3 ”∠) _




Where and the cause of the error

so. It took three days to expose the cause that finally moved the sample. I can't deny anyone's advantage, but I'm sure there are people in the world who will encounter the same error ...! !!

Until ubuntu14.04 installation

"Just install VirtualBox and put Ubuntu on it" When I thought about it, the first thing that caught me was the screen resolution.

For some reason, the screen size cannot be changed from 640 x 480. The result How to change the screen size (resolution) of Ubuntu installed in VirtualBox to something other than 640x480 I found it and solved it safely by doing this street.

I can't create an environment with virtualenv

virtualenv --python=/opt/python2.7.9/bin/python twi-py When I try, I get an error.


***@***VirtualBox:~$ virtualenv --python=/opt/python2.7.9/bin/python twi-py
Running virtualenv with interpreter /opt/python2.7.9/bin/python
New python executable in twi-py/bin/python
Installing setuptools, pip...
  Complete output from command /home/Home directory/twi-py/bin/python -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip:
  Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/share/python-virtualenv/pip-1.5debian1-py2.py3-none-any.whl/pip/__init__.py", line 10, in <module>
  File "/usr/share/python-virtualenv/pip-1.5debian1-py2.py3-none-any.whl/pip/util.py", line 18, in <module>
  File "/usr/share/python-virtualenv/pip-1.5debian1-py2.py3-none-any.whl/pip/_vendor/distlib/version.py", line 14, in <module>
  File "/usr/share/python-virtualenv/pip-1.5debian1-py2.py3-none-any.whl/pip/_vendor/distlib/compat.py", line 31, in <module>
ImportError: cannot import name HTTPSHandler
----------------------------------------
...Installing setuptools, pip...done.
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 2339, in <module>
    main()
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 825, in main
    symlink=options.symlink)
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 993, in create_environment
    install_wheel(to_install, py_executable, search_dirs)
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 961, in install_wheel
    'PIP_NO_INDEX': '1'
  File "/usr/lib/python2.7/dist-packages/virtualenv.py", line 903, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /home/Home directory/twi-py/bin/python -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip failed with error code 1

As conclusion [[Error countermeasures] OS Error: Command ・ ・ ・ "import sys, pip; sys ... d "] + sys.argv [1:])) "setuptools pip failed with error code 1](http: // qiita .com / fantm21 / items / 05946b81df4f7ab35377) As I wrote in the above success example, referring to

$sudo easy_install virtualenv==1.10.1

After executing virtuslenv, it was built.

I feel like I was angry that I didn't have zlib in the first place, but I forgot the details because I failed to get the log ...

I can't pip install

It seems that pip is originally included in Python 2.7.9, but When I try to pip install according to the article I referred to

(twi-py)***@***VirtualBox:~/twi-py$ pip install requests requests_oauthlib
Traceback (most recent call last):
  File "/home/Home directory/twi-py/bin/pip", line 9, in <module>
    load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
  File "/home/Home directory/twi-py/lib/python2.7/site-packages/pkg_resources.py", line 378, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/Home directory/twi-py/lib/python2.7/site-packages/pkg_resources.py", line 2566, in load_entry_point
    return ep.load()
  File "/home/Home directory/twi-py/lib/python2.7/site-packages/pkg_resources.py", line 2260, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/home/Home directory/twi-py/lib/python2.7/site-packages/pip/__init__.py", line 10, in <module>
    from pip.util import get_installed_distributions, get_prog
  File "/home/Home directory/twi-py/lib/python2.7/site-packages/pip/util.py", line 17, in <module>
    from pip.vendor.distlib import version
  File "/home/Home directory/twi-py/lib/python2.7/site-packages/pip/vendor/distlib/version.py", line 13, in <module>
    from .compat import string_types
  File "/home/Home directory/twi-py/lib/python2.7/site-packages/pip/vendor/distlib/compat.py", line 31, in <module>
    from urllib2 import (Request, urlopen, URLError, HTTPError,
ImportError: cannot import name HTTPSHandler

It seems that pip is not included by default, so I checked Install Python 2.7.9 and Python 3.4.x with pip. Install pip referring to. If you try, this time I was angry without open ssl. Also, when I tried to create an environment with virtualenv, I felt angry that there was no zlib. So, in conclusion Beforehand


#Install OpenSSL
$sudo apt-get install libssl-dev

#install zlib
$ sudo apt-get install zlib1g-dev

Then re-install Python from configure! So, I arrived at the success example I wrote above.

Error when trying to run sample file

Finally, you can go through pip-install and run the sample! !! The error I encountered when I thought.

(twi-py)***@***VirtualBox:~/twi-py$ python twi-sample1.py
Error code: 401
Traceback (most recent call last):
  File "twi-sample1.py", line 63, in <module>
    main()
  File "twi-sample1.py", line 19, in main
    for tweet in tweets["statuses"]:
TypeError: 'NoneType' object has no attribute '__getitem__'

TypeError: 'NoneType' object has no attribute '__getitem__'? Is the object type different? But this doesn't seem to be a module error? ~~ * I'm a Python beginner who gets angry at the end of every sentence;

So, the conclusion. It was a copy of the API Key ...

2015-12-18 23_07_37-ubuntu (スナップショット 1) [実行中] - Oracle VM VirtualBox.png

And after this was discovered and the sample was successfully executed in Python 2.7.9, Maybe ... I thought I would make a virtualenv with Python 2.7.6 in the same way ... It worked as expected. I'm scared of copypemis! !! !! However, I got an error due to SSL or something, so I think it was because I didn't put that area properly with apt-get.

However, it took me more than two days to get Python 2.7.9 into pip, so if I could copy it properly, I felt like I could do something like that in three days, so I'll do my best. I'm depressed. But is it a hobby ...

Recommended Posts

Python beginners tried it in 3 days from OS installation to running Twitter API
I tried to create API list.csv in Python from swagger.yaml
Python beginners tried implementing REST API in one day
I tried to touch Python (installation)
[Lambda] [Python] Post to Twitter from Lambda!
Access the Twitter API in Python
python beginners tried to find out
processing to use notMNIST data in Python (and tried to classify it)
Everything from building a Python environment to running it on Windows
Collecting information from Twitter with Python (Twitter API)
I tried to implement PLSA in Python
I tried to implement PLSA in Python 2
Tweet using the Twitter API in Python
I tried using UnityCloudBuild API from Python
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
I tried to implement ADALINE in Python
I tried to implement PPO in Python
Convert from Markdown to HTML in Python
I tried to refactor the template code posted in "Getting images from Flickr API with Python" (Part 2)
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
Introduction to Graph Database Neo4j in Python for Beginners (for Mac OS X)
I tried to automate internal operations with Docker, Python and Twitter API + bonus
From file to graph drawing in Python. Elementary elementary
Try to calculate RPN in Python (for beginners)
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
From easy git installation to docker startup python
I tried to automatically collect erotic images from Twitter using GCP's Cloud Vision API
python beginners tried to predict the number of criminals
[For beginners] How to use say command in python!
Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
App development to tweet in Python from Visual Studio 2017
Push notifications from Python to Android using Google's API
I tried to implement a pseudo pachislot in Python
RaspberryPi3 (STRETCH) setup from OS installation to Hello World
From python to running instance on google cloud platform
I tried to implement Dragon Quest poker in Python
I tried to implement GA (genetic algorithm) in Python
From buying a computer to running a program with python
Try to delete tweets in bulk using Twitter API
Convert callback-style asynchronous API to async / await in Python
Python development environment construction 2020 [From Python installation to poetry introduction]
Deep nesting in Python makes it hard to read
I tried to summarize how to use pandas in python
I was able to repeat it in Python: lambda
Python OpenCV tried to display the image in text.
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Evernote API in Python
C API in Python 3
Try to make it using GUI and PyQt in Python
I tried to implement a one-dimensional cellular automaton in Python
How to slice a block multiple array from a multiple array in Python
Get your heart rate from the fitbit API in Python!
I tried follow management with Twitter API and Python (easy)
[Ipdb] Web development beginners tried to summarize debugging with Python
I tried "How to get a method decorated in Python"
How to sample from any probability density function in Python
[Python] I tried to visualize the follow relationship of Twitter
[For beginners] Script within 10 lines (4. Connection from python to sqlite3)