Category Archives: Python

VScode debug error: configured debug type ‘Python’ is not supported

When using vscode to debug, an error is reported in the pop-up window:

Configured debug type 'python' is not supported

Refer to the reference website: https://github.com/microsoft/vscode/issues/136712

The measured solution is metaphysical, as follows:

Delete Jupiter and python in vscope extensions, and then reinstall them; Close all vscodes; (this step is very important.) Open vscode again and you can debug normally.

Pycharm error: original error was: DLL load failed while importing _multiarray _Umath: the specified module could not be found

Anaconda is installed under the window system and the virtual environment is configured
everything runs normally using the command line, but an error is reported when running on pychram:
original error was: DLL load failed while importing _multiarray _umath

Tried:
1. Uninstall numpy and reinstall numpy
2. Add the python path again many times in the python settings

Final solution:
add system variables

after installing anaconda, only the first two are added, but not enough!!! \Bin and \library\bin should also be added!!!

Since it runs normally on the command line, the version problem between packages can be ignored. The problem must be the configuration of pycharm or system variables

Reference link: https://github.com/conda/conda/issues/7833

[Solved] mmdetection benchmark.py Error: RuntimeError: Distributed package doesn‘t have NCCL built in

Cause:
use mmdetection’s tools/benchmark An error occurs when py calculates FPS
the error contents are as follows:

