Accessing variable from file pyspark - python

I have one variable.list file. All variables are defined here, like:
Tablename="emp"
Dbname="hr"
Now I want access all these variables from main.py script.
I have seen some thread on this where it is suggested to do following.
From variable.list import *
But it is throwing error that variable.list not defined.
No module name variable.list
Where to keep variable.list file? How to import all the variables?

When you write import variables.list python searching for variables module not a file. You can rename your file as variables.py then try
import variables

Related

How to import class from other directory following this structure python

Reading other posts I have structured my project as below:
/__init__.py
/src/main.py
/src/search-tree/node.py
/src/search-tree/multi_child_node.py
/src/utils/node_generator.py
Now when I'm inside node_generator.py I would like to import multi_child_node.py and use its class but I don't know how to do it, I have tried from .x import y but nothing. I'm new in python so maybe I'm missing something very simple.
Actually, the file structure depends on where do you run your main file, not the file itself.
For example, if your main.py calls node_generator.py like this
from utils import node_generator
and then node_generator.py calls multi_child_node.py, it should call it as it was called from main.py:
from search_tree import multi_child_node
P.S, you shouldn't use dash - or spaces in the naming for your module, but if you have to, check out the answer for this question How to import module when module name has a '-' dash or hyphen in it?

Python function from imported file shows as undefined

I have a moveslist.py file with function movesSelection and a main.py file. Within my main.py, I've made sure that my variables are set to global and I had another function makeNewChar. Within makeNewChar, I'm trying to call movesSelection but I get "NameError: name 'movesSelection' is not defined.
Since my moveslist.py uses some global variables from main, I imported main into the file.
For my main.py, I did from moveslist import *. I also tried import movesList and from moveslist import movesSelection. All of those threw back errors.
How can I use movesSelection within my main.py?
There may be a few different reasons but some of the most common are having a file with the same name or likely the file you are looking for is in another directory so i will show you my correct here
[Edit] You should remove the .py when importing example below
## This is "main.py" keep that in mind when viewing
## Change line 4 to import and the python file as Func = Func.py in this context
import Func
Func.Activate()
## This is "Func.py"
class Activate:(
print("Hello World!")
)
Or you may need to do as says below!
##Include file extension and move the file with the folder in the correct space and i'd appreciate it if you'd be so kind to mark this question as correct
import movesSelection.py
Add "from moveslist import movesSelection" into makeNewChar.
You do not have to add the .py suffix when you import.

Python: NameError when trying to use "re" inside a function

I have a script called main_plotter.py that looks like this:
import re
import numbs
numbs.getSquares("file.csv")
numbs.py is the file that I'm importing from. It looks like this:
def getSquares(sqfile):
infile=sqfile
base_name = re.split(".csv", infile)[0]
print (base_name)
When I run main_plotter.py, I get NameError: name 're' is not defined.
Why is this happening? I tried adding global re before the import re statement, but that doesn't help either. Aren't the import statements supposed to be global anyway? Any help appreciated!
PS. the code runs as expected if I import re inside the numbs.py file.
"Global" in Python means "module namespace". Any import re happens exactly there -- module-by-module; there intentionally does not exist any wider scope, which ensures that the content of any Python module can be understood by reading only that module (unlike Ruby, where to know the context in which code is run you need to read every module that was ever loaded by the same interpreter).
If you want to use the re module in numbs.py, you should have a separate import re inside that file. This doesn't reload the module from disk, but just adds a namespace entry pointing to the already-cached instance that was loaded on first reference.

Why is this python code running twice [duplicate]

This question already has an answer here:
Why Python print my output two times when I import the same file in which I am printing?
(1 answer)
Closed 2 years ago.
Here is the code
import random
print("Hello", end="")
print("twice")
and a screenshot of the code
When I execute this code it for some reason is running twice. The problem seems to be from the import random statement, because if I either remove that statement or import some other module it works fine.
What could be the reason for this, should I reinstall Python on my system.
There's nothing wrong with python.
The reason is simple:
Your module is importing itself (because it is also named random) - this has to do with the lookup mechanics of python. python will try to import from your root folder first, before modules from pythonpath are imported.
From the docs:
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
The directory containing the input script (or the current directory
when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
The installation-dependent default.
Since your file (module) is called random.py, import random will import this very file.
Now, what does "import" mean?
The statement import something will cause Python to lookup the name something, starting with the current directory.
Therefore, import random will result in an import of this very file, since its name will shadow the build-in random.
Besides, if the name to import is already in the namespace, then the import statement is ignored.
Once the module to import has been located, its code is executed.
As a result, the flow of your script is as follow:
Lookup the random.py name
Add random to the namespace
Execute the code contained in random.py
The random name already exists in the namespace, so the import random statement is ignored
Print the text
Print the text
The reason for this is you've named the script random.py and inside it you import random.
random will not import the built-in random module but, rather, the random module you've created. This leads to the script executing the same statements twice (and also leads to other ugly errors if you tried and import something from random, like from random import randrange.)
Renaming the script leads to normal behavior.
Because your scripts is called random.py, so when you import random you are executing your script as well. Mind to name correctly your scripts.
your python script is named random.py so when you import random it import itself , in python when you import module it will run it.
therefor you get print twice.
rename your script or remove the import

Python import module from relative folder

i'm creating a python program, and i want to split it up into seperate files. I'm using import to do this, but its not working (specifically, a variable is stored in one python file, and its not being read by the main one.
program /
main.py
lib /
__init__.py
config.py
functions.py
I have in main.py:
import lib.config
import lib.functions
print(main)
and config.py has
main = "hello"
I should be getting the output "hello", when i'm executing the file main.py, but i'm not. I have the same problem with functions stored in functions.py
Any help would be great,
Importing the module with a simple import statement does not copy the names from that module into your own global namespace.
Either refer to the main name through attribute access:
print(lib.config.main)
or use the from ... import ... syntax:
from lib.config import main
instead.
You can learn more about how importing works in the Modules section of the Python tutorial.

Categories