User avatar
liudr
Posts: 685
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Trying to make python import work

Tue Sep 04, 2018 9:42 pm

I have some simple tasks to do but am stuck on the import mechanism. I know that python import is not to be compared with C include or global declarations but I'm looking for simple ways to have a shared name space. Here is what I have in two files:

main.py

Code: Select all

import math
a='haha'
from doit import *
doit_fn()
doit.py

Code: Select all

import math
def doit_fn():
    print(math.sqrt(16))
    print(a)
This doesn't work if doit.py didn't import math.OK that's too bad. I thought the main's import will be enough. But print(a) also doesn't work. I thought main.py was importing the doit.py after "a" was defined. How come this doesn't work? I got "name not defined" error on a.

I thought python runs an interpreter and should not be problem if the imported script has variable names that are not know to the code but defined by the importer.

Do I have to send everything from main.py to imported functions from doit.py via parameters of function calls? Isn't python duck typing or is duck typing not applying to this type of situation?

I also tried global a in doit.py to no avail. Can I safely say that imported python scripts have their individual name spaces where they don't see name space of the importer even if the importer does import * ? If I want to split a single long script into several shorter scripts and use import to load them all, then I can't have global variables among the imported scripts, correct? How do I do globals across scripts?
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
DougieLawson
Posts: 35797
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Trying to make python import work

Tue Sep 04, 2018 9:45 pm

Variable scope is local to a function.

Code: Select all

def doit_fn(a):
    print(math.sqrt(16))
    print(a)
    
doit_fn('99 bottles of beer')
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
liudr
Posts: 685
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Trying to make python import work

Tue Sep 04, 2018 9:58 pm

That's what I am dreading, passing everything from caller to function as parameters. It breaks the symmetry between having these functions defined in the same file and having them defined (unaltered) in separate files. In the same file, globals penetrate through function defs. Once you move the functions to a separate file, you have to rewrite the function. That's really annoying. I'd like to use global such as beer_cost=10 so every beer-related function know the cost to do its job.

In C/C++, you include a common header where beer_cost is defined.
I found this:

https://stackoverflow.com/questions/130 ... ween-files

It's a bit ugly.

Are there switches to turn on so the importer can import and incorporate imported script's namespace into the importer's name space? The default import behavior must be creating separate name spaces.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
DougieLawson
Posts: 35797
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Trying to make python import work

Tue Sep 04, 2018 10:27 pm

Python doesn't work like that. If you want C++ behaviour write your code in C++.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
liudr
Posts: 685
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Trying to make python import work

Wed Sep 05, 2018 4:22 am

From a general programming point of view, splitting a single long program into several shorter programs is a common task, not a C++ only feature. I will explore this further. I'm not convinced that python lacks this basic feature.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Trying to make python import work

Wed Sep 05, 2018 5:32 am

You just need to understand how the global/local variables works.

The whole story is a little complicated.

A variable declared (first used) in main loop can be read from a function internal to that main loop but if you try to write to that variable it will create a new local 1 with the same name unless you first declare it as a global variable.

You can also create a Class/object that can share a variable though out that whole object but isn't accessible to external of the object by using self.variable_name. If you need access and data within the object then you have to make a function that returns that data and if you want to use any data outside that object within the object then you need to pass the data to a function within the object.

Python is in fact very good at doing what you want : break up your program into smaller modules.
Python will in fact will break up the program into separate objects were as using #include in C just cut and pastes all the code into 1 big program unbroken.

It took me quite awhile to get used to Python namespace and class/objects and how the local/global work but once you get your head around it, it is super handy.

User avatar
liudr
Posts: 685
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Trying to make python import work

Wed Sep 05, 2018 2:33 pm

Thanks OutoftheBOTS. For now I'll keep all my functions in one file and look for elegant solutions/reduce or eliminate global variables at the same time. The source of my problem is with MicroPython and wanting to keep project code on sd card while wanting the project code to have access to all the hardware that is initialized in main.py.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Trying to make python import work

Wed Sep 05, 2018 9:35 pm

In python you can pass hardware instances to function or objects. i.e I can create and instance of I2C and pass that instance to functions or objects. This is really handy because I could have a class/object for driving an I2C sensor then another class/object for driving a different I2C device. I can create the instance of the I2C then pass it to both classes/objects. To me it feels like it works like a pointer but a pointer that can not only point at a memory address but can point at anything like hardware or a file or even another function or object.

User avatar
liudr
Posts: 685
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Trying to make python import work

Thu Sep 06, 2018 4:14 am

Yes, I like the duck type where you can just pass an object to a function to be used. It's more flexible than C++. Anyway, that doesn't help me with the separate namespace issues. I want variables and functions stored in other files to be incorporated into the main name space like they were defined in the main script. What I found out was that if I read the separate file into a string and run exec() on the string, it acts as if the content was part of the main script. It's not ideal since memory may run out for longer files. I need to learn garbage collector to properly dispose of the string. For now it seems to be the only way to bring variables and functions that are stored in different files into the same name space. As I read more about import, I might find better ways.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Trying to make python import work

Thu Sep 06, 2018 8:22 am

I think maybe you need to learn better set out of your programs.

Your main program should have the main variables within it then each class/object or function should have the variables that they need within it then any resources/data that needs to be shared can be passed/returned

The whole point of breaking up a program into separate modules and objects is to make it more organized, i.e to group together common tasks and data.

Having a variable that you need in your main program stored in a separate module is just creating a ball of spaghetti. It is a bit like you will store your Sat Nav in your car as that is where you tend to need it, it would be an unusual to store your Sat Nav in your garden shed then when your out driving in your car you call up your wife and say to her can you go to the shed and look at the sat nav and tell me how to get where I am going.

Return to “Python”