Traceback (most recent call last):
  File "tools/analysis_tools/benchmark.py", line 191, in <module>
    main()
  File "tools/analysis_tools/benchmark.py", line 183, in main
    init_dist(args.launcher, **cfg.dist_params)
  File "D:\Anaconda\envs\eagermot\lib\site-packages\mmcv\runner\dist_utils.py", line 18, in init_dist
    _init_dist_pytorch(backend, **kwargs)
  File "D:\Anaconda\envs\eagermot\lib\site-packages\mmcv\runner\dist_utils.py", line 32, in _init_dist_pytorch
    dist.init_process_group(backend=backend, **kwargs)
  File "D:\Anaconda\envs\eagermot\lib\site-packages\torch\distributed\distributed_c10d.py", line 510, in init_process_group
    timeout=timeout))
  File "D:\Anaconda\envs\eagermot\lib\site-packages\torch\distributed\distributed_c10d.py", line 597, in _new_process_group_helper
    raise RuntimeError("Distributed package doesn't have NCCL "
RuntimeError: Distributed package doesn't have NCCL built in

Cause analysis:
Windows does not support nccl backend

Solution:
1. Locate the following code location

File "D:\Anaconda\envs\eagermot\lib\site-packages\mmcv\runner\dist_utils.py", line 32, in _init_dist_pytorch

2. Add code before 1 (line 32)

backend='gloo'

[Solved] UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xbf in position 7: invalid start byte

The code is as follows:

f = csv.reader(open(csvroot, 'r',encoding='utf-8'))
for i in f:
    print(i)

Solution: view the file encoding format

import chardet

f = open(full_csvroot, 'rb')
data = f.read()
print(chardet.detect(data))

# Outcome:
# {'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}

Amend to read:

f = csv.reader(open(csvroot, 'r',encoding='GB2312'))
for i in f:
    print(i)

Perfect operation

[Solved] python Error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.

Python Error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Error Codes:

if (code in list(changed_code['Old material code'])):
            temp_index = changed_code.loc[changed_code['Old material code'] == code].index

The type of code here is float.

Cause analysis:

In this judgment method, the judged value cannot be of float type.

Solution:

Just convert float format to int format

if (int(code) in list(changed_code['Old material code'])):
            temp_index = changed_code.loc[changed_code['Old material code'] == code].index

Solve the problem, brothers, get better!!

[Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘

Problems arising

The JSON object must be STR, bytes or byte array, not ‘Java.lang.String’

Solution:

The last in sutime.py is this:

	return json.loads(self._sutime.annotate(input_str, reference_date))
return json.loads(self._sutime.annotate(input_str))

Replace it with:

	return json.loads(str(self._sutime.annotate(input_str, reference_date)))
return json.loads(str(self._sutime.annotate(input_str)))

 

Test sutime package (this part is also referred to the above website)

import json
import os
from sutime import SUTime

if __name__ == '__main__':
    test_case = "I need a desk for tomorrow from 2pm to 3pm"
    # D:/python_projects/SPARQA/common_resources/resources_sutime/python-sutime-master/jars
    jar_files = os.path.join(os.path.dirname('D:/python_projects/SPARQA/common_resources/resources_sutime/python-sutime-master/jars'),'jars')
    sutime = SUTime(jars=jar_files, include_range=True)
    print(json.dumps(sutime.parse(test_case), sort_keys=True, indent=4))

The operation result is:

[
    {
        "end": 26,
        "start": 18,
        "text": "tomorrow",
        "type": "DATE",
        "value": "2021-12-31"
    },
    {
        "end": 35,
        "start": 32,
        "text": "2pm",
        "type": "TIME",
        "value": "2021-12-30T14:00"
    },
    {
        "end": 42,
        "start": 39,
        "text": "3pm",
        "type": "TIME",
        "value": "2021-12-30T15:00"
    }
]

[Solved] CUDA unknown error – this may be due to an incorrectly set up environment

preface

Today, a project using pytorch on the viewing server suddenly made an error after upgrading. The whole content of the error report is limited by the title. I’ll send it below.

builtins. RuntimeError: CUDA unknown error – this may be due to an incorrectly set up environment, e.g. changing env variable CUDA_ VISIBLE_ DEVICES after program start. Setting the available devices to be zero.

Screenshot of error reporting

Later, I consulted some materials, and the following are some solutions.

Solution:

Method 1: add environment variables

Since I started the project as a docker container, I installed VIM after entering the container, and then in ~/Bashrc finally added something.

export CUDA_ VISIBLE_ DEVICES=0

Since the selected graphics card number is 0 when building the container, the number I configured above is 0.

Check $CUDA after restarting the container_ VISIBLE_ The devices output is normal, but the problem is not solved, and the error is still reported.

Method 2: add environment variables to the code

Add the following code at the beginning of the initialization CUDA area.

import os
os.environ['CUDA_VISIBLE_DEVICES'] =‘0’

It still hasn’t solved the problem.

Method 3: restart the server

Referring to some articles, I mentioned that if the system upgrades the graphics card driver without restarting, it will also lead to the same error.

So I restarted the server and solved the problem.

How to Solve Geopy library Error: Configurationerror Error

Error details

geopy.exc.ConfigurationError: 
Using Nominatim with default or sample `user_agent` "geopy/2.2.0" is strongly discouraged, as it violates Nominatim's ToS https://operations.osmfoundation.org/policies/nominatim/ and may possibly cause 403 and 429 HTTP errors. Please specify a custom `user_agent` with `Nominatim(user_agent="my-application")` or by overriding the default `user_agent`: `geopy.geocoders.options.default_user_agent = "my-application"`.

Solution:

This error is because the default value of UA is bad. Just specify user-agent as a unique string. For example
, when BuyiXiao initializes Nominatim, specify user-agent.

geolocator = Nominatim(user_agent='BuyiXiao')

How to Solve Error: RuntimeError CUDA out of memory

Error: RuntimeError: CUDA out of memory.
Error Messages:

Traceback (most recent call last):
  File "xxx.py", line 110, in <module>
    loss.backward()
  File "/nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/tensor.py", line 185, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/autograd/__init__.py", line 127, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: CUDA out of memory. Tried to allocate 132.00 MiB (GPU 0; 15.78 GiB total capacity; 13.69 GiB already allocated; 91.50 MiB free; 14.53 GiB reserved in total by PyTorch)
Exception raised from malloc at /pytorch/c10/cuda/CUDACachingAllocator.cpp:272 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::string) + 0x42 (0x14c9ce19a1e2 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libc10.so)
frame #1: <unknown function> + 0x1e64b (0x14c9ce3f064b in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libc10_cuda.so)
frame #2: <unknown function> + 0x1f464 (0x14c9ce3f1464 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libc10_cuda.so)
frame #3: <unknown function> + 0x1faa1 (0x14c9ce3f1aa1 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libc10_cuda.so)
frame #4: at::native::empty_cuda(c10::ArrayRef<long>, c10::TensorOptions const&, c10::optional<c10::MemoryFormat>) + 0x11e (0x14c9d10fc90e in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
frame #5: <unknown function> + 0xf33949 (0x14c9cf536949 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
frame #6: <unknown function> + 0xf4d777 (0x14c9cf550777 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
frame #7: <unknown function> + 0x10e9c7d (0x14ca0a2ecc7d in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #8: <unknown function> + 0x10e9f97 (0x14ca0a2ecf97 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #9: at::empty(c10::ArrayRef<long>, c10::TensorOptions const&, c10::optional<c10::MemoryFormat>) + 0xfa (0x14ca0a3f7a1a in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #10: at::native::mm_cuda(at::Tensor const&, at::Tensor const&) + 0x6c (0x14c9d05ebffc in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
frame #11: <unknown function> + 0xf22a20 (0x14c9cf525a20 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
frame #12: <unknown function> + 0xa56530 (0x14ca09c59530 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #13: at::Tensor c10::Dispatcher::call<at::Tensor, at::Tensor const&, at::Tensor const&>(c10::TypedOperatorHandle<at::Tensor (at::Tensor const&, at::Tensor const&)> const&, at::Tensor const&, at::Tensor const&) const + 0xbc (0x14ca0a44181c in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #14: at::mm(at::Tensor const&, at::Tensor const&) + 0x4b (0x14ca0a3926ab in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #15: <unknown function> + 0x2ed0a2f (0x14ca0c0d3a2f in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #16: <unknown function> + 0xa56530 (0x14ca09c59530 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #17: at::Tensor c10::Dispatcher::call<at::Tensor, at::Tensor const&, at::Tensor const&>(c10::TypedOperatorHandle<at::Tensor (at::Tensor const&, at::Tensor const&)> const&, at::Tensor const&, at::Tensor const&) const + 0xbc (0x14ca0a44181c in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #18: at::Tensor::mm(at::Tensor const&) const + 0x4b (0x14ca0a527cab in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #19: <unknown function> + 0x2d11c34 (0x14ca0bf14c34 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #20: torch::autograd::generated::MmBackward::apply(std::vector<at::Tensor, std::allocator<at::Tensor> >&&) + 0x294 (0x14ca0bf30814 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #21: <unknown function> + 0x3375bb7 (0x14ca0c578bb7 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #22: torch::autograd::Engine::evaluate_function(std::shared_ptr<torch::autograd::GraphTask>&, torch::autograd::Node*, torch::autograd::InputBuffer&, std::shared_ptr<torch::autograd::ReadyQueue> const&) + 0x1400 (0x14ca0c574400 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #23: torch::autograd::Engine::thread_main(std::shared_ptr<torch::autograd::GraphTask> const&) + 0x451 (0x14ca0c574fa1 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #24: torch::autograd::Engine::thread_init(int, std::shared_ptr<torch::autograd::ReadyQueue> const&, bool) + 0x89 (0x14ca0c56d119 in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
frame #25: torch::autograd::python::PythonEngine::thread_init(int, std::shared_ptr<torch::autograd::ReadyQueue> const&, bool) + 0x4a (0x14ca19d0ddea in /nfsshare/apps/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #26: <unknown function> + 0xbd6df (0x14ca5616b6df in /usr/lib/x86_64-linux-gnu/libstdc++.so.6)
frame #27: <unknown function> + 0x76db (0x14ca5a6356db in /lib/x86_64-linux-gnu/libpthread.so.0)
frame #28: clone + 0x3f (0x14ca5a35ea3f in /lib/x86_64-linux-gnu/libc.so.6)

Solution:
CUDA_LAUNCH_BLOCKING=1 python xx.py failed
Reduce the batch size to solve the problem.