Korean, Edit

Python Major Troubleshooting [41-60]

Recommended Article : 【Python】 Python Table of Contents



41. ImportError: cannot import name ‘mnist.data_utils’

Problem Situation:

from mnist.data_utils import load_data

...

MNIST_data = h5py.File("mnist/MNISTdata.hdf5", 'r')

Solution 1: Download MNIST data and execute the code as follows:

MNIST_data = h5py.File("./MNISTdata.hdf5", 'r')

X_train = np.float32(MNIST_data['x_train'][:])
y_train = np.int32(np.array(MNIST_data['y_train'][:, 0])).reshape(-1, 1)
X_test = np.float32(MNIST_data['x_test'][:])
y_test = np.int32(np.array(MNIST_data['y_test'][:, 0])).reshape(-1, 1)

MNIST_data.close()

Solution 2: Use tensorflow.keras.datasets.mnist.load_data().

import tensorflow as tf

(Xtr, Ytr), (Xte, Yte) = tf.keras.datasets.mnist.load_data()



42. TypeError: can’t multiply sequence by non-int of type ‘float’

⑴ (grammar) Cause 1. Attempting to multiply a float value in a situation where only integers can be multiplied.


### case 1. (Good)
names = ("John ", "Jane ")
print(names * 2)
# ('John ', 'Jane ', 'John ', 'Jane ')

### case 2. (Bad)
names = ("John ", "Jane ")
print(names * 2.0)
# TypeError: can't multiply sequence by non-int of type 'float'

### case 3. (Good)
names = ("John ", "Jane ")
print(names * int(2.0))
# ('John ', 'Jane ', 'John ', 'Jane ')


⑵ (grammar) Cause 2. As Space Ranger gets updated, it generates tissue_positions.csv instead of tissue_positions_list.csv, and this new file includes an improper header (see below). Errors occur when viewing an AnnData object, which has been loaded using scanpy.read_visium without updating to reflect this change, through scanpy.pl.spatial.


# tissue_positions_list.csv (Previous)
ACGCCTGACACGCGCT-1,1,0,0,256,1780
TACCGATCCAACACTT-1,1,1,1,268,1760
...

# tissue_positions.csv (Newest)
barcode,in_tissue,array_row,array_col,pxl_row_in_fullres,pxl_col_in_fullres
ACGCCTGACACGCGCT-1,1,0,0,256,1780
TACCGATCCAACACTT-1,1,1,1,268,1760


① (grammar) Solution: Rename tissue_positions.csv to tissue_positions_list.csv and remove the header, just like in the previous version.



43. UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xdf in position 991: invalid continuation byte

Problem Situation: Issue arises from using data = pandas.read_csv('example.csv', index_col=0).

① It is suspected that characters like α, β within the .csv file are causing the problem.

Solution: Use data = pandas.read_csv('example.csv', index_col=0, encoding='ISO-8859-1').



44. Server Connection Error: A connection to the Jupyter server could not be established. JupyterLab will continue trying to reconnect. Check your network connection or Jupyter server configuration.

Problem Situation: The same issue occurs in both Jupyter Lab and Jupyter Notebook.

Solution 1: It might be a broken pipe issue, in which case follow the solution provided here.

① Broken pipe issues may be related to network configuration due to national security policies.

② Measures like using VPN can prevent broken pipe issues for users.

Solution 2: If execution takes a long time, there could be intentional disconnection by the server due to unresponsiveness. In such cases, periodic pressing of Enter in the terminal or Anaconda prompt where jupyter notebook / jupyter lab was launched can resolve unresponsiveness and connection errors.



45. No module named ‘google.protobuf’

Problem Situation:

Solution: Install with conda install protobuf.



46. error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cvtColor’

Problem Situation: This error occurs when trying to read an image file using OpenCV, but the image file doesn’t exist.



47. RuntimeError: CUDA error: out of memory

Problem Situation: Occurs when GPU usage exceeds its limit.

Solution: Modify the command to use a specific GPU, e.g., change from python app.py to a command that specifies GPU usage like CUDA_VISIBLE_DEVICES=1 python app.py.

