Python no module named encoding

Python3+uwsgi на Ubuntu 16.04. No module named ‘encodings’ — как узнать в чем дело?

Ubuntu 16.04, Python3, virtualenvwrapper, uwsgi. Python и uwsgi — стандарнтые системные.

Ошибка при запуске сервиса uwsgi, в логе ключевые строчки —

Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named 'encodings'

UPD. Убрал строчку
home=/root/.virtualenvs/vek/bin/python
и все заработало. В крайнем случае буду использовать голобальное окружение Питона, но в чем же все-таки проблема?

Куда только уже не рыл, мысли кончились. В чем может быть проблема и как ее вообще искать (логи детализировать, что-то еще отдиагностировать)?

Все ключевые подробности, если чего не хватает — добавлю.

# cat /etc/uwsgi/apps-enabled/vek.ini [uwsgi] plugins=python3 chdir=/srv/vek/django module=vek.wsgi:application home=/root/.virtualenvs/vek/bin/python # apt-get install uwsgi-plugin-python3 Reading package lists. Done Building dependency tree Reading state information. Done uwsgi-plugin-python3 is already the newest version (2.0.12-5ubuntu3). 0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded. # cat /var/log/uwsgi/app/vek.log . Thu Jun 23 17:12:52 2016 - *** Starting uWSGI 2.0.12-debian (64bit) on [Thu Jun 23 17:12:51 2016] *** Thu Jun 23 17:12:52 2016 - compiled with version: 5.3.1 20160412 on 13 April 2016 08:36:06 Thu Jun 23 17:12:52 2016 - os: Linux-4.4.0-24-generic #43-Ubuntu SMP Wed Jun 8 19:27:37 UTC 2016 Thu Jun 23 17:12:52 2016 - nodename: vek-dverey.ru Thu Jun 23 17:12:52 2016 - machine: x86_64 Thu Jun 23 17:12:52 2016 - clock source: unix Thu Jun 23 17:12:52 2016 - pcre jit disabled Thu Jun 23 17:12:52 2016 - detected number of CPU cores: 1 Thu Jun 23 17:12:52 2016 - current working directory: / Thu Jun 23 17:12:52 2016 - writing pidfile to /run/uwsgi/app/vek/pid Thu Jun 23 17:12:52 2016 - detected binary path: /usr/bin/uwsgi-core Thu Jun 23 17:12:52 2016 - setgid() to 33 Thu Jun 23 17:12:52 2016 - setuid() to 33 Thu Jun 23 17:12:52 2016 - chdir() to /srv/vek/django Thu Jun 23 17:12:52 2016 - your processes number limit is 3912 Thu Jun 23 17:12:52 2016 - your memory page size is 4096 bytes Thu Jun 23 17:12:52 2016 - detected max file descriptor number: 1024 Thu Jun 23 17:12:52 2016 - lock engine: pthread robust mutexes Thu Jun 23 17:12:52 2016 - thunder lock: disabled (you can enable it with --thunder-lock) Thu Jun 23 17:12:52 2016 - uwsgi socket 0 bound to UNIX address /run/uwsgi/app/vek/socket fd 3 Thu Jun 23 17:12:52 2016 - Python version: 3.5.1+ (default, Mar 30 2016, 22:46:26) [GCC 5.3.1 20160330] Thu Jun 23 17:12:52 2016 - Set PythonHome to /root/.virtualenvs/vek/bin/python Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named 'encodings' Current thread 0x00007f0abc3b7780 (most recent call first): # /root/.virtualenvs/vek/bin/python Python 3.5.1+ (default, Mar 30 2016, 22:46:26) [GCC 5.3.1 20160330] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import encodings >>> encodings >>>

Подозреваю что используется какой-то не тот Питон, но в логе uwsgi версия правильная

Читайте также:  Redirect php page to url

UPD: проверил версию неправильного именования плагина:

# uwsgi uwsgi uwsgi-core uwsgi_python3 uwsgi_python35

Видно что есть плагины uwsgi_python3 и uwsgi_python35. Попробовал изменить название плагина в ini на python35 — в логе абсолютно то же.

Источник

Python no module named encoding

Last updated: Feb 20, 2023
Reading time · 3 min

banner

# ModuleNotFoundError: No module named ‘encodings’ in Python

The Python «ModuleNotFoundError: No module named ‘encodings'» occurs for multiple reasons:

  1. Having a glitched virtual environment.
  2. Not having Python in your system’s PATH environment variable.
  3. Having a corrupted Python installation.
Copied!
Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings'

The first thing you should try is to recreate your virtual environment if you have one.

