I'm using Python 3.9
I have the following structure:
parent_folder
|
|-->. useful_functions.py .
|-->. script_folder_1 .
|
|-->. script_1.py
|-->. function_import.py
|-->. script_folder_2 .
|
|-->. script_2.py
|-->. function_import.py
script_1.py and script_2.py are very similar (but not identical), and so both want to use the functions stored in useful_functions.py
I have worked out that I can import the functions from useful_functions.py by putting the following at the top of script_1.py and script_2.py:
import sys
import os
local_dir = os.getcwd()
dir_up = os.path.dirname(local_dir)
sys.path.append(dir_up)
import useful_functions as uf
This works, meaning it allows script_1.py to call functions from useful_function.py as uf.function_name(arguments)
Since this text block is identical in script_1.py and script_2.py, I wanted to pull it out and make it a function in a different file function_import.py, since that way if I need to modify it I can modify the standalone file in script_folder_1 and copy it to script_folder_2.
However, when I create function_import.py, function_import.py is able to "locally" access the functions from useful_functions.py, but script_1.py is not able to access the functions from useful_functions.py.
Details:
def function_import():
import sys
import os
import re
local_dir = os.getcwd()
dir_up = os.path.dirname(local_dir)
sys.path.append(dir_up)
import useful_functions as uf
print(uf.test_function())
script_1.py:
import function_import
function_import.function_import()
uf.test_function()
When I run script_1.py in the terminal, uf.test_function() returns 3, the placeholder value, inside of function_import(), but then when script_1.py calls uf.test_function(), I get:
NameError: name 'uf' is not defined
Is there a way to allow a function inside a script to import modules for the script to use? Or is there some other way entirely that I should be doing this?
I read these posts and they didn't seem to have a good solution to my problem:
https://stackoverflow.com/search?q=import+from+parent+directory+python&s=5452f193-c78d-4966-b69a-896fb6a5a5f8
Import Script from a Parent Directory
This doesn't define a function inside function_import.py but it does the job
function_import.py:
import sys
import os
import re
local_dir = os.getcwd()
dir_up = os.path.dirname(local_dir)
sys.path.append(dir_up)
import useful_functions as uf
useful_functions.py:
def test_function():
print('In the test function')
script_1.py:
from function_import import uf
uf.test_function()
Outputs In the test function
It's not the perfect solution but you can wrap your function into another one like so :
function_import.py:
def function_import():
import sys
import os
import re
local_dir = os.getcwd()
dir_up = os.path.dirname(local_dir)
sys.path.append(dir_up)
import useful_functions as uf
#print(uf.test_function())
def uf_test_function(*args, **kwargs):
uf.test_function(*args, **kwargs)
script_1.py:
import function_import
function_import.function_import()
function_import.uf_test_function()
Related
below the folder structure of my software:
below the code of all the .py files:
run.py:
import modules.module_01.aa as a
a.test()
# test:
if __name__=="__main__":
pass
aa.py (module 1):
import libraries.qq as q
import libraries.zz as z
def test():
q.qq_fun()
z.zz_fun()
print("ciao")
qq.py (library used by aa.py):
def qq_fun():
pass
zz.py (library used by aa.py):
def zz_fun():
pass
my question is really simple, why when I run "run.py" Python say to me:
why "aa.py" can't import the module "qq.py" and "zz.py"? how can I fix this issue?
run.py
In run.py, the Python interpreter thinks you're trying to import module_01.aa from a module named module. To import aa.py, you'll need to add this code to the top of your file, which adds the directory aa.py is in to the system path, and change your import statement to import aa as a.
import sys
sys.path.insert(0, "./modules/module_01/")
aa.py
The same problem occurs in aa.py. To fix the problem in this file, you'll need to add this code to the top of aa.py, which adds the directory qq.py and zz.py are in, and remove the libraries. from both of your import statements.
import sys
sys.path.insert(0, "./modules/module_01/libraries")
I have three files and I need to use one variable in these three files but the problem is when I use the variable in the third file test.py, it uses the first value from var.py. Also, when I define a new variable in the run.py and import the variable in the third file test.py, it keeps running without run the third file test.py and give this message non-resource variables are not supported in the long term. In addition, I still have a file env.py that I import in 'test.py' using the modified 'var.demend'
var.py
demend=-1
run.py
import var
print(var.demend)
for i in range(0,6) :
var.demend=i
gc.collect()
bots = [subprocess.check_call(["python"+version, os.path.join(current_dir, "test.py")])])]
modules = map(__import__,bots)
import multiprocessing,subprocess
for bot in (bots):
p = multiprocessing.Process(target=lambda: __import__(bot))
p.start()
test.py
import env
import var
print(var.demend)
env.py
import var
print(var.demend)
I use the demend in run.py file and then I pass the new parameter str(i)
run.py
import os
import var
import subprocess,_multiprocessing
import sys
import gc # Garbage Collector
print(var.demend)
version = ".".join(map(str, sys.version_info[:3]))
if len(version) >3:
version=version[:-2];
current_dir = os.path.dirname(os.path.realpath(__file__))
for i in range(0,6) :
demend=i
gc.collect()
bots = [subprocess.check_call(["python"+version, os.path.join(current_dir, "test.py"),str(i)])]
modules = map(__import__,bots)
import multiprocessing,subprocess
for bot in (bots):
p = multiprocessing.Process(target=lambda: __import__(bot))
p.start()
In the test.py file
import env
from sys import argv
demend=int(argv[1])
print(demend)
In the env.py file
from sys import argv
demend=int(argv[1])
print(demend)
I have a directory as such:
python_scripts/
test.py
simupy/
__init__.py
info.py
blk.py
'blk.py' and 'info.py are modules that contains several functions, one of which is the function 'blk_func(para)'.
Within '__init__.py' I have included the following code:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
file_lst = os.listdir(dir_path)
filename_lst = list(filter(lambda x: x[-3:]=='.py', file_lst))
filename_lst = list(map(lambda x: x[:-3], filename_lst))
filename_lst.remove('__init__')
__all__ = filename_lst.copy()
I would like to access the function 'blk_func(para)', as well as all other functions inside the package, within 'test.py'. Thus I import the package by putting the following line of code in 'test.py':
from simupy import*
However, inorder to use the function, I still have to do the following:
value = blk.blk_func(val_param)
How do I import the package simupy, such that I can directly access the function in 'test.py' by just calling the function name? i.e.
value = blk_func(val_para)
Pretty easy
__init__.py:
from simupy.blk import *
from simupy.info import *
Btw, just my two cents but it looks like you want to import your package's functions in __init__.py but perform actions in __main__.py.
Like
__init__.py:
from simupy.blk import *
from simupy.info import *
__main__.py:
from simupy import *
# your code
dir_path = ....
It's the most pythonic way to do. After that you will be able to:
Run your script as a proper Python module: python -m simupy
Use your module as library: import simupy; print(simupy.bar())
Import only a specific package / function: from simupy.info import bar.
For me it's part of the beauty of Python..
I have three files under the same directory, namely, main.py, Newtester.py, and fileUtility.py. In Newtester.py there is a class named Function. In main.py, there are the following codes:
from file.py import *
...
def main():
...
funcs = parseFuncSpec(funcInputFile)
parseFuncSpec is defined in fileUtilities.py as:
some code to import Newtester.py
def parseFuncSpec(fName):
curFunc = function(funcName, numTest, [], score)
Regardless of what I put in import Newtester.py, I always get an error saying "Function" (the class defined in the file "Newtester.py") is not defined. Following Python: How to import other Python files, I have attempted
import Newtester
__import__("Newtester")
exec("Newtester.py")
exec("Newtester")
import importlib
importlib.__import__("Newtester")
os.system("Newtester.py")
But none of them seemed to work. Any advice is appreciated. See https://github.com/r2dong/unitTesting if you are interested in seeing the complete files.
It's because you are not using it correctly
well when you use import statement like below only Newstester file is imported
import Newtester
hence instead of using parseFuncSpec() directly you have to use it as Newtester.parseFuncSpec()
or to use parseFuncSpec() directly you need to use below import statement:
from Newtester import parseFuncSpec
I'm trying to use a function called start to set up my enviroment in python. The function imports os.
After I run the function and do the following
os.listdir(simdir+"main")
I get a error that says os not defined
code
>>> def setup ():
import os.path
import shutil
simdir="e:\\"
maindir="c:\\backup\\bitcois\\test exit\\"
>>> setup()
>>> os.listdir(simdir+"main")
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
os.listdir(simdir+"main")
NameError: name 'os' is not defined
The import statement is scoped. When importing modules they are defined for the local namespace.
From the documentation:
Import statements are executed in two steps: (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the import statement occurs). [...]
So in your case the os package is only defined within function setup.
You are getting this error because you are NOT importing the whole os library but just the os.path module. In this way, the other resources at the os library are not made available for your use.
In order to be able to use the os.listdir method, you need to either import it alongside the os.path like this:
>>> def setup ():
import os.path, os.listdir
import shutil
simdir="e:\\"
maindir="c:\\backup\\bitcois\\test exit\\"
or import the full library:
>>> def setup ():
import os
import shutil
simdir="e:\\"
maindir="c:\\backup\\bitcois\\test exit\\"
You can read more here:
https://docs.python.org/2/tutorial/modules.html
try:
import os.path
import shutil
import glob
def setup ():
global simdir
simdir="e:\\"
maindir="c:\\backup\\bitcois\\test exit\\"
setup()
os.listdir(simdir+"main")
You need to return the paths and assign the returned values in the global scope. Also, import os too:
import os
def setup():
# retain existing code
return simdir, maindir
simdir, maindir = setup()
When you import os or do any sort of command within a function, the command's effect only last while that function itself is running. What you need to do is
import os
...Do your function and other code
This way, your import lasts for the whole program :).