Category Archives: Python

How to Solve Python Error : **kwargs and takes 1 positional argument but 2 were given

In Python’s function definition, you can add * * kwargs to the parameters. In short, the purpose is to allow to add parameters with indefinite parameter names , and pass parameters as dictionary . But the premise is – you must provide the parameter name

For example:

1 class C():
2     def __init__(self, **kwargs):
3         print(kwargs)

Python2.7 Install Numpy Error:is not a supported wheel on…

The error message of installing numpy in python2.7 is as follows:

C:\Python27\Scripts>pipinstall"numpy-1.9.2+mkl-cp26-none-win_amd64.whl"
numpy-1.9.2+mkl-cp26-none-win_amd64.whlisnotasupportedwheelonthisplatfor
m.

Error reason:

In fact, my Python version is 2.7 and my numpy version is 2.6, so it’s wrong

Solution:

See: file name.wheel is not supported wheel on this platform

Download the numpy installation file of the corresponding version of Python:

Download the file version cp27

Re install run:

C:\Python27\Scripts>pipinstall"numpy-1.9.2+mkl-cp26-none-win_amd64.whl"
numpy-1.9.2+mkl-cp26-none-win_amd64.whlisnotasupportedwheelonthisplatfor
m.

C:\Python27\Scripts>pipinstallC:\python-tools\numpy-1.9.2+mkl-cp27-none-win_am
d64.whl
Processingc:\python-tools\numpy-1.9.2+mkl-cp27-none-win_amd64.whl
Installingcollectedpackages:numpy
Foundexistinginstallation:numpy1.9.2
Uninstallingnumpy-1.9.2:
Successfullyuninstallednumpy-1.9.2
Successfullyinstallednumpy-1.9.2

C:\Python27\Scripts>

Successfully installed

Verify the installation success in shell:

>>>fromnumpyimport*
>>>

RuntimeError: Python is not installed as a framework [How to Solve]

Encountered such a problem when using matplotlib to plot in a virtualenv environment:

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>

in <module>
from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see ‘Working with Matplotlib in Virtual environments’ in the Matplotlib FAQ

 

It seems that it is caused by the different installation and configuration of the virtual environment and the default environment.

After searching for the error message, I found a solution on STO:

 

1. After pip installs matplotlib, it will generate a .matplotlib directory in the root directory:

➜ bin ll ~/.matplotlib
total 280
-rw-r–r– 1 me staff 78K 10 4 2015 fontList.cache
-rw-r–r– 1 me staff 59K 1 17 15:56 fontList.py3k.cache
drwxr-xr-x 2 me staff 68B 10 4 2015 tex.cache

 

2. Create a file named matplotlibrc in this directory, the content is:

backend: TkAgg

Then save and exit, restart the Python interactive interface or re-run the script, import is executed normally.

 

Django Run Error: RuntimeError: maximum recursion depth exceeded while calling a Python object

Error when starting a django project

C:\Users\jerry.home-pc\PycharmProjects\mysite>python manage.py startapp app01
Traceback (most recent call last):
File “manage.py”, line 22, in <module>
execute_from_command_line(sys.argv)
File “C:\Python27\lib\site-packages\django\core\management\__init__.py”, line 363, in execute_from_command_line
utility.execute()
File “C:\Python27\lib\site-packages\django\core\management\__init__.py”, line 337, in execute
django.setup()
File “C:\Python27\lib\site-packages\django\__init__.py”, line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File “C:\Python27\lib\site-packages\django\apps\registry.py”, line 108, in populate
app_config.import_models()
File “C:\Python27\lib\site-packages\django\apps\config.py”, line 202, in import_models

RuntimeError: maximum recursion depth exceeded while calling a Python object

Command line can not be started, pycharm also can not be started

Analyze the cause: check the system python version number:.

C:\Users\jerry.home-pc\PycharmProjects\mysite>python -V
Python 2.7

