Tag Archives: python

Record pyinnstaller package file execution error modulenotfoundererror: no module named ‘cmath’ solution

Problem description

After packaging Python scripts with pyinnstaller, the executable file reported an error modulenotfoundererror: no module named ‘cmath’. However, I did not use cmath. After checking, I found that cmath was implicitly imported by default after panda version 1.2 (there is a description link here), but pyinnstaller did not check that cmath needs to be executed in the scripts, So it’s not packaged into an executable.

Solution

    similar to the errors caused by the inclusion of pandas or other libraries mentioned above, you can try to change the version of the corresponding library and modify the packaged. Spec configuration file (such as modification method). If not many dependencies are missing, this simple method is recommended to display the package with import error prompt in your own script

Use xx [‘xx ‘] = XX to set field value or does not support field: XXX

Demand:

After the tiem object is created, modify the written table of the original item_ Name value

My item:

class People(scrapy.Item):
    table_name = 'people'
    id = scrapy.Field()
    url_token = scrapy.Field()
    name = scrapy.Field()

Solution:

people = People()
people.__class__.table_name='people_20216'

Source code analysis:

If this is what the item says

table_name= scrapy.Field()

After creation, you can directly assign values in this way, which is normal operation

people = People()

people['table_name'] = 'people_20216'

Situation 1:

But if you write like me:

class People(scrapy.Item):
    table_name = 'people'

In addition, it can be modified as follows:

people[‘table_ name’] = ‘people_ 20216’

You will report an error:

Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\site-packages\twisted\internet\defer.py", line 662, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "C:\Program Files\Python39\lib\site-packages\scrapy\utils\defer.py", line 150, in f
    return deferred_from_coro(coro_f(*coro_args, **coro_kwargs))
  File "E:\codedata\gitee\pySpace\study\npoms\npoms\pipelines.py", line 62, in process_item
    item['table_name'] = item.table_name + "_" + mysql_split_util.current_year_month
  File "C:\Program Files\Python39\lib\site-packages\scrapy\item.py", line 100, in __setitem__
    raise KeyError(f"{self.__class__.__name__} does not support field: {key}")

Corresponding to this section of the original code, the source code covers this:

Case 2:

If you write like this

people.table_ name’= ‘people_ 20216’

You will report an error:

Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\site-packages\twisted\internet\defer.py", line 662, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "C:\Program Files\Python39\lib\site-packages\scrapy\utils\defer.py", line 150, in f
    return deferred_from_coro(coro_f(*coro_args, **coro_kwargs))
  File "E:\codedata\gitee\pySpace\study\npoms\npoms\pipelines.py", line 63, in process_item
    item.set_table_name(table_name)
  File "E:\codedata\gitee\pySpace\study\npoms\npoms\items.py", line 66, in set_table_name
    self.table_name = table_name
  File "C:\Program Files\Python39\lib\site-packages\scrapy\item.py", line 112, in __setattr__
    raise AttributeError(f"Use item[{name!r}] = {value!r} to set field value")
AttributeError: Use item['table_name'] = 'people_202106' to set field value

Corresponding to this section of the original code, the source code also has corresponding interception:

At this time, we need to look at the creation phase of the source code object

The object creation phase is divided into fields and classes, which we can use above__ setitem__ Method to modify the object in fields. So we need to get the class object and modify the values of the properties in the class.

Anaconda Matplotlib drawing Chinese garbled solution

I found a lot of methods to solve the Chinese garbled code of Matplotlib in Anaconda on the Internet. I feel it is not concise enough. Now let’s make a summary. Two steps, one is to find the font, the other is to set it.

1. Font

The system font file is usually saved in the   C:\Windows\Fonts

Open copy any font:

Save it in the TTF folder. Lu Jin’s name is e::’anaconda ‘,’lib’,’site-packages’,’matplotlib ‘,’mpl-data’,’fonts’,’ttf ‘

You can change the file name of Anaconda installed on your computer, and the following path is the same.

So the font is chosen.

2. Setting

Go back to MPL data and open the matplotlibrc file: e:// Anaconda/lib/site packages/Matplotlib/MPL data

Notepad shows about 249 lines, the upper middle part of the file:

Remove the comment in front of font. Family, and then change the following name to the newly copied font name: simhei.

Save with Ctrl + s and reopen the compiler.

[Solved] AttributeError: ‘Manager‘ object has no attribute ‘get_by_natural_key‘

Problem description


E:\SweetYaya\MyProj03>python manage.py createsuperuser
Identifier: 12
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Program Files\Python36\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "D:\Program Files\Python36\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 113, in handle
    error_msg = self._validate_username(username, verbose_field_name, database)
  File "D:\Program Files\Python36\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 234, in _validate_username
    self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
AttributeError: 'Manager' object has no attribute 'get_by_natural_key'

terms of settlement


Add to the defined model

class SysUsers(models.Model):
    
	...
	# add
    objects = UserManager()
	
	...

[Solved] Plugin requires missing class loader for ‘Python‘

plugin requires missing class loader for ‘Python’

Original Cause Solution

Original cause

Remove the original pycharm on linux, reinstall tar.zip, and run pycharm.sh in the bin directory and get an error

solve

The reason for this is that there are residual files left over when removing pycharm.
After deleting the ~/.local/share/JetBrains and ~/.cache/JetBrains folders, re-run pycharm.sh and you're good to go.

[Solved] Backtrader_plotting Error: RuntimeError: Unexpected data type

