My file name is 'corr.py' located in the Downloads folder and I'm running python 2.7 in terminal in the correct working directory. When I run the command 'python corr.py' I get the error message below. What should I do?
>>> os.getcwd()
'/Users/Daniel/Downloads'
>>> python corr.py
File "<stdin>", line 1
python corr.py
^
SyntaxError: invalid syntax
>>> python3 corr.py
File "<stdin>", line 1
python3 corr.py
^
SyntaxError: invalid syntax
You're in a Python interpreter already. First type exit() to get back to the terminal. (Or just open a new terminal tab/window.) Then use python corr.py.
Related
SOLVED: see solution in replies
I have a bash script that looks like this:
#!/bin/bash
python3 /Users/me/path/to/my/file.py
python3 /Users/me/path/to/my/file2.py
Rscript /Users/me/path/to/my/rfile.R
python3 /Users/me/path/to/my/file.py
When I execute it from terminal with the command
bash /Users/me/path/to/my/shellscript.sh
it executes perfectly. However, I would like to be able to run it from Keyboard Maestro or AppleScript. In AppleScript, when I run
do shell script "bash /Users/me/path/to/my/shellscript.sh"
I get the following error message:
error "/Users/me/path/to/my/shellscript.sh: line 2: python3: command not found
/Users/me/path/to/my/shellscript.sh: line 3: python3: command not found
/Users/me/path/to/my/shellscript.sh: line 4: Rscript: command not found
/Users/me/path/to/my/shellscript.sh: line 5: python3: command not found" number 127
I receive the same error message if I run the equivalent command in Keyboard Maestro. I have tried changing python3 to python, but it fails anyway because it runs the files in Python 2 (despite the shebang of the Python files specifying Python 3), and the Rscript command would surely still throw an error.
What is the difference between the environments that causes the shell script to fail in Keyboard Maestro and AppleScript but not Terminal?
I got it to work by changing the command to PATH=$PATH:/usr/local/bin; bash /Users/me/path/to/my/shellscript.sh.
I am using MACOSX 10.12.3 and Python 3.52
I am running into issues when running pydoc in a bash script in python IDLE.
For example:
myFile = ‘/home/user/afile.py’
import subprocess
subprocess.run([‘pydoc’, ‘-w’, myFile])
Inevitably does not create the html file as would be expected.
On the other hand, when I run pydoc in the terminal
pydoc3 -w /home/user/afile.py
it always creates the wanted html file. But pydoc -w /home/user/afile.py generates an error
This is due to the fact that:
the python /home/user/afile.py contains a few print statements in python3 style. That is: print('blabla') instead of: print 'blabla' as would be the case on python2.
the bash script in IDLE does not load the correct pydoc. I should also mention that running subprocess.run([‘pydoc3’, ‘-w’, myFile]) generates an error, for example:
subprocess.run(['pydoc3', '-w', myFile]) File
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py",
line 693, in run
with Popen(*popenargs, **kwargs) as process: File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py",
line 947, in init
restore_signals, start_new_session) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py",
line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'pydoc3'
Any one has a way to circumvent this please?
Question 1:
Regarding running the python 3 version of pydoc against python 2 code- That will not work. Pydoc will execute (import) the target module. You would need to use python 2 version of pydoc by modifying your path or using your favorite virtual environment tool.
Question 2:
If you can get a reference to your desired installation of python (by setting the path or activating a virtual environment), then you can handle missing or broken entry points with this syntax:
python -m pydoc -w myfile
You will also need to make sure myfile is in the current directory or on the PYTHONPATH or otherwise importable
Good Day,
I am trying to run command line stuff from Sublime text's console. Is that possible? doing stuff like "ls" doesn't work
ls
Traceback (most recent call last):
File "", line 1, in
NameError: name 'ls' is not defined
The Sublime Text console is actually the built-in Python interpreter, so it only accepts Python commands. You can use the os* module to interact with the operating system. The following commands will give you a directory listing:
import os
os.chdir('/path/to/something')
os.listdir('.')
Check out the Files and Directories* section of the docs for more commands.
* Sublime Text 2 uses Python 2.6, while ST3 uses version 3.3. If you're using ST3, just change the 2.6 in the URLs I gave to 3 and you'll get the correct documentation.
I get an error when I try to create a new virtualenv using virtualenvwrapper. Here's the command I'm trying:
mkvirtualenv -a . -i Flask ~/.virtualenvs/dcc-admin/
Here's the output:
New python executable in /Users/raddevon/.virtualenvs/dcc-admin/bin/python
Installing setuptools......
Complete output from command /Users/raddevon/.vir...dcc-admin/bin/python -c "#!python
\"\"\"Bootstra...sys.argv[1:])
" /Library/Python/2.7/...ols-0.6c11-py2.7.egg:
Processing setuptools-0.6c11-py2.7.egg
Removing /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg
error: /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg: Permission denied
----------------------------------------
...Installing setuptools...done.
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 8, in <module>
load_entry_point('virtualenv==1.9.1', 'console_scripts', 'virtualenv')()
File "/Library/Python/2.7/site-packages/virtualenv.py", line 979, in main
no_pip=options.no_pip)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 1091, in create_environment
search_dirs=search_dirs, never_download=never_download)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 611, in install_setuptools
search_dirs=search_dirs, never_download=never_download)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 583, in _install_req
cwd=cwd)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 1057, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /Users/raddevon/.vir...dcc-admin/bin/python -c "#!python
\"\"\"Bootstra...sys.argv[1:])
" /Library/Python/2.7/...ols-0.6c11-py2.7.egg failed with error code 1
I thought I could get around this with sudo, but that gives me sudo: mkvirtualenv: command not found.
I've checked my permissions on the file causing the error, and my user appears to have read and write permissions.
I'm not sure where to go from here. mkvirtualenv should be able to delete that file since I have the delete permission for it. Alternately, sudo should be able to run mkvirtualenv. Someone please tell me where I'm going wrong.
Update: I figured out part of this problem. virtualenvwrapper was trying to run the Python commands through a different install of the Python interpreter than the one I wanted. I used the -p switch to specify the correct interpreter, and that worked.
I read up on this, and, apparently, it should be using the same interpreter that would be the default in my shell, but this is not the case. The interpreter used as the default by the shell is /usr/local/bin/python but virtualenvwrapper is trying to run through /Library/Python/... I'm still unsure why this is the case. /usr/local/bin is the last python path defined in my .zshrc.
I currently found a workaround
virtualenv env -p /usr/local/bin/python
I saved a python file to my desktop. In order to test it, I used the command prompt. However, my command prompt is located in
C:\Users\Name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories
To change the current directory, I wrote
cd C:\Users\Name\Desktop
in the command prompt. However, it keeps telling me that I have an "invalid syntax"
>>>cd C:\Users\Name\Desktop
File "<stdin>", line 1
cd C:\Users\Name\Desktop
^
SyntaxError: invalid syntax
Any suggestion to fix this problem?
the >>> prompt indicates you are already running python interpreter.
You need to enter that command from the shell prompt.
Press Ctrl-D to exit Python to the ordinary shell prompt and try again.
P.S: I'm not related to anyway with python Reference : http://ubuntuforums.org/showthread.php?t=1558728
try this on ms-dos
cd\
cd C:\Users\YourUserName\Desktop
python filename.py