Category Archives: Python

ModuleNotFoundError: No module named ‘torch.utils.serialization’ [How to Solve]

 

1. There is a problem

ModuleNotFoundError: No module named 'torch.utils.serialization'

2. Analyze the reasons

it may be that the version of Python is too new to cause

3. Solutions

1. Install the older version of Python 2

2. Replace with a new version of function: torchfile 2

(1) install torchfile

pip install torchfile

(2)from torch.utils.serialization import load_lua to import torchfile

(3)load_lua to torchfile.load

How to use htmltestrunner.py to report importerror: no module named ‘stringio’ in Python 3

How to use htmltestrunner.py to report importerror: no module named ‘stringio’ in python3

1. The reason is that the official website is written in python2 syntax, and the official manually changes the official website’s htmltestrunner.py to python3 syntax: 

 

2. Modification summary: 

Line 94, change import stringio to import io

Line 539, change self. Outputbuffer = stringio. Stringio() to self. Outputbuffer = io. Stringio ()

On line 642, change if not rmap. Has_ Key (CLS): modify to if not CLS in rmap:

In line 766, change uo = O. code (‘latin-1 ‘) to uo = E

In line 772 (775 if not found), change UE = E. decode (‘latin-1 ‘) to UE = E

On line 631, print & gt> Sys. Stderr, ‘\ ntime elapsed% s’% (self. StopTime self. Starttime) is modified to print (sys. Stderr,’ \ ntime elapsed% s’% (self. StopTime self. Starttime))

Typeerror: write() argument must be STR, not bytes+

There is a problem with the file opening method. If you change the previous opening statement to binary mode, there is no problem

fo = open("temp.txt", "wb+")  #Change 'w' to 'wb+'

Pycharm Error: ImportError: No module named model_selection

first of all, I want to explain that I have this problem because I need to use train_ test_ Split method, the specific reference is:

from sklearn.model_selection import train_test_split

this error occurred:

ImportError: No module named model_selection

I checked on the Internet and found out the cause of the problem. One thing Dashen gave me was that there was a problem with my sklearn version, which was train before version v0.18_ test_ Split is placed in cross_ In the validation module, now, there are two solutions to this problem:

Method 1.

will:

from sklearn.model_selection import train_test_split

TO:

from sklearn.cross_validation import train_test_split

 

Method 2.

Upgrade sklearn to v0.18 or above

If your pychar compilation environment is Anaconda’s python, enter
in the anaconda prompt operation line

The CONDA update scikit learn command updates the version of sklearn. Before updating, you will be prompted to what version to update

Or enter PIP install — upgrade scikit learn to update the version
as well

 

 

 

Python for mac or windows: ModuleNotFoundError: No module named ‘lxml’ [How to Solve]

error

Traceback (most recent call last):

File “reptile.py”, line 4, in <module>

from lxml import etree

ModuleNotFoundError: No module named ‘lxml’

jiaxiaobindeMacBook-Pro:exercise jss$ No module named ‘lxml’

-bash: No: command not found

Mac OS python

Under an existing pip

$STATIC_DEPS=true pip install lxml

refer:

https://stackoverflow.com/questions/22674730/importerror-no-module-named-lxml-on-mac

Using the jupyter notebook (error/usr/bin/python3: no module named ipykernel) on the local browser remote server_ launcher)

In the part of CONDA virtual environment in the remote access server in the local browser, create a virtual environment called Env, and input it in the virtual environment

python -m ipykernel install --user --name env --display-name "env"

Installed kernelspec learn in/home/Amax /. Local/share/jupyter/kernels/env will be reported later

Access the directory and edit the kernel.json file (you may need to run jupyter notebook once before this file appears, during which an error is reported/usr/bin/python3: no module named ipykernel)_ Here, Jupiter notebook looks for ipykunnel from/usr/bin/python3_ Launcher, because Jupiter is installed in the CONDA virtual environment, the ipykunnel cannot be found_ (error report)

