How to print a variable from a function at python prompt - python

There are various python files in a directory and all these contain a function desciption() as follows :
def description():
desc = 'something'
return desc
Now I have main.py as follows :
def a():
pth = os.listdir('homedir/workspace')
for filename in pth :
exec "import " + filename
desc = eval(filename + '.desciption()')
print desc
Right now when I run python main.py, nothing happens. How do I print this desc when I run python main.py?
Thanks in advance!

Assuming the import worked, and that you have imported a module called filename in each iteration, then you could get the module by name, and call its descrpition() method:
import sys
mod = sys.modules[filename]
print mod.description()
But note that it may make more sense to print the module's pydocs:
print mod.__doc__

You didn't close quotes properly in this line:
pth = os.listdir('homedir/workspace)
Also you should not use eval here:
desc = eval(filename + '.desciption()')
and I assume you wanted to import by variable here:
exec "import " + filename
This is how it should look like:
def a():
import importlib
pth = os.listdir('homedir/workspace')
for filename in pth :
mdl = importlib.import_module(os.path.splitext(filename)[0])
desc = mdl.description()
print desc
see https://docs.python.org/2/library/importlib.html#importlib.import_module
https://docs.python.org/2/library/os.path.html#os.path.splitext

You missed the closing ' in the pth variable
You've referenced desc on the last line without declaring it first.
Start your function with desc = None

def a():
pth = os.listdir('homedir/workspace')
for filename in pth:
module_name = os.path.splitext(filename)[0]
exec "import " + module_name
desc = eval(module_name + '.description()')
print desc
make sure the main.py located at the same path with your modules. or add it to sys path by
import sys
sys.path.append(r"homedir/workspace")

Related

In Python 3, how do I convert a file:// URL to an OS path with code that works in both Linux and Windows?

Here is code that demonstrates the problem when converting a file:// URL to an OS path.
import os
import pathlib
import sys
import urllib.parse
print ("sys.argv[0] = {0}".format(sys.argv[0]))
varFilename = sys.argv[0]
varFilename = os.path.abspath(varFilename)
print ("abs varFilename = {0}".format(varFilename))
varMainFolder = os.path.dirname(varFilename)
print ("varMainFolder = {0}".format(varMainFolder))
varTarget = os.path.join(varMainFolder,"test test.py")
print ("varTarget = {0}".format(varTarget))
varURL = pathlib.Path(varTarget).as_uri()
print ("varURL = {0}".format(varURL))
varPathRaw = urllib.parse.urlparse(varURL).path
print ("varPathRaw = {0}".format(varPathRaw))
varPathDecode = urllib.parse.unquote(varPathRaw)
print ("varPathDecode = {0}".format(varPathDecode))
varOSPath = os.path.normpath(varPathDecode)
print ("varOSPath = {0}".format(varOSPath))
In Linux, this code prints:
sys.argv[0] = test.py
abs varFilename = /home/ldbader/test.py
varMainFolder = /home/ldbader
varTarget = /home/ldbader/test test.py
varURL = file:///home/ldbader/test%20test.py
varPathRaw = /home/ldbader/test%20test.py
varPathDecode = /home/ldbader/test test.py
varOSPath = /home/ldbader/test test.py
Notice the varOSPath is a perfectly valid absolute path. But in Windows, the code prints:
sys.argv[0] = test.py
abs varFilename = C:\mli\Junk\test.py
varMainFolder = C:\mli\Junk
varTarget = C:\mli\Junk\test test.py
varURL = file:///C:/mli/Junk/test%20test.py
varPathRaw = /C:/mli/Junk/test%20test.py
varPathDecode = /C:/mli/Junk/test test.py
varOSPath = \C:\mli\Junk\test test.py
Notice that varOSPath has an absolute path preceded by an invalid backslash. Attempting to open a file with this path will fail. I would have expected os.path.normpath() to discard a slash to the left of a drive specification, but it doesn't.
What should I do so the same logic gives me a valid absolute path on both platforms?
Well, this is my answer. It appears to work for my scenarios, but I am not sure what will happen if a Linux user creates a folder named /C: (or any other name that looks like a Windows drive letter).
import os
import pathlib
import sys
import urllib.parse
print ("sys.argv[0] = {0}".format(sys.argv[0]))
varFilename = sys.argv[0]
varFilename = os.path.abspath(varFilename)
print ("abs varFilename = {0}".format(varFilename))
varMainFolder = os.path.dirname(varFilename)
print ("varMainFolder = {0}".format(varMainFolder))
varTarget = os.path.join(varMainFolder,"test test.py")
print ("varTarget = {0}".format(varTarget))
varURL = pathlib.Path(varTarget).as_uri()
print ("varURL = {0}".format(varURL))
varPathRaw = urllib.parse.urlparse(varURL).path
print ("varPathRaw = {0}".format(varPathRaw))
varPathDecode = urllib.parse.unquote(varPathRaw)
print ("varPathDecode = {0}".format(varPathDecode))
varOSPath = os.path.normpath(varPathDecode)
print ("varOSPath = {0}".format(varOSPath))
varFixedPath = varOSPath
varDrive = os.path.splitdrive(varFixedPath[1:])[0]
print ("varDrive = {0}".format(varDrive))
if varDrive:
varFixedPath = varFixedPath[1:]
print ("varFixedPath = {0}".format(varFixedPath))
The trick is to use os.path.splitdrive() on the absolute path after removing the first character. For a Windows absolute path, it will return the drive specification. For a Linux absolute path, it will return an empty string. If the drive specification is an empty string, use the absolute path as is. If the drive specification is not an empty string, remove the first character from the absolute path and use the result.
I still believe that the os.path.normpath() method should not return an absolute Windows path with a leading backslash.

Printing a value from a randint

import os
import random
path = os.listdir(r"file path here")
list = [os.listdir(r"life path here")]
print(len(path))
for i in range(len(path)):
full_path = (r"file path here" + path[i])
print(full_path)
print_random_items = random.randint(0, len(path[i]))
print(print_random_items)
So Hi I would like to know how I can print the name of the file associated with the value return to print(print_random_items)
ex: If the value is 15 I would like to print the 15th files name
First time asking a question here sorry if the format is wrong.
Don't bother with the random numbers. Just use random.choice:
random.choice(paths)
For example:
>>> import random
>>> paths = os.listdir('./exampledir')
>>> paths
['a.txt', 'b.txt', 'c.txt', 'd.txt', 'e.txt', 'f.txt']
>>> random.choice(paths)
'e.txt'
>>> random.choice(paths)
'c.txt'
Note: I see in your code that you are not familiar with python style iteration.
This
for i in range(len(path)):
full_path = (r"file path here" + path[i])
print(full_path)
is better written as
for partial_path in path:
full_path = r"file path here" + partial_path
print(full_path)
So rather than using range(len(path)) to get an index you can just iterate over path directly.

create user input directory name in python

I am trying to generate a python script that takes 3 input arguments and creates a directory whose name is a concatenation of the 3 arguments. The command i give is python new.py user1 32 male and I should get a directory name created as user1_32_male, but I am getting directory name as user_name + "" + age + "" + gender. Can someone please correct my code
#!/usr/bin/python
import pexpect
import numpy as np
#import matplotlib.pyplot as plt
#import pylab as p
from math import *
from sys import argv
import os.path
import numpy as np
import os, sys
#print "Hello, Python!"
script, user_name, age, gender = argv
dirname = user_name + "_" + age + "_" + gender
newpath = r'./dirname'
if not os.path.exists(newpath):
os.makedirs(newpath)
You put the expression you want to evaluate to the directory name you want in quotes, so it doesn't get evaluated. Try:
newpath = r'./' + user_name + "_" + age + "_" + gender
First of all, I noticed that you're importing things more than once. There's no reason to import os.path since it's included in os. The same goes with sys.
It's easier to use string substitution in cases like this. The tuple that comes after the % contains values that are substituted into the %s in the part before the %.
from sys import argv
import os.path
script, user_name, age, gender = argv
newpath = '%s_%s_%s' % (user_name, age, gender)
if not os.path.exists(newpath):
os.makedirs(newpath)

Python Sub process call with filename variable

I've got a small script with monitors when files are added or removed to a directory. The next step is for me to get the script to execute the files (windows batch files) once they’ve been added to the directory. I’m struggling to understand how to use a variable with subprocess call (if this is the best way this can be acheived). Could anyone help me please? Many thanks. Code looks like this so far ;
import sys
import time
import os
inputdir = 'c:\\test\\'
os.chdir(inputdir)
contents = os.listdir(inputdir)
count = len(inputdir)
dirmtime = os.stat(inputdir).st_mtime
while True:
newmtime = os.stat(inputdir).st_mtime
if newmtime != dirmtime:
dirmtime = newmtime
newcontents = os.listdir(inputdir)
added = set(newcontents).difference(contents)
if added:
print "These files added: %s" %(" ".join(added))
import subprocess
subprocess.call(%,shell=True)
removed = set(contents).difference(newcontents)
if removed:
print "These files removed: %s" %(" ".join(removed))
contents = newcontents
time.sleep(15)
This should do what you wanted, cleaned it up a little.
import sys
import time
import os
import subprocess
def monitor_execute(directory):
dir_contents = os.listdir(directory)
last_modified = os.stat(directory).st_mtime
while True:
time.sleep(15)
modified = os.stat(directory).st_mtime
if last_modified == modified:
continue
last_modified = modified
current_contents = os.listdir(directory)
new_files = set(current_contents).difference(dir_contents)
if new_files:
print 'Found new files: %s' % ' '.join(new_files)
for new_file in new_files:
subprocess.call(new_file, shell=True)
lost_files = set(dir_contents).difference(current_contents)
if lost_files:
print 'Lost these files: %s' % ' '.join(lost_files)
dir_contents = current_contents

Return a list of imported Python modules used in a script?

I am writing a program that categorizes a list of Python files by which modules they import. As such I need to scan the collection of .py files ad return a list of which modules they import. As an example, if one of the files I import has the following lines:
import os
import sys, gtk
I would like it to return:
["os", "sys", "gtk"]
I played with modulefinder and wrote:
from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script('testscript.py')
print 'Loaded modules:'
for name, mod in finder.modules.iteritems():
print '%s ' % name,
but this returns more than just the modules used in the script. As an example in a script which merely has:
import os
print os.getenv('USERNAME')
The modules returned from the ModuleFinder script return:
tokenize heapq __future__ copy_reg sre_compile _collections cStringIO _sre functools random cPickle __builtin__ subprocess cmd gc __main__ operator array select _heapq _threading_local abc _bisect posixpath _random os2emxpath tempfile errno pprint binascii token sre_constants re _abcoll collections ntpath threading opcode _struct _warnings math shlex fcntl genericpath stat string warnings UserDict inspect repr struct sys pwd imp getopt readline copy bdb types strop _functools keyword thread StringIO bisect pickle signal traceback difflib marshal linecache itertools dummy_thread posix doctest unittest time sre_parse os pdb dis
...whereas I just want it to return 'os', as that was the module used in the script.
Can anyone help me achieve this?
UPDATE: I just want to clarify that I would like to do this without running the Python file being analyzed, and just scanning the code.
IMO the best way todo this is to use the http://furius.ca/snakefood/ package. The author has done all of the required work to get not only directly imported modules but it uses the AST to parse the code for runtime dependencies that a more static analysis would miss.
Worked up a command example to demonstrate:
sfood ./example.py | sfood-cluster > example.deps
That will generate a basic dependency file of each unique module. For even more detail use:
sfood -r -i ./example.py | sfood-cluster > example.deps
To walk a tree and find all imports, you can also do this in code:
Please NOTE - The AST chunks of this routine were lifted from the snakefood source which has this copyright: Copyright (C) 2001-2007 Martin Blais. All Rights Reserved.
import os
import compiler
from compiler.ast import Discard, Const
from compiler.visitor import ASTVisitor
def pyfiles(startPath):
r = []
d = os.path.abspath(startPath)
if os.path.exists(d) and os.path.isdir(d):
for root, dirs, files in os.walk(d):
for f in files:
n, ext = os.path.splitext(f)
if ext == '.py':
r.append([d, f])
return r
class ImportVisitor(object):
def __init__(self):
self.modules = []
self.recent = []
def visitImport(self, node):
self.accept_imports()
self.recent.extend((x[0], None, x[1] or x[0], node.lineno, 0)
for x in node.names)
def visitFrom(self, node):
self.accept_imports()
modname = node.modname
if modname == '__future__':
return # Ignore these.
for name, as_ in node.names:
if name == '*':
# We really don't know...
mod = (modname, None, None, node.lineno, node.level)
else:
mod = (modname, name, as_ or name, node.lineno, node.level)
self.recent.append(mod)
def default(self, node):
pragma = None
if self.recent:
if isinstance(node, Discard):
children = node.getChildren()
if len(children) == 1 and isinstance(children[0], Const):
const_node = children[0]
pragma = const_node.value
self.accept_imports(pragma)
def accept_imports(self, pragma=None):
self.modules.extend((m, r, l, n, lvl, pragma)
for (m, r, l, n, lvl) in self.recent)
self.recent = []
def finalize(self):
self.accept_imports()
return self.modules
class ImportWalker(ASTVisitor):
def __init__(self, visitor):
ASTVisitor.__init__(self)
self._visitor = visitor
def default(self, node, *args):
self._visitor.default(node)
ASTVisitor.default(self, node, *args)
def parse_python_source(fn):
contents = open(fn, 'rU').read()
ast = compiler.parse(contents)
vis = ImportVisitor()
compiler.walk(ast, vis, ImportWalker(vis))
return vis.finalize()
for d, f in pyfiles('/Users/bear/temp/foobar'):
print d, f
print parse_python_source(os.path.join(d, f))
I recently needed all the dependencies for a given python script and I took a different approach than the other answers. I only cared about top level module module names (eg, I wanted foo from import foo.bar).
This is the code using the ast module:
import ast
modules = set()
def visit_Import(node):
for name in node.names:
modules.add(name.name.split(".")[0])
def visit_ImportFrom(node):
# if node.module is missing it's a "from . import ..." statement
# if level > 0 it's a "from .submodule import ..." statement
if node.module is not None and node.level == 0:
modules.add(node.module.split(".")[0])
node_iter = ast.NodeVisitor()
node_iter.visit_Import = visit_Import
node_iter.visit_ImportFrom = visit_ImportFrom
Testing with a python file foo.py that contains:
# foo.py
import sys, os
import foo1
from foo2 import bar
from foo3 import bar as che
import foo4 as boo
import foo5.zoo
from foo6 import *
from . import foo7, foo8
from .foo12 import foo13
from foo9 import foo10, foo11
def do():
import bar1
from bar2 import foo
from bar3 import che as baz
I could get all the modules in foo.py by doing something like this:
with open("foo.py") as f:
node_iter.visit(ast.parse(f.read()))
print(modules)
which would give me this output:
set(['bar1', 'bar3', 'bar2', 'sys', 'foo9', 'foo4', 'foo5', 'foo6', 'os', 'foo1', 'foo2', 'foo3'])
You might want to try dis (pun intended):
import dis
from collections import defaultdict
from pprint import pprint
statements = """
from __future__ import (absolute_import,
division)
import os
import collections, itertools
from math import *
from gzip import open as gzip_open
from subprocess import check_output, Popen
"""
instructions = dis.get_instructions(statements)
imports = [__ for __ in instructions if 'IMPORT' in __.opname]
grouped = defaultdict(list)
for instr in imports:
grouped[instr.opname].append(instr.argval)
pprint(grouped)
outputs
defaultdict(<class 'list'>,
{'IMPORT_FROM': ['absolute_import',
'division',
'open',
'check_output',
'Popen'],
'IMPORT_NAME': ['__future__',
'os',
'collections',
'itertools',
'math',
'gzip',
'subprocess'],
'IMPORT_STAR': [None]})
Your imported modules are grouped['IMPORT_NAME'].
It depends how thorough you want to be. Used modules is a turing complete problem: some python code uses lazy importing to only import things they actually use on a particular run, some generate things to import dynamically (e.g. plugin systems).
python -v will trace import statements - its arguably the simplest thing to check.
This works - using importlib to actually import the module, and inspect to get the members :
#! /usr/bin/env python
#
# test.py
#
# Find Modules
#
import inspect, importlib as implib
if __name__ == "__main__":
mod = implib.import_module( "example" )
for i in inspect.getmembers(mod, inspect.ismodule ):
print i[0]
#! /usr/bin/env python
#
# example.py
#
import sys
from os import path
if __name__ == "__main__":
print "Hello World !!!!"
Output :
tony#laptop .../~:$ ./test.py
path
sys
I was looking for something similar and I found a gem in a package called PyScons. The Scanner does just what you want (in 7 lines), using an import_hook. Here is an abbreviated example:
import modulefinder, sys
class SingleFileModuleFinder(modulefinder.ModuleFinder):
def import_hook(self, name, caller, *arg, **kwarg):
if caller.__file__ == self.name:
# Only call the parent at the top level.
return modulefinder.ModuleFinder.import_hook(self, name, caller, *arg, **kwarg)
def __call__(self, node):
self.name = str(node)
self.run_script(self.name)
if __name__ == '__main__':
# Example entry, run with './script.py filename'
print 'looking for includes in %s' % sys.argv[1]
mf = SingleFileModuleFinder()
mf(sys.argv[1])
print '\n'.join(mf.modules.keys())
Well, you could always write a simple script that searches the file for import statements. This one finds all imported modules and files, including those imported in functions or classes:
def find_imports(toCheck):
"""
Given a filename, returns a list of modules imported by the program.
Only modules that can be imported from the current directory
will be included. This program does not run the code, so import statements
in if/else or try/except blocks will always be included.
"""
import imp
importedItems = []
with open(toCheck, 'r') as pyFile:
for line in pyFile:
# ignore comments
line = line.strip().partition("#")[0].partition("as")[0].split(' ')
if line[0] == "import":
for imported in line[1:]:
# remove commas (this doesn't check for commas if
# they're supposed to be there!
imported = imported.strip(", ")
try:
# check to see if the module can be imported
# (doesn't actually import - just finds it if it exists)
imp.find_module(imported)
# add to the list of items we imported
importedItems.append(imported)
except ImportError:
# ignore items that can't be imported
# (unless that isn't what you want?)
pass
return importedItems
toCheck = raw_input("Which file should be checked: ")
print find_imports(toCheck)
This doesn't do anything for from module import something style imports, though that could easily be added, depending on how you want to deal with those. It also doesn't do any syntax checking, so if you have some funny business like import sys gtk, os it will think you've imported all three modules even though the line is an error. It also doesn't deal with try/except type statements with regards to import - if it could be imported, this function will list it. It also doesn't deal well with multiple imports per line if you use the as keyword. The real issue here is that I'd have to write a full parser to really do this correctly. The given code works in many cases, as long as you understand there are definite corner cases.
One issue is that relative imports will fail if this script isn't in the same directory as the given file. You may want to add the directory of the given script to sys.path.
I know this is old but I was also looking for such a solution like OP did.
So I wrote this code to find imported modules by scripts in a folder.
It works with import abc and from abc import cde format. I hope it helps someone else.
import re
import os
def get_imported_modules(folder):
files = [f for f in os.listdir(folder) if f.endswith(".py")]
imports = []
for file in files:
with open(os.path.join(folder, file), mode="r") as f:
lines = f.read()
result = re.findall(r"(?<!from)import (\w+)[\n.]|from\s+(\w+)\s+import", lines)
for imp in result:
for i in imp:
if len(i):
if i not in imports:
imports.append(i)
return imports
Thanks Tony Suffolk for inspect, importlib samples ... I built this wee module and you're all welcome to use it if it helps you. Giving back, yaaaay!
import timeit
import os
import inspect, importlib as implib
import textwrap as twrap
def src_modules(filename):
assert (len(filename)>1)
mod = implib.import_module(filename.split(".")[0])
ml_alias = []
ml_actual = []
ml_together = []
ml_final = []
for i in inspect.getmembers(mod, inspect.ismodule):
ml_alias.append(i[0])
ml_actual.append((str(i[1]).split(" ")[1]))
ml_together = zip(ml_actual, ml_alias)
for t in ml_together:
(a,b) = t
ml_final.append(a+":="+b)
return ml_final
def l_to_str(itr):
assert(len(itr)>0)
itr.sort()
r_str = ""
for i in itr:
r_str += i+" "
return r_str
def src_info(filename, start_time=timeit.default_timer()):
assert (len(filename)>1)
filename_in = filename
filename = filename_in.split(".")[0]
if __name__ == filename:
output_module = filename
else:
output_module = __name__
print ("\n" + (80 * "#"))
print (" runtime ~= {0} ms".format(round(((timeit.default_timer() - start_time)*1000),3)))
print (" source file --> '{0}'".format(filename_in))
print (" output via --> '{0}'".format(output_module))
print (" modules used in '{0}':".format(filename))
print (" "+"\n ".join(twrap.wrap(l_to_str(src_modules(filename)), 75)))
print (80 * "#")
return ""
if __name__ == "__main__":
src_info(os.path.basename(__file__))
## how to use in X file:
#
# import print_src_info
# import os
#
# < ... your code ... >
#
# if __name__ == "__main__":
# print_src_info.src_info(os.path.basename(__file__))
## example output:
#
# ################################################################################
# runtime ~= 0.049 ms
# source file --> 'print_src_info.py'
# output via --> '__main__'
# modules used in 'print_src_info':
# 'importlib':=implib 'inspect':=inspect 'os':=os 'textwrap':=twrap
# 'timeit':=timeit
# ################################################################################
For the majority of scripts which only import modules at the top level, it is quite sufficient to load the file as a module, and scan its members for modules:
import sys,io,imp,types
scriptname = 'myfile.py'
with io.open(scriptname) as scriptfile:
code = compile(scriptfile.readall(),scriptname,'exec')
newmodule = imp.new_module('__main__')
exec(codeobj,newmodule.__dict__)
scriptmodules = [name for name in dir(newmodule) if isinstance(newmodule.__dict__[name],types.ModuleType)]
This simulates the module being run as a script, by setting the module's name to '__main__'. It should therefore also capture funky dynamic module loading. The only modules it won't capture are those which are imported only into local scopes.
It's actually working quite good with
print [key for key in locals().keys()
if isinstance(locals()[key], type(sys)) and not key.startswith('__')]
I understand that this post is VERY old but I have found an ideal solution.
I came up with this idea:
def find_modules(code):
modules = []
code = code.splitlines()
for item in code:
if item[:7] == "import " and ", " not in item:
if " as " in item:
modules.append(item[7:item.find(" as ")])
else:
modules.append(item[7:])
elif item[:5] == "from ":
modules.append(item[5:item.find(" import ")])
elif ", " in item:
item = item[7:].split(", ")
modules = modules+item
else:
print(item)
return modules
code = """
import foo
import bar
from baz import eggs
import mymodule as test
import hello, there, stack
"""
print(find_modules(code))
it does from, as, commas and normal import statements.
it requires no dependencies and works with other lines of code.
The above code prints:
['foo', 'bar', 'baz', 'mymodule', 'hello', 'there', 'stack']
Just put your code in the find_modules function.
I'm editing my original answer to say this. This is doable with a code snippet like the one below, but parsing the AST may be the best way to go.
def iter_imports(fd):
""" Yield only lines that appear to be imports from an iterable.
fd can be an open file, a list of lines, etc.
"""
for line in fd:
trimmed = line.strip()
if trimmed.startswith('import '):
yield trimmed
elif trimmed.startswith('from ') and ('import ' in trimmed):
yield trimmed
def main():
# File name to read.
filename = '/my/path/myfile.py'
# Safely open the file, exit on error
try:
with open(filename) as f:
# Iterate over the lines in this file, and generate a list of
# lines that appear to be imports.
import_lines = list(iter_imports(f))
except (IOError, OSError) as exIO:
print('Error opening file: {}\n{}'.format(filename, exIO))
return 1
else:
# From here, import_lines should be a list of lines like this:
# from module import thing
# import os, sys
# from module import *
# Do whatever you need to do with the import lines.
print('\n'.join(import_lines))
return 0
if __name__ == '__main__':
sys.exit(main())
Further string parsing will be needed to grab just the module names. This does not catch cases where multi-line strings or doc strings contain the words 'import ' or 'from X import '. This is why I suggested parsing the AST.

Categories