During installation and use of backtrader_plotting, an error was reported.
Traceback (most recent call last):
File “main.py”, line 86, in
cerebro.plot(b)
File “C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py”, line 991, in plot
start=start, end=end, use=use)
File “C:\ProgramData\Anaconda3\lib\site-packages\backtrader_plotting-2.0.0-py3.7.egg\backtrader_plotting\bokeh\bokeh.py”,
line 516, in plot
self._blueprint_strategy(obj, start, end, tradingdomain, **kwargs)
File “C:\ProgramData\Anaconda3\lib\site-packages\backtrader_plotting-2.0.0-py3.7.egg\backtrader_plotting\bokeh\bokeh.py”,
line 228, in _blueprint_strategy
figureenv.plot(master)
File “C:\ProgramData\Anaconda3\lib\site-packages\backtrader_plotting-2.0.0-py3.7.egg\backtrader_plotting\bokeh\figure.py”, line 180, in plot
self.plot_data(obj)
File “C:\ProgramData\Anaconda3\lib\site-packages\backtrader_plotting-2.0.0-py3.7.egg\backtrader_plotting\bokeh\figure.py”, line 253, in plot_data
title = sanitize_source_name(labelizer.label(data))
File “C:\ProgramData\Anaconda3\lib\site-packages\backtrader_plotting-2.0.0-py3.7.egg\backtrader_plotting\bokeh\labelizer.py”, line 84, in label
primary = _label_datafeed(obj)
File “C:\ProgramData\Anaconda3\lib\site-packages\backtrader_plotting-2.0.0-py3.7.egg\backtrader_plotting\bokeh\labelizer.py”, line 52, in _label_datafeed
raise RuntimeError(f’Unexpected data type: {data.__class__}’)
RuntimeError: Unexpected data type:

Check that it is the getattr(data, n, “”) statement that cannot get the name of the data, so it needs to be actively assigned, modifying the main code.
# Load data
cerebro.adddata(data)
for
# Load data
codename=’510300′
cerebro.adddata(data,name=codename)
Problem solved.

 

Postgres Multiple data insertion error: The ‘default‘ dialect with current database version settings does not support

Error report of inserting multiple data into postges database by Python

Solutions for error reporting related information

Information related to error reporting

Error report content: the 'default' dialect with current database version settings does not support
Python version: 3.7
psotgres version: 13
specific situation: single data insertion is normal, multiple data insertion may report errors once, but not every time.

Solution

At first, I thought there was a problem with the database version, but I still couldn’t change Postgres 10

[Solved] AttributeError: module ‘tensorflow‘ has no attribute ‘distributions‘

Recently, I was watching Mo fan’s PPO code running problems

norm_dist = tf.distributions.Normal(loc=mu, scale=sigma)

report errors:

AttributeError: module 'tensorflow' has no attribute 'distributions'

TF. Distributions is one of the core components provided by tensorflow. It is used to implement some common probability distributions and gives a series of auxiliary calculation functions.

The reason for the problem:
I didn’t look carefully. Mo fan’s version of TensorFlow is TensorFlow r1.3
his version is too old. It’s better to upgrade 1.2.0 to the same version
pay attention to the version of the code in the future

urllib3 (1.26.4) or chardet (4.0.0) doesn‘t match a supported version!

Python script execution requests dependent package warning:

C:\Python36\lib\site-packages\requests\__ init__. py:80: RequestsDependencyWarning: urllib3 (1.26.4) or chardet (4.0.0) doesn’t match a supported version! RequestsDependencyWarning)

Reason: the version of urllib3 (1.26.4) or chardet (4.0.0) in Python library is not compatible
– – – –
let’s talk about the solution first: this prompt means that the current version of urllib3 is 1.26.4 and chardet is 4.0.0__ init__ The version required in the file does not match. Follow the prompts to open the__ init__ File, line 80, you can see that there are detailed regulations for the versions of these two files, which are between the two version numbers. Here, my urllib3 or chardet is higher than the version number specified in the file, so I need to update the requests version to a higher version. Update command to   pip install upgrade requests.

Let’s talk about the background of writing this article: last week, I debugged the interface and called the requests method very smoothly. Today, when I suddenly executed this interface again, I reported the initial error. At the beginning, I didn’t know what it meant, so I went to the Internet to search, and then I started to try. For example, someone suggested using the uninstall method to unload urllib and chardet. So I just brain, regardless of all, first to unload the urllib. It’s OK to uninstall it. When I install it again, I can’t understand the error reported. Maybe the requests library can’t run normally. Well, use PIP install third party library name again     To install, who knows that my computer has turned on the agent by default when I don’t pay attention to it, so all kinds of errors that failed to install PIP pop up. There is no screenshot. The main idea is that the server host is inconsistent with the proxy host. Because of this mistake, it is another online search solution. Finally, it is found. The answer is concise and accurate. It is precisely located at the problem: the computer agent has been opened. Hurry to shut down the agent So: PIP install urllib3 is successful, PIP install upgrade requests is successful.

Time: it took me about one day to solve this problem, and finally let me solve it. It can also be regarded as a bit of harvest on the way of learning interface automation. I know how to solve this problem in the future.

Perfect, sprinkle flowers

Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“

Alipay’s online payment function has recently been developed to download and pay the SDK package. The wrong presentation is as follows:
reported false prompts: Microsoft Visual C++ 14 or greater is required. Get it it ” https://visualstudio.microsoft.com/visual-cpp-build-tools/

Reason: python3 is compiled with VC + + 14, python27 is compiled with C + + 9, and the package to install python3 needs to be compiled with C + + 14 or above

Don’t panic for small scenes

That’s the thing above. Double click the installation. Do you think it’s over?

In case of no accident, most of the installation packages you download will finally prompt you: the installation package is missing or damaged
some netizens say that it can succeed with a ladder, and others say that it can’t succeed
I uploaded an offline version here. You can try