Python Forum
to import just part of a modulr
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
to import just part of a modulr
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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
Reply
#3
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.
Reply
#4
is there a way to do it by line numbers?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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.
Reply
#6
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
You're a lost cause!
Reply
#9
i already know that.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can you please explain what the part after import sys is doing? Pedroski55 3 2,047 Dec-09-2020, 07:02 AM
Last Post: bowlofred
  Import error when trying to import DDE (part of PyWin32) fbicalho 0 3,779 Apr-21-2018, 07:26 PM
Last Post: fbicalho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020