Category Archives: Python

[Solved] TypeError: xx takes 1 positional argument but 4 were given

The problem is that there is an error when the thread transmits data. For example, the following sentence will report an error when it runs

threading.Thread(target=intent, args=([1, 2, 3, 4])).start()

The solution is to add a comma to the last face, as follows

    threading.Thread(target=intent, args=([1, 2, 3, 4],)).start()
    # or
    threading.Thread(target=intent, args=[[1, 2, 3, 4], ]).start()

[Solved] Selenium python send_key error: list object has no attribute

Learning selenium positioning element, the teacher wrote send_ Key (), but I didn’t click in this method. I kept prompting
selenium Python send_ Key error: list object has no attribute
therefore, both methods can be used to solve the problem.
PS: [0] I don’t know what it means. I’ll come back to understand it when I’m in-depth learning

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
userID = driver.find_elements(By.ID,"kw")[0]
userID.send_keys("123")
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.ID,"kw").send_keys("456")

How to Solve Turtle_tf Error in ROS

Software configuration

Operating system: Ubuntu 16.04
ROS version: kinetic
Python version: python2.7, python3.5, python3.8

Function package and routine

Refer to “ROS robot development and practice” section 4.2.3 using turtle simulator routine turtle_ TF, execute the command:

roslaunch turtle_tf turtle_tf_demo.launch

report errors

Traceback (most recent call last):
   File "/opt/ros/kinetic/lib/turtle_tf/turtle_tf_broadcaster.py", line 37, in <module>
        import tf   
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf/__init__.py", line 28, in <module>
        from tf2_ros import TransformException as Exception, ConnectivityException, LookupException, ExtrapolationException
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf2_ros/__init__.py", line 38, in <module>     
        from tf2_py import *   
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf2_py/__init__.py", line 38, in <module>     
        from ._tf2 import * ImportError: dynamic module does not define module export function (PyInit__tf2) Traceback (most recent call last):   
   File "/opt/ros/kinetic/lib/turtle_tf/turtle_tf_broadcaster.py", line 37, in <module>     
        import tf   
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf/__init__.py", line 28, in <module>    
        from tf2_ros import TransformException as Exception, ConnectivityException, LookupException, ExtrapolationException   
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf2_ros/__init__.py", line 38, in <module>     
        from tf2_py import *  
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf2_py/__init__.py", line 38, in <module>     
        from ._tf2 import * ImportError: dynamic module does not define module export function (PyInit__tf2) 
Traceback (most recent call last):   
   File "/opt/ros/kinetic/lib/turtle_tf/turtle_tf_listener.py", line 37, in <module>     
        import tf   
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf/__init__.py", line 28, in <module>     
        from tf2_ros import TransformException as Exception, ConnectivityException, LookupException, ExtrapolationException  
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf2_ros/__init__.py", line 38, in <module>     
        from tf2_py import *   
   File "/opt/ros/kinetic/lib/python2.7/dist-packages/tf2_py/__init__.py", line 38, in <module>     
        from ._tf2 import * ImportError: dynamic module does not define module export function (PyInit__tf2) 

find by hard and thorough search

At the beginning, try the methods mentioned on the Internet, such as modifying the python version and soft link, and input on the terminal:

python --version

After getting python2.7, the running routine still reports an error, so I want to find the reason according to the error

Find the error report file from the first line of the error report:

/opt/ros/kinetic/lib/turtle_tf/turtle_tf_broadcaster.py

Turn on Turbo_ tf_ After editing the broadcast.py file, it is found that the first line of the file states:

#!/usr/bin/env python

Input in the terminal:

/usr/bin/env python

It shows the python version pointed by the python variable in the environment variable, which is different from the routine’s requirement of python2.7.

Problem solving: modify Python in environment variables

//Modify the priority of different python versions
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

//Manual priority selection
sudo update-alternatives --config python

*Remember to input “/ usr/bin/env Python” in the terminal to check whether the modification is successful.

After the modification is successful, run the routine again to see two little turtles. The little turtle at the bottom moves towards the little turtle at the center.

Python: Django exception when executing migrate operation: django.db.utils.OperationalError: (1051, “Unknown table’xxx’”)

Problem scenario:    When executing Python manage.py migrate, an error is reported (1051, “unknown table ‘xxx’),   At this time, there is no ‘xxx’ table in the database

Solution:
1. Comment out the tables in models.py that you failed to update

2. Execute after comments are dropped

python manage.py makemigrations

