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

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

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

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

Coding

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

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?

C & C++ & C#

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

Programming Languages

- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow