Using python modules without screwing up the name space
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using python modules without screwing up the name space
Hi,
I'm kinda new to Python and have been working with single file programs so far. Now I have a lot of functions in the main file, that I would love to take out as modules, so I don't have to dig through the file everytime.
But I noticed that when I put them into the external file, and import them, they don't work anymore, as it seems that modules don't treat the name space the same way as it is in the main file. So what I would need is that the functions that I import from the file behave exactly the same and get included into the main file, as though they would be pasted in there.
So that all functions and variables are available both to the modules and the main code file.
Is there a way to make modules behave this way? What is the advantage that they don't share the whole same name space? After all, after importing they should be integral part of the main code and be unified with them. Do I need something more like an include in Python?
I tried to modularize it, but then realized that I had to change a lot of code because of it and the idea would be to not have to change any code.
Hmm, thanks for the help. Modularize probably means changing a lot of code.
What I wanted to do was not do that extra work. The Idea for me was this:
an import includes the code in a module into your main file. All Functions, variables, will be treated exactly the same as though they would be physically written in the main file.
I have found, that this is unfortunately not true: functions in the external module file cannot access all variables that are in the main one and the other way around.
So importing does not work at all for me, I would have to change the whole code, and I don't want to do that, the idea of importing and creating a module for me would be to clean up the script and splitting off the functions into another file, not having to do a lot of extra work to make it happen.
There is another solution though:
import os
def include(filename):
if os.path.exists(filename):
execfile(filename)
include('myfile.py')
With this you should be able to do it, since it runs the file (i.e. declares the whole variables and functions, which import strangely does not do.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.