Python Forum
from copy import deepcopy fails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from copy import deepcopy fails
#1
i am getting this import failure even in this minimal code:

Output:
lt1/pdh /home/pdh 7> cat tryx.py from copy import deepcopy lt1/pdh /home/pdh 8> python tryx.py Traceback (most recent call last):   File "tryx.py", line 1, in <module>     from copy import deepcopy ImportError: cannot import name deepcopy lt1/pdh /home/pdh 9> python3 tryx.py Traceback (most recent call last):   File "tryx.py", line 1, in <module>     from copy import deepcopy ImportError: cannot import name 'deepcopy' lt1/pdh /home/pdh 10>
any idea why?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
do you have another file named copy.py in your path?
Recommended Tutorials:
Reply
#3
(Oct-29-2016, 02:45 AM)metulburr Wrote: do you have another file named copy.py in your path?

Doh yes, i do.

maybe the message should be clearer about not finding 'deepcopy' in module 'copy'.  the way it is now phrased suggested (to me) there might be some rule about 'deepcopy' that it cannot be imported alone.  i guess if i had run across this message (a lot) before, i would have quickly figured it out. my bad (for thinking in a small box about this).

mv copy.py ...
rm copy.pyc
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
There is a rule about it. In your file "copy.py", you never defined a "deepcopy", so when "deepcopy" is attempted to be imported, it fails. Importing "copy" worked fine, it was the "deepcopy" that failed.
Reply
#5
(Oct-29-2016, 03:12 AM)Skaperen Wrote: maybe the message should be clearer about not finding 'deepcopy' in module 'copy'.
it does say this
ImportError: cannot import name deepcopy
We all just get into the habit of not using module names to name our scripts. The only reason that comes to my mind first is because i have done it so many times in the past
http://python-forum.io/Thread-Python-Gotchas
Recommended Tutorials:
Reply
#6
the import is copy
deepcopy is copy.deepcopy
Reply
#7
(Nov-07-2016, 08:11 PM)nilamo Wrote: There is a rule about it. In your file "copy.py", you never defined a "deepcopy", so when "deepcopy" is attempted to be imported, it fails. Importing "copy" worked fine, it was the "deepcopy" that failed.
i still cannot figure out
Reply
#8
if i were to do "from copy import foo" and still had that copy.py file which has no name "foo" in it, the import will fail and i would get the message "ImportError: cannot import name foo" because "foo" is not there in my "copy" module. python looks in my home directory first when it looks for files to import. it's a feature: that let's me substitute my own modules. then if i do that, all the same features like importing individual functions and data variables still work ... in my module ... including the error messages.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 299 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020