Solution: Upgrade python to 2.7.5 (referenced from: https://stackoverflow.com/questions/16369637/maximum-recursion-depth-exceeded-in-cmp-error-while-executing-python-manage-py-r?rq=1)

To view the Python version number after upgrading.

C:\Users\jerry.home-pc\PycharmProjects\mysite>python -V
Python 2.7.5

Start successfully after upgrade:

C:\Users\jerry.home-pc\PycharmProjects\mysite>python manage.py startapp app01

Method 2: modify the functools.py file to achieve the same effect (not tested, you can try it yourself if you are interested)

How to Solve Python RuntimeError: implement_array_function method already has a docstring

Recently, I began to learn Matplotlib. After installing and running the code in pychar, I will be prompted

RuntimeError: implement_array_function method already has a docstring

Online search for information, the implementation of the

pip install numpy
pip install scipy
pip install pandas
pip install matplotlib
pip install scikit-learn


Resolve the version of matplotlib from 3.2.1 to 3.0.3
pip uninstall matplotlib
pip install matplotlib==3.0.3
Solved without a hitch!

How to Solve Python RuntimeError: can’t start new thread

Obviously I just simply ran a python script for data cleaning 28W data, somehow it reported the following error.

too many threads running within your python process

The “can’t start new thread” error almost certainly due to the fact that you have already have too many threads running within your python process,

and due to a resource limit of some kind the request to create a new thread is refused. You should probably look at the number of threads you’re creating; the maximum number you will be able to create will be determined by your environment,

but it should be in the order of hundreds at least. It would probably be a good idea to re-think your architecture here;

seeing as this is running asynchronously anyhow, perhaps you could use a pool of threads to fetch resources from another site instead of always starting up a thread for every request. Another improvement to consider is your use of Thread.join and Thread.stop; this would probably be better accomplished by providing a timeout value to the constructor of HTTPSConnection.

Django auth.User.groups: (fields.E304) Reverse accessor for User.groups clashes with reverse

Brief description of the problem

In django, when creating a new User class and inheriting from the AbstractUser class, the following error occurs.
ERRORS: auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.

from django.db import models
from ..db.base_model import BaseModel
from django.contrib.auth.models import AbstractUser

# Create your models here.
class User(AbstractUser, BaseModel):
    '''User Model Class'''

    class Meta:
        db_table = 'df_user'
        verbose_name = 'User'
        verbose_name_plural = 'User'

Cause of error

This is because the new user class conflicts with Django’s own user class

Solutions

Add a line of configuration in the global setting file and use the custom model class:

AUTH_USER_MODEL = 'user.User'  #  where user is the app name and User is the model class name

A python namespace conflict, about the from import mechanism

 

from os import *
#import os

def foo():
    a = listdir("trainingDigits")
    b = open("trainingDigits/0_0.txt")

This code, if only enabled

from os import *

It will then be in

b = open(“trainingDigits/0_0.txt”)

This location reports

TypeError: Required argument ‘flags’ (pos 2) not found

If you enable only

import os

Name error: name ‘listdir’ is not defined will be reported at a = listdir (“training digits”)

The solution is

import os

def foo():
    a = os.listdir("trainingDigits")
    b = open("trainingDigits/0_0.txt")

[Solved] requests.exceptions.InvalidSchema: No connection adapters were found for

At the beginning of learning, when using requests, I knocked on demo

1 import requests
2 
3 params = {
4     "name": "name",
5     "password": "gao"
6 }
7 
8 res = requests.get("127.0.0.1:8000", params=params)

There was an error in the result

C:\Users\gaoyu\Envs\py_path1\Scripts\python.exe D:/python3.6/spider/demo/requests_test.py
Traceback (most recent call last):
  File "D:/python3.6/spider/demo/requests_test.py", line 8, in <module>
    res = requests.get("127.0.0.1:8000", params=params)
  File "C:\Users\gaoyu\Envs\py_path1\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\gaoyu\Envs\py_path1\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\gaoyu\Envs\py_path1\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\gaoyu\Envs\py_path1\lib\site-packages\requests\sessions.py", line 640, in send
    adapter = self.get_adapter(url=request.url)
  File "C:\Users\gaoyu\Envs\py_path1\lib\site-packages\requests\sessions.py", line 731, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for '127.0.0.1:8000'

Finally, it is found that the reason is very simple, that is, the URL needs to be preceded by http://