I'm trying to import another python script with a name of the form fe_fi_fo_fam.py, I'm using importlib.import_module("fe_fi_fo_fam.py") but I'm getting the error : "module fe not found" , how do I get it to read the whole string instead of only the first part? I'm new to python and I seem to have scoured the internet without an answer.
Yo can instead use the built-in function exec after you compose the module string
>>> modOs = 'os'
>>> exec('import ' + modOs)
>>> os.getcwd()
'C:\\Desktop'
Note: Python 3.6 exec
I think you have to understand the difference between module name in Python and file name. importlib.import_module() gets a "module name", not a "file name", so "fe_fi_fo_fam.py" is not a valid argument, instead you have to use "fe_fi_fo_fam" with no .py extension.
Related
I have a python script that successfully sends SOAP to insert a record into a system. The values are static in the test. I need to make the value dynamic/argument that is passed through the command line or other stored value.
execute: python myscript.py
<d4p1:Address>MainStreet</d4p1:Address> ....this works to add hard coded "MainStreet"
execute: python myscript.py MainStreet
...this is now trying to pass the argument MainStreet
<d4p1:Address>sys.argv[1]</d4p1:Address> ....this does not work
It saves the literal text address as "sys.argv[1]" ... I have imported sys ..I have tried %, {}, etc from web searches, what syntax am I missing??
You need to read a little about how to create strings in Python, below is how it could look like in your code. Sorry it's hard to say more without seeing your actual code. And you actually shouldn't create XMLs like that, you should use for instance xml module from standard library.
test = "<d4p1:Address>" + sys.argv[1] + "</d4p1:Address>"
This is my Python file (myfile.py). I am new to Python and programming.
title = "The meaning of life" #myfile.py
And it's present in 'learning_python' directory.
Try this:
from myfile import *
you should then be able to access the variable by it's name. (in your case, title)
Why do I receive the error message in the title from the code below?
Edit: Cause I didn't pay attention to how I wrote "ascii". Thanks everyone
The code below works fine on my Iphone IDE but not on my Windows 7 (w/Notepad++ and Command Prompt). I checked the directory to see if any string.py files existed which I did not see any. I ran a search on my desktop and found 4 files named that, two of which said they were complied. I deleted the compiled files and left the other two. I'm a noob.
import string
import random
x = string.acsii_letters
y = random.choice(x)
print y * 5
It should be string.ascii_letters letters instead of string.acsii_letters. If that's a typo in code statement here only, then your guess must be right, there is another string module in your PYTHONPATH. Open python shell,
import string
print(string.__file__)
to ensure string is being imported from right path. If its not remove that path from PYTHONPATH.
In python 3 I found that using the string.ascii_letters works as string.letters results in an AttributeError.
You have a typo. It should be string.ascii_letters or string.letters. You can look at the attributes of the string module with dir(string) and see what you can access.
i had the same issue the reason was that the name of the file is the same of the module name . so just rename your file the module will work well
I wanted to play a .wav file, without using external modules, and i read i could do that using this:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", /Users/me/Downloads/sample.wav])
I however get:
SyntaxError: invalid syntax
If i use os.path.realpath to get the absolute path of the file, i get just the same thing. (The path i see at get info)
Environment is OSX, Python 2.7
Can someone tell me what i am doing wrong? I am new to Python (and to Programming).
There are multiple problems.
Indentation
Code inside the function should be indented, to show that it is part of the function
File name should be in a quotes
It should be a string
It should be:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", "/Users/me/Downloads/sample.wav"])
I am trying to make a dictionary with pickle by using command line but while getting input from command line I am getting "module object has no attribute load " ?
Here's my code:
import pickle
import sys
dfile = open ("new.dat","w")
print "get argument"
lifesize=(sys.argv[1])
print "get another argument"
two=sys.argv[2]
print "last argument"
three=sys.argv[3]
z={lifesize:[two,three]}
pickle.dump(z,dfile)
dfile.close()
ifile=open("new.dat")
d1= pickle.load(ifile)
and save above as newdocument
cmd:python newdocument.py
I also tried to do a dictionary for every lifesize and save them as new.dat and get them..
need really help .?thank you
The code you posted is perfectly fine, please check your version of Python, and possibly update/reinstall.
The Python interpreter complains that pickle.load() doesn't exist, while it certainly does: http://docs.python.org/library/pickle.html#pickle.load