{
 "argv": [
  "/usr/bin/python3", #replaced here with the virtual environment's python,/home/amax/anaconda3/envs/env/bin/python
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

After modification, run jupyter notebook to start ipykernel successfully

ImportError: No module named ‘httplib’ [How to Solve]

Reason: the “httplib” module in Python 2. X becomes “http.Client” in Python 3. X

Original code:

import httplib
import urllib

reqheaders={
'MobileType':'Android',
'DeviceToken':'xxxxxxxxx',
'OSVersion':'1.0.3',
'AppVersion':'14',
'Host':'192.xxx.x.xxxx'}  

reqconn=httplib.HTTPConnection("192.xxx.x.xxxx")
reqconn.request("GET", "/Login?username=1416&password=123", None, reqheaders)
res=reqconn.getresponse()
print res.status,  res.reason
print res.msg
print res.read()

Modified code:

import http.client    #Modify the referenced module
import urllib

reqheaders={
'MobileType':'Android',
'DeviceToken':'xxxxxxxxx',
'OSVersion':'1.0.3',
'AppVersion':'14',
'Host':'192.xxx.x.xxxx'}  

reqconn=http.client.HTTPConnection("192.xxx.x.xxxx")  #Modify the corresponding method
reqconn.request("GET", "/Login?username=1416&password=123", None, reqheaders)
res=reqconn.getresponse()
print (res.status,  res.reason)
print (res.msg)
print (res.read())

Django exception-ImportError: No module named django.core.management

Django error-ImportError: No module named django.core.managementProblem
description:
Enter manage.py runserver on the command line, it prompts that the django.core.management module cannot be found.

Problem analysis:
1. Make sure that Django has been installed, and check the Django installation directory. django.core.management does exist
. 2. There are two versions of Python on the computer. Check that the Python version set by the environment variable has Django installed
. 3. Followed by Command line input:
python
import django
django.VERSION
prints out Django version V1.3.1

4. Continue to point 3, type in the command line:
from django.core.management import execute_manager
command input is normal! ! ! This is weird. .

5. You can determine the function of normal Django, Python If you have heard that there may be multiple versions installed version compatibility issues, try to manually specify the version of Python to run Django project, at the command line:
Python25 manage.py the runserver
problem is solved

Solution :
specified before the command manage.py runserver to use Python version, at the command line:
Python25 manage.py runserver

Summary:
When there are multiple versions of Python installed on the computer, sometimes even if the version to be run is correctly set in the environment variable, there may still be errors. At this time, you can try to specify the Python to be used on the command line, such as python25 manage.py runserver

 

modulenotfounderror: no module named ‘cv2’ [How to Solve]

PIP install OpenCV Python
in Python, OpenCV reports an error. Importerror: no module named CV2 or modulenotfounderror: no module named ‘CV2’ is solved in this way. Because I forgot to install it when I was running TensorFlow, I recorded it. If pip is not installed, install PIP:

(1) Download PIP

Enter https://pypi.python.org/pypi/pip, download the second item

(2) decompression and installation

Unzip the downloaded file (only use unzip tools under windows, such as rar, and input tar – XF pip-9.0.1.tar.gz, or tar – XF file name at the terminal under Linux), enter the unzipped folder, call out the command line window or terminal, and input it under windows

python setup.py install

Input under Linux

sudo python setup.py install

After the installation is successful, enter

pip -v

Bpython ImportError: No module named _curses [How to Solve]

Python from the command line is actually very difficult to use. Recently, I wanted to try bpython, which has features such as syntax prompt and color highlight. As a result, after sudopipinstallbpython, I ran the prompt importerror: no module named_ curses

This is embarrassing. The curses library provides a terminal independent way to control the character screen. Curses is the standard part of most UNIX like systems (including linux ), and it has been ported to windows and other systems

1. The solution for windows is as follows

Installation package address http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses

Install the corresponding package PIP install name.whl

Since my project is CentOS, the above solution is not applicable, so it is necessary to study the solution of Linux/CentOS

2. Solutions for Linux:

Use PIP (python2.7, if configure – with – insure PIP = yes, it will automatically help you install PIP) to install curses-2048 (sudo PIP install curses-2048) and continue to prompt

import curses
File “/usr/local/lib/python2.7/curses/__init__.py”, line 15, in <module>
from _curses import *
ImportError: No module named _curses

What is the reason?Why is curses installed

After careful analysis, you should find that the first time you did not install the cures module and after installation, the errors prompted are not exactly the same. When curses is not installed normally, you will be prompted that the module curses cannot be found. This time, you will be prompted no module named_ Curses, and prompts you to curses/__ init__. The error in the line of py. Actually, curves has been loaded, but there is an error in the middle of reloading

After comparing python2.6, we finally found that there was no corresponding curses Library in pythong2.7/lib-dynload

_ curses_ panel.so
_ curses.so

See the name, you may understand, originally_ Curses is this_ No doubt

Solution steps:

find/-iname "_curses*.so" 2>/dev/null
/usr/lib64/python2.6/lib-dynload/_curses.so
/usr/lib64/python2.6/lib-dynload/_curses_panel.so

find/2>/dev/null | grep 'python2.7/lib-dynload'|head -2 
/opt/soft/python-2.7.11/lib/python2.7/lib-dynload
/opt/soft/python-2.7.11/lib/python2.7/lib-dynload/math.so

cp /usr/lib64/python2.6/lib-dynload/_curses*.so /usr/local/lib/python2.7/lib-dynload/

find/-iname "_curses*.so" 2>/dev/null
/usr/lib64/python2.6/lib-dynload/_curses_panel.so
/usr/lib64/python2.6/lib-dynload/_curses.so
/usr/local/lib/python2.7/lib-dynload/_curses_panel.so
/usr/local/lib/python2.7/lib-dynload/_curses.so

bpython

That is, you copy these 2 so files from python2.6 to the lib-dynload folder of python2.7, and the problem is solved.

But, as to why pip install curses-2048 did not install these 2 so’s, it is not clear, but the problem is finally solved, and the specific reasons are subject to further analysis.

3. A pitfall under Ubuntu:

Under Ubuntu, copy the above two files from the included Python 3.5 to your own python 3.7 installation under /usr/local/lib/python3.7/lib-dynload, and make sure to change the version number in the name:

mv _curses.cpython-35m-x86_64-linux-gnu.so _curses.cpython-37m-x86_64-linux-gnu.so
mv _curses_panel.cpython-35m-x86_64-linux-gnu.so _curses_panel.cpython-37m-x86_64-linux-gnu.so

Otherwise, bpython will still report an error that the module was not found.

4. EOF occurred in violation of protocol

I frequently encounter the following error when using requests.

EOF occurred in violation of protocol (_ssl.c:600)

This error occurs on Python2 and Python3, macOS and Ubuntu. The error is related to the HTTPS handshake as seen in the error message.

This error is most fully discussed in this issue.

Through analysis, I found the cause of the problem: the security package was not properly installed when the requests library was installed.

Here is the solution (for Ubuntu): 1.

1. install the supported dev packages.

sudo apt-get install libffi-dev libssl-dev python-dev

If you do not install the above packages, you may get the following error when installing the support packages for requests.

distutils.errors.DistutilsError: Setup script exited with error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 1
—————————————-
Failed cleaning build dir for cryptography</blockquote
2. install requests and its security support packages

pip install ‘requests[security]’

requests[security] is an extension that installs the following three additional packages to support secure connections.
pyOpenSSL

ndg-httpsclient

pyasn1

5、ImportError: No module named _sqlite3

find/-name _sqlite3.so 2>/dev/null
/usr/lib64/python2.6/lib-dynload/_sqlite3.so

find/-type d -name “lib-dynload” 2>/dev/null
/usr/lib64/python2.6/lib-dynload
/opt/soft/python-2.7.11/lib/python2.7/lib-dynload
/opt/soft/python3.6/lib/python3.6/lib-dynload

6. Microsoft Visual C++ Build Tools Download/Solve Visual C++ 14.0 is Required Issue

 

 

How to Solve Beautifulsoup Error: “No module named ‘bs4’.” [from bs4 import BeautifulSoup]

When we install beautiful soup 4 in PIP, we will report an error when we import beautiful soup 4: no module named’BS4′

  pip install beautifulsoup4

Then, we write this in the python script:

  from bs4 import BeautifulSoup4

The above error “no module named’BS4′” is reported during execution

After reading some methods on the Internet, I finally solved the problem in this way (on the premise that I used pychar)

Step 1:

Step 2:

Step 3: 

Step 4:

Step 5:

Step 6: If you execute the python program again, no error will be reported