Copied!
# 👇️ optionally update your requirements.txt file pip freeze > requirements.txt # 👇️ deactivate virtual environment deactivate # 👇️ Remove the old virtual environment folder: macOS and Linux rm -rf venv # 👇️ Remove the old virtual environment folder: Windows rd /s /q "venv" # 👇️ specify correct Python version when creating virtual environment python -m venv venv # 👇️ activate on Unix or MacOS source venv/bin/activate # 👇️ activate on Windows (cmd.exe) venv\Scripts\activate.bat # 👇️ activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # 👇️ install the modules in your requirements.txt file (optional) pip install -r requirements.txt

If the python -m venv venv command doesn’t work, try the following 2 commands:

Make sure to use the correct activation command depending on your operating system and your shell.

Try running your script with the new virtual environment active.

If the error is not resolved, you might have a corrupted Python installation or Python might not be set up correctly in your system’s PATH environment variable.

# Setting Python in your system’s PATH environment variable

Here is an example of how to correctly set up Python on Windows and add Python to the PATH environment variable in a straightforward way.

Download the installer from the official python.org website.

If you have Python already installed, start the installer and click on «Modify».

click modify

You can leave the optional features ticked.

optional features

On the «Advanced Options» screen, make sure to tick the «Add Python to environment variables» option.

add python to environment variables

Once the «Add Python to environment variables» checkbox is checked, click «Install».

Now your Python installation should be set up correctly and Python should be added to your system’s PATH environment variable.

If that didn’t work, your Python installation might be corrupted.

Start the installer again and click on «Uninstall».

uninstall python

Now that you don’t have Python installed on your machine, start the installer again and make sure to tick the «Add python.exe to PATH» option.

add python exe to path

The checkbox won’t be checked by default.

Once the «Add python.exe to PATH» checkbox is checked, click on «Install Now».

After the installation, Python will be installed and configured properly.

If you are on Linux or macOS, check if you have the PYTOHNHOME environment variable set to a wrong value.

Copied!
echo $PYTOHNHOME echo $PYTHONPATH

check if pythonhome environment variable is set

If the environment variables are incorrectly set, you can try to remove them and restart your machine.

unset pythonhome environment variable

If that doesn’t help, try reinstalling Python from the official python.org website.

# Conclusion

To solve the «ModuleNotFoundError: No module named ‘encodings'» error, make sure:

  1. Your virtual environment is not glitched by recreating it.
  2. Python is set up correcting in your system’s PATH environment variable.
  3. Your Python installation is not corrupted.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Modulenotfounderror: no module named ‘encodings’

modulenotfounderror no module named encodings

In this tutorial, you will learn the solutions to resolve the error Modulenotfounderror: no module named ‘encodings’.

Which are encountered by several programmers that are new in python program.

What is encoding in Python?

In Python, the encoding refers to the process of transforming a string of characters into a sequence of bytes that can be stored in memory or written to a file.

This is essential because computers store and process data in binary format (0s and 1s) rather than as human-readable characters.

There are multiple encoding schemes available in Python, such as ASCII, UTF-8, UTF-16, and more.

The choice of encoding scheme depends on the type of characters that need to be encoded and the specific requirements of the application.

What is the usage of encodings?

The encode() method in Python is used to convert a string of characters to bytes using a detailed encoding scheme.

The following code encodes a string using the UTF-8 encoding scheme:

my_string = "Example, Hello encodings!" my_bytes = my_string.encode('utf-8')

In this example, the encode() method takes the UTF-8 encoding scheme as an argument.

It will returns a bytes object that represents the encoded version of the string.

The resulting bytes object can then be stored in memory or written to a file.

Why the error no module named encodings occur?

The “no module named encodings” error occurs because it can be caused by a number of factors, such as a corrupted Python installation, a missing module, or problems with environment variables.

Another possible cause of this error is a problem with the environment variables used by Python to locate its modules.

To resolve this issue, you can try setting the PYTHONPATH environment variable to include the path to the Python installation directory.

How to solve the error no module named ‘encodings’?

Here are the solutions to solve the error no module named ‘encodings’.

If the error is caused by a corrupted Python installation, the solution is to reinstall the Python to resolve the issue.

If the error message indicates a missing module, install the module using pip command, the package installer for Python.

For example, if the error is “no module named encodings.utf_8”, run the following command in the terminal:

pip install —user encodings

The command above will install the encodings module in your Python environment.

If the error is caused by problems with the environment variables.

Try setting the PYTHONPATH environment variable to include the path to the Python installation directory or the site-packages directory.

If you are using a virtual environment, make sure it is properly activated and the correct version of Python is being used.

Conclusion

To conclude, I hope the above solutions can help you to solve the error Modulenotfounderror: no module named ‘encodings’.

Leave a Comment Cancel reply

You must be logged in to post a comment.

Источник

Оцените статью