1

I'm trying to develop a simulation class that replaces serial at specific apps(Win7, python 2.7).

I have a SerialHandle class that works in number of apps, It's job is add logic to the well known serial methods, the idea was to replace python serial.py with a dummy file with the same name so we won't have to change and imports at SerialHandle.

Now i have this file with Serial class just like the origin and it works fine:

serial.py
...Serial()

Since i want to really simulate the methods i need the SerialException from serialutil so inside my serial.py i'm trying to import it using:

from serial import SerialException

But as expected i'll get this raise since from serial goes to the local file at first:

Traceback (most recent call last):
  File "C:/CROW/ATE/DUTDrivers/DD_SimulatorExample/DD_SimulatorExample.py", line 18, in <module>
    from Utilities.Serial.SerialHandle.trunk.SerialHandle import SerialHandle
  File "C:\CROW\ATE\Utilities\Serial\SerialHandle\trunk\__init__.py", line 4, in <module>
    from Utilities.Simulator import serial
  File "C:\CROW\ATE\Utilities\Simulator\serial.py", line 11, in <module>
    from serial import SerialException
ImportError: cannot import name SerialException

I understand the problem is the file name since at any other file it will work...

I've tried sys.append(site-packages....serial.py) no luck.

Questions:

  1. Any way to tell the interpreter to ignore the local file at a specific from..import?

  2. Is there any other way to import from an absolute path?

Notes:

  1. the file naming as serial.py is not a decision it's a definition so changing the name is not relevant...

  2. Overloading python serial is not an option also...

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Kobi K
  • 7,743
  • 6
  • 42
  • 86
  • What about copying SerialException from python source code and paste it into your module? – furins Jan 08 '14 at 15:07
  • @furins it will work for sure, the reason i asked this question is because i was wondering if there is some elegant solution... – Kobi K Jan 08 '14 at 15:09
  • 1
    then this approach should be what you are searching for: http://stackoverflow.com/questions/6031584/python-importing-from-builtin-library-when-module-with-same-name-exists?rq=1 (the approved answer) – furins Jan 08 '14 at 15:15

1 Answers1

3

You must be using python 2.x, since absolute imports are the default in python 3.x. You can use absolute imports in your serial.py file by adding this at the top of the file:

from __future__ import absolute_import

Note that you will need to convert any implicit relative imports from your serial.py file into explicit relative imports. So if you were importing some_func from other_file.py, which is in the same directory, you would need to change that to:

from .other_file import some_func

Note that the "." indicates a relative import from the same package as the current file. See here for additional detail.

bogatron
  • 18,639
  • 6
  • 53
  • 47
  • no, he needs the opposite. He need always to load implicitly from local sources except from inside his module. – furins Jan 08 '14 at 15:16
  • That's what my answer explains. Importing `absolute_import` only affects the file in which it is used. Others would still use relative imports. – bogatron Jan 08 '14 at 15:18
  • yes, but in the module he does not want to access the local file, he wants the python one. – furins Jan 08 '14 at 15:19
  • And that is what my answer does. After importing `absolute_import`, when he then does `import serial`, it will import the top-level `serial` package (from the python distro). – bogatron Jan 08 '14 at 15:21
  • actually both of you are right :) @furnis the link was very helpful! and with both the link and answer it solved the issue!, thanks. – Kobi K Jan 08 '14 at 15:21