Pipenv: Always get `ModuleNotFoundError: No module named 'encodings'` error.

Created on 24 Feb 2018  ·  12Comments  ·  Source: pypa/pipenv

I tried to use twitter-scraper. I ran the command described in the README:

pipenv install twitter-scraper

I got this error back:

❯ pipenv install twitter-scraper
Installing twitter-scraper…
⠇
Error:  An error occurred while installing twitter-scraper!
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fff793b5000 (most recent call first):

I searched online for ModuleNotFoundError: No module named 'encodings' and could not find anything I could use to solve this.

Describe your environment
  1. OS Type: macOS
  2. Python version: Python 3.7.0b1
  3. Pipenv version: version 10.0.0
Expected result

pipenv works and doesn't throw that error.

Actual result
~/dev/testing/py-test
❯ pipenv install twitter-scraper --verbose
Installing twitter-scraper…
⠋Installing 'twitter-scraper'
$ "/Users/nikivi/.local/share/virtualenvs/py-test-7jzMe1F-/bin/pip" install   --verbose   "twitter-scraper" -i https://pypi.python.org/simple --exists-action w
⠧
Error:  An error occurred while installing twitter-scraper!
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fff793b5000 (most recent call first):
Steps to replicate

I ran pipenv install twitter-scraper. But it seems that in my case all instances of pipenv install are broken. Not sure what to do.

Thank you for any help.

All 12 comments

Can you provide the output of env please?

TERM_SESSION_ID=w0t10p0:9FBA7577-7853-4D9A-BCEC-0948259FC886
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.kMqysZVlhb/Listeners
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.Kz7uU36JWh/Render
COLORFGBG=15;0
ITERM_PROFILE=Default
XPC_FLAGS=0x0
LANG=en_GB.UTF-8
PWD=/Users/nikivi/dev/testing/py-test
SHELL=/bin/zsh
SECURITYSESSIONID=186a7
TERM_PROGRAM_VERSION=3.2.20180130-nightly
TERM_PROGRAM=iTerm.app
PATH=./node_modules/.bin/:/Users/nikivi/.yarn/bin:/usr/local/opt/ruby/bin:/usr/local/bin:/usr/local/sbin:/Users/nikivi/.nix-profile/bin:/Users/nikivi/.nix-profile/sbin:/Users/nikivi/.nix-profile/lib/kde4/libexec:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/nix/var/nix/profiles/default/lib/kde4/libexec:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Applications/Server.app/Contents/ServerRoot/usr/bin:/Applications/Server.app/Contents/ServerRoot/usr/sbin:/usr/local/git/bin:/usr/local/go/bin:/usr/local/MacGPG2/bin:/Applications/Postgres.app/Contents/Versions/latest/bin:/Library/TeX/texbin:/Users/nikivi/.dotfiles/bin:/Users/nikivi/go/bin:/Users/nikivi/.cargo/bin:/Users/nikivi/.fzf/bin
DISPLAY=/private/tmp/com.apple.launchd.gmsuvcxL3r/org.macosforge.xquartz:0
COLORTERM=truecolor
COMMAND_MODE=unix2003
TERM=xterm-256color
HOME=/Users/nikivi
TMPDIR=/var/folders/1h/fwb8ld896k92llzx3vytcmtm0000gn/T/
USER=nikivi
XPC_SERVICE_NAME=0
LOGNAME=nikivi
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
ITERM_SESSION_ID=w0t10p0:9FBA7577-7853-4D9A-BCEC-0948259FC886
SHLVL=1
OLDPWD=/Users/nikivi/Desktop
NIX_REMOTE=daemon
NIX_USER_PROFILE_DIR=/nix/var/nix/profiles/per-user/nikivi
NIX_PROFILES=/nix/var/nix/profiles/default /Users/nikivi/.nix-profile
NIX_SSL_CERT_FILE=/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt
NIX_PATH=/nix/var/nix/profiles/per-user/root/channels
EDITOR=nvim
SUDO_EDITOR=nvim
DOTFILES=/Users/nikivi/.dotfiles
HOMEBREW_AUTO_UPDATE_SECS=3600
FZF_DEFAULT_COMMAND=rg --files --no-ignore --hidden --follow -g "!{.git,node_modules}/*" 2> /dev/null
FZF_CTRL_T_COMMAND=rg --files --no-ignore --hidden --follow -g "!{.git,node_modules}/*" 2> /dev/null
NODE_PATH=/usr/local/lib/node_modules
ETC_ZSH_ZSHRC=1
PROMPT_EOL_MARK=
VIRTUAL_ENV_DISABLE_PROMPT=1
KEYTIMEOUT=1
_=/usr/bin/env

Does it work on 3.6? I'm skeptical of it having been tested on 3.7 beta, we aren't really testing pipenv on 3.7 either yet so I'm not sure which of these two complexities is the source of the issue.

Can you install anything using pipenv on 3.7?

Can you install anything using pipenv on 3.7?

I believe no. It seems to always fail.

Should I wait for pipenv to get updated? Or there is something wrong with my system?

As I mentioned, we aren't testing pipenv on 3.7 yet (although your issue inspired me to build 3.7), beyond that there are many variables in play related to how $PYTHONPATH is set and interpreted, and OSX has some weird encoding related issues.

That said, With 3.7 installed, I was able to install pipenv, then (once I installed the development bindings for libxml2 and libxslt) I was able to install twitter-scraper using pipenv into an environment running python 3.7.0b1

Erm, as I type this it does occur to me that in order for pipenv to find your pyenv pythons, you need to follow the documentation and set PYENV_ROOT and you should also set PIPENV_DEFAULT_PYTHON_VERSION if you want pipenv to use a default version when creating environments (for instance this is set to 3.6.4 for me).

I just tried running pipenv from Python 3.6 and I still get the same error.

~/dev/testing/py-test
❯ python --version
Python 3.6.4

~/dev/testing/py-test
❯ pipenv install twitter-scraper
Creating a Pipfile for this project…
Installing twitter-scraper…
⠦
Error:  An error occurred while installing twitter-scraper!
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fff793b5000 (most recent call first):

Really wish I can solve this as pipenv seems incredibly useful tool.

It seems that pipenv can't find the python at all when making the virtual env:

~/dev/testing/py-test
❯ pipenv shell
Spawning environment shell (/bin/zsh). Use 'exit' to leave.
. /Users/nikivi/.local/share/virtualenvs/py-test-7jzMe1F-/bin/activate

~/dev/testing/py-test
❯ . /Users/nikivi/.local/share/virtualenvs/py-test-7jzMe1F-/bin/activate

~/dev/testing/py-test
py-test-7jzMe1F- ❯ python --version
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Python 3.6.2 :: Anaconda, Inc.

~/dev/testing/py-test
py-test-7jzMe1F- ❯

It tries to Python 3.6.2 :: Anaconda, Inc. which I probably don't have. What can I do to get around this?

it looks like something's wrong with your python installation

I tried reinstalling python with homebrew. I also installed the latest Python from python.org.

Not really sure what I can do besides this. 😞

Okay, I realised my mistake. Apparently you cannot run pipenv in a directory that has other files. I ran the command in empty dir and everything worked.

Hi @kennethreitz

I wanted to make sure I do actually have pipenv working as intended. Is this normal that if I have a .py file already in a directory, pipenv fails with that message of ModuleNotFoundError: No module named 'encodings'?

Was this page helpful?
0 / 5 - 0 ratings