fj123x
fj123x

Reputation: 7522

python load module from built-in

I'm using python 3, and i have a module named "http" (mypackage.http), and i have another module called foo, i want to load the built-in http module (not my mypackage.http module)

I can use

imp.find_module('http', sys.path[1:])

for get the built-in __ init__.py importlib path

Example:

/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/__ init__.py

But the use of imp.find_module()/load_module() are deprecated.

how can i import this built-in http module by another way like importlib?

project example:

Thanks!

Upvotes: 1

Views: 143

Answers (1)

user2357112
user2357112

Reputation: 281948

Just use

import http

In Python 2, this wouldn't have worked if foo was in mypackage, but relative imports need to be explicit in Python 3.

If you're running the module as a script, you'll need to fix the path somehow. If mypackage is findable using the normal import mechanisms, then you can run the module with the -m switch:

python -m mypackage.foo

Otherwise, you may need to check the path and alter it manually, as well as setting __package__ so relative imports work right.

Upvotes: 1

Related Questions