LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2015, 08:07 PM   #1
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu, Debian Buster Stable, Windoze 7
Posts: 684

Rep: Reputation: 56
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.
 
Old 02-26-2015, 09:11 PM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
You can use the form:
Code:
from my_module import *
 
Old 02-27-2015, 09:36 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
I tried to modularize it, but then realized that I had to change a lot of code
If it works as expected after you "modularize" it, then that means that you know how to fix it, and you should continue doing so.

If not, then show me a code sample and I'll tell you how to fix it.

Last edited by dugan; 02-27-2015 at 09:38 AM.
 
Old 02-27-2015, 08:27 PM   #4
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu, Debian Buster Stable, Windoze 7
Posts: 684

Original Poster
Rep: Reputation: 56
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python -- Importing Modules Eahil Programming 3 06-17-2013 02:30 PM
[SOLVED] Python Modules no available macsmith Red Hat 3 08-18-2011 07:37 PM
listing python modules southsibling Programming 2 10-09-2005 12:53 AM
Python modules in Superkaramba wakeupbomb Linux - Software 12 08-03-2003 05:55 PM
Setup free space on /dev/hdc without screwing up /swap DavidPhillips Linux - General 4 06-24-2001 10:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration