rhgrant10 / berserk

Python client for the lichess API
https://berserk.readthedocs.io
Other
142 stars 36 forks source link

berserk module not found #1

Closed sentrworks closed 6 years ago

sentrworks commented 6 years ago

Description

Berserk.py is my client to communicate with lichess.org. It works pretty well on my macbookpro but some how it doesn't work on ubuntu due to berserk package error. I tried with python 2.7 and python 3.6 and none worked. it throws ' berserk module not found' for python 3.6 whilst there is an exception on line 144 in clients.py of berserk module for python 2.7. error traces are below.

Can you support?

What I Did

prod@PROD-512mb-lon1-01:~/workspace/lichess$ pyhton Berserk.py -bash: pyhton: command not found prod@PROD-512mb-lon1-01:~/workspace/lichess$ python Berserk.py Traceback (most recent call last): File "Berserk.py", line 2, in import berserk File "/home/prod/.local/lib/python2.7/site-packages/berserk/init.py", line 10, in from .clients import Client # noqa: F401 File "/home/prod/.local/lib/python2.7/site-packages/berserk/clients.py", line 144 path = f'player/top/{count}/{perf_type}' ^ SyntaxError: invalid syntax prod@PROD-512mb-lon1-01:~/workspace/lichess$ python3.6 Berserk.py Traceback (most recent call last): File "Berserk.py", line 2, in import berserk ModuleNotFoundError: No module named 'berserk' prod@PROD-512mb-lon1-01:~/workspace/lichess$ pip

prod@PROD-512mb-lon1-01:~/workspace/lichess$ pip install berserk Requirement already satisfied: berserk in /home/prod/.local/lib/python2.7/site-packages (0.1.2) Requirement already satisfied: ndjson==0.1.0 in /home/prod/.local/lib/python2.7/site-packages (from berserk) (0.1.0) Requirement already satisfied: requests==2.18.4 in /home/prod/.local/lib/python2.7/site-packages (from berserk) (2.18.4) Requirement already satisfied: certifi>=2017.4.17 in /home/prod/.local/lib/python2.7/site-packages (from requests==2.18.4->berserk) (2018.10.15) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/prod/.local/lib/python2.7/site-packages (from requests==2.18.4->berserk) (3.0.4) Requirement already satisfied: idna<2.7,>=2.5 in /home/prod/.local/lib/python2.7/site-packages (from requests==2.18.4->berserk) (2.6) Requirement already satisfied: urllib3<1.23,>=1.21.1 in /home/prod/.local/lib/python2.7/site-packages (from requests==2.18.4->berserk) (1.22) prod@PROD-512mb-lon1-01:~/workspace/lichess$ pip3 install berserk Requirement already satisfied: berserk in /home/prod/.local/lib/python2.7/site-packages (0.1.2) Requirement already satisfied: ndjson==0.1.0 in /home/prod/.local/lib/python2.7/site-packages (from berserk) (0.1.0) Requirement already satisfied: requests==2.18.4 in /home/prod/.local/lib/python2.7/site-packages (from berserk) (2.18.4) Requirement already satisfied: certifi>=2017.4.17 in /home/prod/.local/lib/python2.7/site-packages (from requests==2.18.4->berserk) (2018.10.15) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/prod/.local/lib/python2.7/site-packages (from requests==2.18.4->berserk) (3.0.4) Requirement already satisfied: idna<2.7,>=2.5 in /home/prod/.local/lib/python2.7/site-packages (from requests==2.18.4->berserk) (2.6) Requirement already satisfied: urllib3<1.23,>=1.21.1 in /home/prod/.local/lib/python2.7/site-packages (from requests==2.18.4->berserk) (1.22) prod@PROD-512mb-lon1-01:~/workspace/lichess$ python3.6 Berserk.py Traceback (most recent call last): File "Berserk.py", line 2, in import berserk ModuleNotFoundError: No module named 'berserk'

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
rhgrant10 commented 6 years ago

So pip and python can be confusing sometimes. I think what's happening here is that you're using the wrong pip to install the dependency. Note that when you run pip3 install berserk it prints out that lots of requirements are already satisfied but the paths are for a python2.7/site-packages directory. Obviously, something isn't right there. Two things can help to avoid these problems: virtual environments and python module execution.

Let's talk about python module execution first. Python provides a -m flag that accepts a module name as an argument and invokes the module as a program. The syntax is

<PYTHON> -m <MODULE_NAME> [<ARGS>...]

Notable examples are the HTTP server for serving the current directory of files over some port and the JSON module for pretty-printing:

$ python2.7 -m SimpleHTTPServer 8000
$ python3 -m http.server 9000  # module is different for python3
$ echo '{"foo": "bar", "baz": "qux"}' | python -m json.tool
{
    "baz": "qux",
    "foo": "bar"
}

Because pip installs packages to a specific python version, I always recommend running pip as a module because it forces you to invoke it with a specific version of python:

$ python3.6 -m pip install berserk
$ python2.7 -m pip install berserk

Using this method, there's no ambiguity or confusion as to which python's site-packages directory will be used for installation.

Secondly, virtual environments are a tool for creating isolated python environments and are highly recommended both when developing or hacking on packages and when deploying to a production machine. There are many tools out there for creating environments (read up on virtualenv, pyvenv, tox, and pipenv), but here's how I would create one to work on berserk:

$ python3.6 -m venv env
$ source env/bin/activate
[env] $ pip install berserk

A couple of things to note here. Firstly, I create the environment using the venv module through a specific version of python (3.6 in this case) to avoid any confusion. Secondly, after I create the environment I "activate" it. This changes my sys path so that when I run python I get the specific version of python used to create the virtual environment. It also makes pip refer to the specific version of pip that will install packages to the site-packages directory of the python for my virtual environment. That is to say, once my virtual environment is activated, python and pip are the right python and pip without having to worry.

tl;dr: use virtual environments to isolate installations from each other and system python. Use module execution when outside of a virtual environment to remove any ambiguity.

Last tip: you can invoke the site module to see which directories it checks for installed packages:

$ python3.6 -m site
sys.path = [
    '/Users/rhgrant10/code',
    '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
    '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
    '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
    '/Users/rhgrant10/Library/Python/3.6/lib/python/site-packages',
    '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages',
]
USER_BASE: '/Users/rhgrant10/Library/Python/3.6' (exists)
USER_SITE: '/Users/rhgrant10/Library/Python/3.6/lib/python/site-packages' (exists)
ENABLE_USER_SITE: True

Let me know how you get on :)

sentrworks commented 6 years ago

Brilliant! Your first option which is forcing pip to target python3.6 worked pretty well. In addition to your suggestion, I had to use one more prerequisite step to get pip module first for python3.6.

  1. curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6
  2. python3.6 -m pip install berserk

The Berserk is a fabulous 🥇 package within both its name and functionality, now I do not need to develop another parser with BeautifulSoup (I hope :)

cheers 👍