CUDA_VISIBLE_DEVICES=1 python app.py



48. RuntimeError: CUDA error: no kernel image is available for execution on the device

Problem Situation: Occurs when PyTorch or torchvision version doesn’t match system conditions, such as CUDA version.

① For example, CUDA11 or more recent versions should be used for RTX3090.

② Related error log : The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_70. If you want to use the NVIDIA GeForce RTX 3090 GPU with PyTorch, please check the instruction.

Solution: Install the correct PyTorch and torchvision versions that match the system, as described here.

pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 -f https://download.pytorch.org/whl/torch_stable.html



49. ImportError: cannot import name ‘ctx’ from ‘dash’

Problem Situation:

Solution: Upgrade the dash package to version 2.9.3 (or later, as of the author’s reference).



50. AttributeError: ‘Flask’ object has no attribute ‘before_first_request’

Problem Situation:

Solution: Upgrade the dash package to version 2.9.3 (or later, as of the author’s reference).



51. ImportError: cannot import name ‘cached_property’ from ‘werkzeug’

Problem Situation:

pip uninstall Werkzeug
pip install Werkzeug==0.16.0

Solution: Downgrade Werkzeug to version 1.0 or earlier.



52. TypeError: expected str, bytes or os.PathLike object, not numpy.ndarray

Problem Situation: Issue occurs when using gseapy.enrichr with an array input.

Cause: You need to provide a list instead of a numpy.ndarray as input.

Solution: Use numpy.ndarray.tolist(ar) or ar.tolist() as input.



53. OSError: wkhtmltopdf exited with non-zero code 1. error: qt.qpa.screen: QXcbConnection: Could not connect to display Could not connect to any X display.

Problem Situation:

pdfkit.from_string(output_text, 'pdf_generated.pdf', configuration=config)

Solution: Refer to the discussion in this Stack Overflow thread.

# newly added
from pyvirtualdisplay import Display
display = Display()
display.start()

# previous
pdfkit.from_string(output_text, 'pdf_generated.pdf', configuration=config)

# newly added
display.stop()

Reference



54. FileNotFoundError: [Errno 2] No such file or directory: ‘Xvfb’

Solution 1: Install the Xvfb package (reference).

sudo apt-get install xvfb
pip install xvfbwrapper

Solution 2: Avoid using pyvirtualdisplay.

# before
...
from pyvirtualdisplay import Display
display = Display()
display.start()
pdfkit.from_string(output_text, 'my_pdf.pdf', configuration=config)
display.stop()

# after
...
pdfkit.from_string(output_text, 'my_pdf.pdf', configuration=config)



55. TypeError: import_optional_dependency() got an unexpected keyword argument ‘errors’

Solution: Upgrade pandas with pip install pandas --upgrade.

Reference



56. bash: java: command not found

Solution: Follow the steps outlined here.

sudo apt install default-jre



57. Error: LinkageError occurred while loading main class picard.cmdline.PicardCommandLine java.lang.UnsupportedClassVersionError: picard/cmdline/PicardCommandLine has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0

Problem Situation: Version mismatch between picard and Java Runtime.

Solution: Downgrade picard to a version compatible with the installed Java Runtime.



58. Assertion failure in +[NSUndoManager _endTopLevelGroupings], NSUndoManager.m:1117

Problem Situation: Potential thread-safety conflict when multiple Python threads attempt to access macOS GUI simultaneously.

Solution 1: Run in a Linux environment instead of a macOS environment.

Solution 2: Disable the interactive mode of the web server that may contribute to the problem.

import matplotlib
matplotlib.use('Agg')



59. ImportError: cannot import name ‘Int64Index’ from ‘pandas’

Solution: Reinstall pandas and xgboost (reference).

pip uninstall pandas
pip install pandas --upgrade
pip3 uninstall xgboost
pip3 install xgboost



60. E: Unable to locate package tree

Solution:

sudo apt-get install update
sudo apt-get install tree



Input : 2023-01-28 02:15

Modified : 2023-07-28 17:22

results matching ""

    No results matching ""