TeddyIP
Posts: 22
Joined: Fri Jul 20, 2018 9:47 am

Import "filename" to another project

Sun Jul 29, 2018 12:15 pm

Hello friends,
so far I have been fairly successful import <filename>. Sometimes I find that I have declared/created a variable with very similar name/s. How do I add a function/class that will recognize the name has already been used within a code?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Import "filename" to another project

Mon Jul 30, 2018 9:14 am

The variable conflict is why using "import <filename>" alone is a bad idea. Variables on the global namespace will just overwrite each other.

A better method is to do something like this
import <filename> as <someabbreviationoffilename>
e.g.
import tkinter as tk

Then variables/functions from that file will need to have the correct namespace before they can be used.
import tkinter as tk
root = tk.Tk()


It is difficult to make a more detailed recommendation without seeing your code, but it is generally a bad idea for you to have these variable conflicts in the first place. When you import a module, it (generally) shouldn't contain global variables, just functions or classes.
Last edited by scotty101 on Mon Jul 30, 2018 1:24 pm, edited 3 times in total.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Import "filename" to another project

Mon Jul 30, 2018 12:21 pm

scotty101 wrote:
Mon Jul 30, 2018 9:14 am
The variable conflict is why using "import <filename>" alone is a bad idea. Variables on the global namespace will just overwrite each other.

A better method is to do something like this

Code: Select all

import <filename> as <someabbreviationoffilename>
e.g.

Code: Select all

import tkinter as tk
Then variables/functions from that file will need to have the correct namespace before they can be used.

Code: Select all

import tkinter as tk
root = tk.Tk()
It is difficult to make a more detailed recommendation without seeing your code, but it is generally a bad idea for you to have these variable conflicts in the first place. When you import a module, it (generally) shouldn't contain global variables, just functions or classes.
Erm, not sure what you are thinking here... The only difference between import tkinter and import tkinter as tk is that the local name bound to the module for the former will be called tkinter and for the latter will be called tk. In both cases any names declared in the module will only be in that module's namespace.

I think you are confusing this with from, e.g.

Code: Select all

from tkinter import *
which would bind local names to each of the module's module-level names.
She who travels light — forgot something.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Import "filename" to another project

Mon Jul 30, 2018 1:23 pm

You are correct. Monday morning brain!
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Import "filename" to another project

Tue Jul 31, 2018 2:05 am

scotty101 wrote:
Mon Jul 30, 2018 1:23 pm
You are correct. Monday morning brain!
Thought it was a strange mistake you made. I usually have those moments in the early hours.

To the OP, I don't think there is a way for Python to let you know when a name is already in use short of checking yourself if the name is in either the dictionaries returned by globals() or locals() first. If you can give a concrete example that is causing you problems we can try to point you in the right direction.
She who travels light — forgot something.

TeddyIP
Posts: 22
Joined: Fri Jul 20, 2018 9:47 am

Re: Import "filename" to another project

Tue Jul 31, 2018 10:14 am

Thanks all,
I have been thinking about the problem over the last couple of days. After taking on board your responses, I am trending towards what the variable returns after it exits. A I simplified breakdown as follows.

True/False will return 1, or 0 // possible conflict other variables are equal.
string will return // string + True/False
pins change state // sounds like a bad idea
Add delay // if variable hasn't been read/write in timeframe (ms)
add elif statement //

Return to “Python”