How do I import an external module when a local module masks a standard library module imported by the external module?



I have a local module named tokenize.py, masks a standard library module of the same name. I only discovered this when I tried to import an external module (sklearn.linear_model), which in turn does import tokenize and expects to get the standard library module, but gets my local module instead.


This is related to How to access a standard-library module in Python when there is a local module with the same name?, but the setting is different, because applying the above solution would require modifying the external module.


An option would be to rename the local tokenize.py, but I would prefer not to do so as "tok


Related to : How do I import an external module when a local module masks a standard library module imported by the external module?
ImportError: cannot import module - Only one module can not be imported from package
Programming Languages

I created a package, for the ease of use I call it packageA.


In this package I have 4 submodules in (separate files) and an init file, so the package contains the following files:


__init__.py
moduleA.py
moduleB.py
moduleC.py
moduleD.py

Module B-D stand alone, they don't import any other local module. Module A imports module B-D.


Now I have a script that wants to import packageA, the init.py is empty so far.


import packageA

works without problems. But moduleA is not available from this import. If I wan

In python, how do you import all classes from another module without keeping the imported module's namespace?
Programming Languages

How do you import classes and methods from another module without retaining the former module namespace?


I am current refactoring some legacy code and am frequently doing imports similar to these.


from legacy_module import ClassA as ClassA
from legacy_module import ClassB as ClassB
from legacy_module import ClassC as ClassC
from legacy_module import methodA as methodA
from legacy_module import methodB as methodB

This is done so that the classes can be referenced as ClassA rather than legacy_module.ClassA.


In python, how do you import all of the classes and methods above

How to access a standard-library module in Python when there is a local module with the same name?
Programming Languages

How can a standard-library module (say math) be accessed when a file prog.py is placed in the same directory as a local module with the same name (math.py)?


I'm asking this question because I would like to create a package uncertainties that one can use as


import uncertainties
from uncertainties.math import *

Thus, there is a local math module inside the uncertainties directory. The problem is that I want to access the standard library math module from uncertainties/__init__.py.


I prefer not to rename uncertainties.math because this module is precisely intended to replace funct

How to import standard library instead of same-named module in module path
Programming Languages

I have the following directory structure


main_code.py
libs/
__init__.py
mylib.py
time.py

with main_code.py just importing mylib:


from libs import mylib

and mylib.py just importing time:


import time
print time

Now it turns out that mylib.py imports libs/time.py and not the built-in standard library time. Is there any way to get the 'normal' behavior, i.e. that mylib.py imports the built-in

Constructing/destructing an object in external c++ module on module load/unload
Programming Languages

I wrote a python module in c++. All works fine but now I want to add logging. My logger class (3rd party) requires initializtion similar to:


int main()
{
Logger logger("log.log");
Logger::Register(&logger);
DoSomethingAndLog();
return 0;
}

As you can see logger is created when the program is initialized and destructed when program is terminated.


Where would I create the Logger object so that it would be constructed upon module loading and destructed when the module is unloaded?


How do I import an external module when a local module masks a standard library module imported by the external module?
Programming Languages

I have a local module named tokenize.py, masks a standard library module of the same name. I only discovered this when I tried to import an external module (sklearn.linear_model), which in turn does import tokenize and expects to get the standard library module, but gets my local module instead.


This is related to How to access a standard-library module in Python when there is a local module with the same name?, but the setting is different, because applying the above solution would require modifying the external module.


An option would be to rename the local tokenize.py, but I would prefer not to do so as "tok


Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .