Python Forum
to import just part of a modulr - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: to import just part of a modulr (/thread-13326.html)

Pages: 1 2


to import just part of a modulr - Skaperen - Oct-10-2018

i want to import just part of a module, is there a way to do that? i know the first and last line numbers i want to import.


RE: to import just part of a modulr - snippsat - Oct-10-2018

Can use from my_module import d
my_module.py:
a = 100
b = 200
c = 300
d = 400
bar.py:
from my_module import d

print(d)
Output:
400
So now have access to only d in bar.py.
A module is always fully imported,so doing import my_module and from my_module import d make no difference in bottom.
So sys.modules['my_module'] has reference to whole file.
>>> import sys
>>> sys.modules['my_module']
<module 'my_module' from 'E:\\div_code\\new\\my_module.py'> 
from my_module import d point straight to d.
But whether you use anything else from the module or not,it's always fully imported.

import my_module binds the name my_module to sys.modules['my_module'].
Then can access all other names from it.
>>> import my_module

>>> my_module.a
100
>>> my_module.b
200
>>> my_module.d
400



RE: to import just part of a modulr - nilamo - Oct-10-2018

If you want to do this, then you probably have logical breaks in the module, and could redesign it as sub-modules, so you only import the small piece you want.


RE: to import just part of a modulr - Skaperen - Oct-11-2018

is there a way to do it by line numbers?


RE: to import just part of a modulr - Larz60+ - Oct-11-2018

line numbers would be a bad way because they're a moving target.
Could be a disaster. I hope there's no built in method for that.
If for some reason you want to do something stupid, I guess you could write the code easily enough.


RE: to import just part of a modulr - ichabod801 - Oct-11-2018

(Oct-11-2018, 12:52 AM)Skaperen Wrote: is there a way to do it by line numbers?

This sort of thinking is your problem. You are constantly saying "This is how I want to write my code. Can I do that in Python?"

This is the wrong way to think about programming in any language.

The way to think is "This is what I want to do. How can I do it in Python?" Note that there is no code involved in the first sentence.


RE: to import just part of a modulr - Skaperen - Oct-13-2018

yes, line numbers could be a moving target. so now i want to import everything between the lines "#foobarstart" and "#foobarend".

this module is a bunch of variable settings. it has 2 groups of settings. originally i wanted to set a global variable and check it with an if/else that sets one group or the other. but the module could not see what the importer set.


RE: to import just part of a modulr - Larz60+ - Oct-13-2018

You're a lost cause!


RE: to import just part of a modulr - Skaperen - Oct-13-2018

i already know that.


RE: to import just part of a modulr - nilamo - Oct-14-2018

(Oct-13-2018, 05:21 AM)Skaperen Wrote: so now i want to import everything between the lines "#foobarstart" and "#foobarend".

How about a different file for the things you sometimes want to import?
# foobar.py
shared config here
# conf.py
# foobarstart
from foobar import *
# foobarend
#more config
# unrelated.py

import foobar as shared_config
Or, you can look at the goto april fool's module: http://entrian.com/goto/
It works by installing a handler in python's stack trace, looking for certain errors, and "fixing" the script when those errors are thrown.