Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:16:49+00:00 2026-06-15T05:16:49+00:00

tl;dr: How do I create a new namespace in some __init__.py , and copy

  • 0

tl;dr: How do I create a new namespace in some __init__.py, and copy select bits of another namespace into it, without requiring a sub-module in a new directory?

Full question:

Let’s say I have a pair of modules in a package, with the directory structure as follows:

foo/mod_1.py
foo/mod_2.py
foo/__init__.py

mod_1.py is as follows:

import numpy

__all__ = ['test1']

def test1():
    return numpy.zeros(10)

mod_2.py is as follows:

import numpy

def test2():
    return numpy.zeros(20)

and __init__.py as follows:

from mod_1 import *
import mod_2

del mod_1

This yields more or less the desired namespace:

In [7]: dir(foo)
Out[7]: 
['__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__path__',
 'mod_2',
 'test1']

Except that numpy is now in foo.mod_2 namespace. If I try to delete it in __init__.py, with del mod_2.numpy, it no longer exists for that module, so mod_2.test2 is broken.

I can achieve what I want by creating an extra directory mod_2 containing mod_2.py, but I want to keep the directory structure as-is if possible.

Hence my initial question.

Edit: I don’t mind doing a bit of name mangling to stop one name treading on another. Let’s say putting the relevant bits of mod_2.py into foo._mod_2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:16:51+00:00Added an answer on June 15, 2026 at 5:16 am

    You don’t want to do that, as you discovered you break your code.

    Python is a dynamic language, introspection gives us unfettered access to almost everything the interpreter can reach. This applies to your module too.

    If it makes you feel any better, the same thing applies to the standard library:

    >>> import unittest
    >>> unittest.sys
    <module 'sys' (built-in)>
    >>> unittest.types
    <module 'types' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc'>
    >>> dir(unittest)
    ['FunctionTestCase', 'TestCase', 'TestLoader', 'TestProgram', 'TestResult', 'TestSuite', 'TextTestRunner', '_CmpToKey', '_TextTestResult', '_WritelnDecorator', '__all__', '__author__', '__builtins__', '__doc__', '__email__', '__file__', '__metaclass__', '__name__', '__package__', '__unittest', '__version__', '_makeLoader', '_strclass', 'defaultTestLoader', 'findTestCases', 'getTestCaseNames', 'main', 'makeSuite', 'os', 'sys', 'time', 'traceback', 'types']
    

    Everything a module needs in it’s global namespace can be seen from the outside. This is normal.

    As you already discovered, use __all__ to control from modulename import * wildcard importing, and leave your module namespace alone otherwise.

    Of course, if you want to provide users of your package a clean API, there is nothing stopping you from importing the names that are part of the API into the top-level package __init__.py, or by using a api.py module.

    Importing just means adding the same name as a variable in the current module; mod_2.numpy is the exact same thing as sys.modules['numpy'], or numpy in your current module after you ran import numpy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason when I create a new namespace in Visual Studio 2008 its
Every new web service you create using visual studio comes with a predefined namespace
I need to create a new user in Active Directory. I have found several
I create new desktop with CreateDesktop and want to get it's DC & RC.
I create new blank solution. Add exist web site. Structure: Solution E:...\projectname Folders: bin
Code to create new form instance of a closed form using form name I
I want to create new WCF service and client. The 2 parties will communicate
How to create new type in c#? For example byte has range 0 to
I want to create new creative and check its audit status in facebook ads
Im trying to create new file on D: drive with c/c++ I found this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.