python manage.py migrate --fake

3. After the above execution is successful, cancel the table annotation in models.py

4. Cancel the comment and execute

python manage.py makemigrations

python manage.py migrate

Collection and solution of error reporting on various platforms of pyspider

Collection and solution of error reporting in pyspider

Command “python setup.py egg_ info” failed with error code 10

This means that pycurl installation reported an error

Download address of WHL file of pycurl in win10 solution

# Follow the link above to download the pycurl whl file, pay attention to your own python version, I'm using python 3.7 here
# locate the path to change the file
pip3 install pycurl-7.43.0.4-cp37-cp37m-win_amd64.whl

CentOS solution

yum install python-devel curl-devel
pip3 install pycurl

Ubuntu solution

sudo apt-get install libcurl4-gnutls-dev
pip install pyspider

SyntaxError: invalid syntax

Async cannot be used as a keyword and needs to be changed as a variable

Run pyspider to find the error file and replace async to know that the error was not reported

ImportError: cannot import name ‘Disp

[How to Solve] Kernel died with exit code 1.

  When jupyter is installed on the vscode and a sentence of Python code is calculated randomly in the ipynb file, the error of kernel died with exit code 1, C:: (users, hpccp, appdata, roaming, python, python38, site packages, traitlets) appears. I’ve been looking for solutions on the Internet all the time. I’ve spent a lot of energy trying many solutions, but I can’t, Because the error of kernel died with exit code 1 may be caused by many different details, and the root cause of the error is different. Of course, other people’s solutions may not be applicable to themselves.

So I began to pay attention to the details of my error. I wonder if there is something wrong with this folder, because my vscode and juptyer plug-ins have just been re installed (I have repeatedly uninstalled and re installed several times, but the first two have not been completely uninstalled), C: The folder was created before. I deleted the folder and its contents. Then the problem was solved.

[Solved] selenium.common.exceptions.JavascriptException: Message: javascript error: windows is not defined

Execution code:

bro.execute_script('windows.scrollTo(0,document.body.scrollHeight)')
sleep(2)

Implementation results:

selenium.common.exceptions.JavascriptException: Message: javascript error: windows is not defined
  (Session info: chrome=91.0.4472.124)

Attention
don’t add s to windows.
pay attention when auto filling!!

Correct writing!!

bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(2)

[Solved] Django Access to the backend error: Invalid block tag on line 98: ‘trans‘, expected ‘elif , ‘else‘或‘ endif. Did you forget

Error:
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 98: ‘translate’, expected ‘elif’, ‘else’ or ‘endif’. Did you forget to register or load this tag?

Solution:

Find the the catalog in the error message and then delete the red codes.

Save the file and solve it.

For the problem of rejecting old usage errors after numpy is updated, modified in numpy 1.20; for more details and guidance

There’s something wrong with numpy

Because the latest version of numpy is updated today, the previous usage is invalid

So You need to use the latest usage

Originally, my return value is like this

return np.array(df).astype(np.float)

Error:

DeprecationWarning: np.float is a deprecated alias for the builtin
float. To silence this warning, use float by itself. Doing this
will not modify any behavior and is safe. If you specifically wanted
the numpy scalar type, use np.float64 here.

Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
return np.array(df).astype(np.float)

It seems to be because of the version of the problem

If you look at the picture
Modify to the following can be.

return np.array(df, dtype=float)

perhaps

return np.array(df).astype(np.float64)

So you won’t make a mistake

[Solved] ValueError: only one element tensors can be converted to Python scalars

This error occurred in Python.

At first, the reason is that I want to change the list with a tensor to a tensor type, that is, [tensor (), tensor ()] to a tensor, and then I write like this,

a = torch.randn(1,2) # tensor([[-0.4962,  0.6034]])
d = [a, a, a] # [tensor([[-0.4962,  0.6034]]), tensor([[-0.4962,  0.6034]]), tensor([[-0.4962,  0.6034]])]
d = torch.tensor(d)

It’s a mistake. ValueError: only one element tensors can be converted to Python scalars

I see a solution on the Internet,

val= torch.tensor([item.cpu().detach().numpy() for item in val]).cuda() 

This method is very unsophisticated and concise.

Another way is to use torch. Cat, which is very concise. If you want to expand dimensions, you can use operations such as unsqueeze on this basis.

d = torch.cat(d, 0) 

'''
Output: tensor([[-0.4962,  0.6034],
        [-0.4962,  0.6034],
        [-0.4962,  0.6034]])
'''