AttributeError: module 'shutil' has no attribute 'copy' - python-3.5

I have run to a wired error in python 3.5. I appreciate your help.
I want to copy some images from one directory to another directory using shutil. As I know, shutil is a standard library (doesn't need to be installed). But it doesn't work properly. How can I reinstall it or fix the error?
import shutil
import os
import random
## This program copy some images of a folder randomly into an existed directory
src = "/FirstPath/BIODI_V3_175x100/Train_set/"
dist = "/SecondPath/Random_Images_of_BIODI_for_test/"
copied = 0
mlist = os.listdir(src)
#os.mkdir(dist)
for i in range(1000):
rand = random.randint(1,len(mlist))
shutil.copy((os.path.join(src,mlist[rand])),dist)
i += 1
And it is the error:
Traceback (most recent call last):
File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 1, in <module>
import shutil
File "/usr/lib/python3.5/shutil.py", line 13, in <module>
import tarfile
File "/usr/lib/python3.5/tarfile.py", line 49, in <module>
import copy
File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 15, in <module>
shutil.copy((os.path.join(src,mlist[rand])),dist)
AttributeError: module 'shutil' has no attribute 'copy'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, re, pwd, grp, os, time
File "/usr/lib/python3.5/tempfile.py", line 44, in <module>
import shutil as _shutil
File "/usr/lib/python3.5/shutil.py", line 13, in <module>
import tarfile
File "/usr/lib/python3.5/tarfile.py", line 49, in <module>
import copy
File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 15, in <module>
shutil.copy((os.path.join(src,mlist[rand])),dist)
AttributeError: module 'shutil' has no attribute 'copy'
Original exception was:
Traceback (most recent call last):
File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 1, in <module>
import shutil
File "/usr/lib/python3.5/shutil.py", line 13, in <module>
import tarfile
File "/usr/lib/python3.5/tarfile.py", line 49, in <module>
import copy
File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 15, in <module>
shutil.copy((os.path.join(src,mlist[rand])),dist)
AttributeError: module 'shutil' has no attribute 'copy'

Related

ImportError: cannot import name '_compat' from 'cirq' (unknown location)

I am trying to import the module tensorflow_quantum as follows
>>> import tensorflow_quantum as tfq
But, I get the following error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/conda/lib/python3.8/site-packages/tensorflow_quantum/__init__.py", line 18, in <module>
from tensorflow_quantum.core import (append_circuit, get_expectation_op,
File "/opt/conda/lib/python3.8/site-packages/tensorflow_quantum/core/__init__.py", line 17, in <module>
from tensorflow_quantum.core.ops import (get_expectation_op,
File "/opt/conda/lib/python3.8/site-packages/tensorflow_quantum/core/ops/__init__.py", line 18, in <module>
from tensorflow_quantum.core.ops.circuit_execution_ops import (
File "/opt/conda/lib/python3.8/site-packages/tensorflow_quantum/core/ops/circuit_execution_ops.py", line 20, in <module>
from tensorflow_quantum.core.ops import (cirq_ops, tfq_simulate_ops,
File "/opt/conda/lib/python3.8/site-packages/tensorflow_quantum/core/ops/cirq_ops.py", line 22, in <module>
import cirq_google
File "/opt/conda/lib/python3.8/site-packages/cirq_google/__init__.py", line 18, in <module>
from cirq import _compat
ImportError: cannot import name '_compat' from 'cirq' (unknown location)
I have already installed cirq. It works as follows
>>> import cirq
>>>
Any help please?

ImportError when I change the directory in cmd

I am trying to import sklearn using cmd, I am changing the directory to a folder where I saved previous python files, however, I can't import sklearn in that particular folder and this is what I get:
>>> import sklearn
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\__init__.py", line 64, in <module>
from .base import clone
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py", line 14, in <module>
from .utils.fixes import signature
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\__init__.py", line 14, in <module>
from . import _joblib
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\_joblib.py", line 22, in <module>
from ..externals import joblib
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\__init__.py", line 119, in <module>
from .parallel import Parallel
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\parallel.py", line 28, in <module>
from ._parallel_backends import (FallbackToBackend, MultiprocessingBackend,
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 22, in <module>
from .executor import get_memmapping_executor
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\executor.py", line 14, in <module>
from .externals.loky.reusable_executor import get_reusable_executor
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\externals\loky\__init__.py", line 6, in <module>
from ._base import Executor, Future
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\externals\loky\_base.py", line 22, in <module>
from concurrent.futures import wait, as_completed
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\concurrent\futures\__init__.py", line 17, in <module>
from concurrent.futures.process import ProcessPoolExecutor
File "C:\Users\VER\AppData\Local\Programs\Python\Python36\lib\concurrent\futures\process.py", line 52, in <module>
from queue import Full
ImportError: cannot import name 'Full'
>>>
A file or folder is occupying a python file's namespace, for example, 'time.py'.
In this case, it is 'queue.py'
You need to remove it or you will not be able to import the module.
This is because python defaults to importing from the folder containing the program, and you must have a file overwriting 'queue.py' there.
I myself have experienced the error when attempting to import matplotlib into a file named 'copy', as a deep, deep dependency of matplotlib was named 'copy.py'

Can't import urllib.request module in python 3

I was executing a code from the book Think Python in Python 3. The code follows.
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import urllib.request
conn = urllib.request.urlopen('http://thinkpython.com/secret.html')
for line in conn:
print(line.strip())
When I execute this code in my machine's terminal, I got this following output.
Traceback (most recent call last):
File "secret.py", line 4, in <module>
import urllib.request
File "/usr/lib/python3.6/urllib/request.py", line 84, in <module>
import base64
File "/usr/lib/python3.6/base64.py", line 10, in <module>
import struct
File "/usr/lib/python3.6/struct.py", line 13, in <module>
from _struct import *
ModuleNotFoundError: No module named '_struct'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, re, pwd, grp, os, time
File "/usr/lib/python3.6/subprocess.py", line 136, in <module>
import _posixsubprocess
ModuleNotFoundError: No module named '_posixsubprocess'
Original exception was:
Traceback (most recent call last):
File "secret.py", line 4, in <module>
import urllib.request
File "/usr/lib/python3.6/urllib/request.py", line 84, in <module>
import base64
File "/usr/lib/python3.6/base64.py", line 10, in <module>
import struct
File "/usr/lib/python3.6/struct.py", line 13, in <module>
from _struct import *
ModuleNotFoundError: No module named '_struct'
I did this same code in Anaconda, and I got the correct output. Can anybody help me with this problem.

Error installing package of python using pip3

Every time I try to install any package using pip3 this error comes.
And when I open python3 in terminal and try to import string module the same error comes. I am not able to figure out any solution for it. My OS version is Ubuntu 16.04.
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 5, in <module>
import logging
File "/usr/lib/python3.5/logging/__init__.py", line 28, in <module>
from string import Template
ImportError: No module named 'string'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 21, in <module>
from urllib.request import urlopen
File "/usr/lib/python3.5/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.5/http/client.py", line 71, in <module>
import email.parser
File "/usr/lib/python3.5/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.5/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.5/email/message.py", line 16, in <module>
from email import utils
File "/usr/lib/python3.5/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.5/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.5/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
ImportError: No module named 'string'
Original exception was:
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 5, in <module>
import logging
File "/usr/lib/python3.5/logging/__init__.py", line 28, in <module>
from string import Template
ImportError: No module named 'string'
The problem solved since my python 3.5 directory was missing string.py file.

After conda update --all, Qt breaks in iPython

I ran "conda update --all" and it updated a lot of things, including Python itself (from 3.5.2-0 to 3.5.3-1, I think) and PyQt. I have used Pip in the past; I imagine they might conflict. Now when I try to run an iPython session, as soon as I evaluate a plot, the kernel dies, and I get a lot of "ImportError: No module named 'PyQt4.QtGui'" errors.
I opened a Python shell and found that QtGui exists in PyQt5 but not PyQt4:
>>> import PyQt4
>>> import PyQt4.QtGui
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'PyQt4.QtGui'
>>> import PyQt5
>>> import PyQt5.QtGui
I run Linux, specifically Ubuntu 16.04.
What should I do?
Here's the full traceback:
Traceback (most recent call last):
File "/home/jeff/apps/pythoon-libs/lib/python3.5/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/home/jeff/apps/pythoon-libs/lib/python3.5/runpy.py", line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/home/jeff/apps/pythoon-libs/lib/python3.5/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/ipykernel/__init__.py", line 2, in <module>
from .connect import *
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/ipykernel/connect.py", line 13, in <module>
from IPython.core.profiledir import ProfileDir
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/IPython/__init__.py", line 54, in <module>
from .core.application import Application
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/IPython/core/application.py", line 25, in <module>
from IPython.core import release, crashhandler
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/IPython/core/crashhandler.py", line 27, in <module>
from IPython.core import ultratb
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/IPython/core/ultratb.py", line 116, in <module>
from IPython.core import debugger
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/IPython/core/debugger.py", line 46, in <module>
from pdb import Pdb as OldPdb
File "/home/jeff/apps/pythoon-libs/lib/python3.5/pdb.py", line 75, in <module>
import code
File "/home/jeff/stats_with_luis/code.py", line 1, in <module>
import pandas
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/pandas/__init__.py", line 58, in <module>
from pandas.io.api import *
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/pandas/io/api.py", line 8, in <module>
from pandas.io.clipboard.clipboard import read_clipboard
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/pandas/io/clipboard/__init__.py", line 103, in <module>
copy, paste = determine_clipboard()
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/pandas/io/clipboard/__init__.py", line 76, in determine_clipboard
return init_qt_clipboard()
File "/home/jeff/apps/pythoon-libs/lib/python3.5/site-packages/pandas/io/clipboard/clipboards.py", line 49, in init_qt_clipboard
from PyQt4.QtGui import QApplication
ImportError: No module named 'PyQt4.QtGui'

Resources