Importing a function from a .py file - python

I work in a .ipybn file, but i want to import a function from a .py file.
My code is:
from function1 import my_function
However, I get the following error:
SyntaxError: unexpected EOF while parsing
How can I fix this? P.s the files are in the same folder.

You get the error when the file's source code ended before all the blocks in it are completed. For example, if in your file is:
a = input("> ")
if a == 'yes':
print("hello")
As you can see, you tell the program to proceed to print before the if statement is completed.

unexpected EOF while parsing
It was able to open the file, but not parse the content correctly. I would start by checking indentations (spaces vs tabs, # of spaces), quotes, colons.
Something to try is executing python from the command line and importing there. That will eliminate iPython/Jupyter notebook as a variable.

Related

Python subprocess.call with multiline string EOF

I've hit a issue that I don't really understand how to overcome. I'm trying to create a subprocess in python to run another python script. Not too difficult. The issue is I'm unable to get around is EOF error when a python file includes a super long string.
Here's an example of what my files look like.
Subprocess.py:
### call longstr.py from the primary pyfile
subprocess.call(['python longstr.py'], shell = True)
Longstr.py
### called from subprocess.py
### the actual string is a lot longer; this is an example to illustrate how the string is formatted
lngstr = """here is a really long
string (which has many n3w line$ and "characters")
that are causing the python file to state the file is ending early
"""
print lngstr
Printer error in terminal
SyntaxError: EOF while scanning triple-quoted string literal
As a work around, I tried to remove all linebreaks as well as all spaces to see if it was due to it being multi-line. That still returned the same result.
My assumption is that when the subprocess is running and the shell is doing something with the file contents, when the new line is reached the shell itself is freaking out and that's what's terminating the process; not the file.
What is the correct workaround for having subprocess run a file like this?
Thank you for your help.
Answering my own question here; my problem was that I didn't file.close() before trying to execute a subprocess.call.
If you encounter this problem, and are working with recently written files this could be your issue too. Thank you to everyone who read or responded to this thread.

Playing Audio with subprocess.call in Python

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"])

Why do I get a SyntaxError <unicode error> on my import statement? Should this not be simple?

Here's my first simple test program in Python. Without importing the os library the program runs fine... Leading me to believe there's something wrong with my import statement, however this is the only way i ever see them written. Why am I still getting a syntax error?
import os # <-- why does this line give me a syntax error?!?!?! <unicode error> -->
CalibrationData = r'C:/Users/user/Desktop/blah Redesign/Data/attempts at data gathering/CalibrationData.txt'
File = open(CalibrationData, 'w')
File.write('Test')
File.close()
My end goal is to write a simple program that will look through a directory and tabularize data from relevant .ini files within it.
Well, as MDurant pointed out... I pasted in some unprintable character - probably when i entered the URL.

Python script, syntax error, invalid syntax

I have the following python script which i want to run..
However, it keeps showing the error message on my command prompt whenever i attempt to run the script.
Error message:
File "xor.py", line 9
File = open(sys.argv[1], 'rb').read<>
SyntaxError: Invalid Syntax
The following is the command i executed in cmd:
python xor.py sample_output.txt 'what would the secret be?'
The following is the script:
# xor.py
import sys
from itertools import cycle
file = open(sys.argv[1], 'rb').read()
string = sys.argv[2]
sys.stdout.write(''.join(chr(ord(x)^ord(y)) for (x,y) in zip(file, cycle(string))))
You are not running the code you are editing, instead you are running a different file than the one you edited.
This is because there is no syntax error in the code that you have provided. However, there is a syntax error in the code in the error message:
File = open(sys.argv[1], 'rb').read<>
This ends with <>, not with (). I assumed this to be a transcription error, but you say that the error message really appears like this, although the code does not.
Hence: You are running a different file than the one you are editing.
You have .read<> when you probably intended .read()
First of all, "file" is already reserved; that is built-in keyword so unable to set as the name of variable.
And second, do not use <> instead of (). incorrect in grammar.
The problems might be clearly solved if you code like:
fd = open(sys.argv[1], 'rb').read()

Syntax for input redirection in IDLE

I need to enter the contents of a text (.txt) file as input for a Python (.py) file. Assuming the name of the text file is TextFile and the name of the Python file PythonFile, then the code should be as follows:
python PythonFile.py < TextFile.txt
Yet, when I try to do this in IDLE and type in
import PythonFile < TextFile,
IDLE gives me an invalid syntax message, pointing to the < sign. I tried all sorts of variations on this theme (i.e.,using or not using the file name extensions), but still got the same invalid-syntax message. How is the syntax different for input redirection in IDLE?
If it works in the command line, then why do you want to do this in IDLE? There are ways to achieve a similar result using, for example, subprocess, but a better way would be to refactor PythonFile.py so that you can call a function from it, e.g.:
>>> import PythonFile
>>> PythonFile.run_with_input('TextFile.txt')
If you post the contents of PythonFile.py, we might be able to help you do this.

Categories