Can't Configure Sublime Text for Python 3 on Mac [duplicate] - python

I installed sublime text 2 to OSX 10.8.2.
In my Mac, python 2.7.3 is installed.
In sublime text2, I just type
print 'Hello'
but error occurred like below.
/usr/bin/python: can't find '__main__' module in ''
[Finished in 0.2s with exit code 1]
How can I fix it?

I got the same error as I didn't save the script before executing it. Check to see if you have saved it!

Note to anyone else:
If you have a directory like so, you can add a __main__.py file to tell the interpreter what to execute if you call the module directly.
my_module
|
| __init__.py
| my_cool_file.py # print "Hello World"
| __main__.py # import my_cool_file
$ python my_module # Hello World

You need to SAVE your code file with the ".py" extension. Then, on the 'Tools/Build System' menu, make sure your build system is set to either 'auto' or 'Python'. What that message is telling you is there is no valid Python file to 'build' (or, in this case just run).

First save your program file type as "example.py then run your code its working fine.

did you add the shebang to the top of the file?
#!/usr/bin/python

Don't run with a space between the directory and the filename:
python /root/Desktop/1 hello.py
Use a / instead:
python /root/Desktop/1/hello.py

The problem: The format of the file as to how it is saved.
Use a proper text editor and save it with the .py extension
and run it in terminal.
eg: file name should be saved as `example.py`
run
python example

Save the .py files before you build in sublime.Such as save the file on desktop or other document.

Make sure that you aren't clicking on "Run unnamed" from 'run' tab. You must click on "run ". Or just click the green shortcut button.

Edit the configuration and then in the box: Script path, select your .py file!

You get that error because you haven't saved your file, save it for example "holamundo.py" then run it Ctrl + B

Related

No such file or directory

I am trying to use Pillow Library in Python. I am writing basic code to open up a picture that I have saved.
But every time I run the code, it says
"can't open file 'C:\Users\saadn\PycharmProjects\Giraffe\main.py':
[Errno 2] No such file or directory".
The path for where the picture is saved is "C:\Users\saadn\PycharmProjects\Giraffe\DirectoryAssign1"
Now I know that in the error it says "Girrafe\main.py". but I don't have a python file named main at all. Someone guide me.
the code I wrote was
from PIL import Image
img = Image.open("dany.jpg")
img.show()
Here is a screenshot for better understanding.
Actually I think there are 2 issues, and not 1.
Error 1 - Wrong File is being tried to run :
In the picture you attached there is an error which means it cannot open main.py (No such file) . You entered a wrong file name, your file name is OpenAndShow.py
How to fix?
Be sure to open the terminal or command prompt and be sure you are in the following directory C:\Users\saadn\PycharmProjects\Giraffe\ and enter the following command as you are a Windows OS User:
py OpenAndShow.py
Fun fact: Of course Python won't search in the whole PC or directory (including folders which are in the directory) , so that's why you need to mention it is in a folder in the Giraffe folder!
Error 2 - Wrong location for the picture
If you had fixed error 1, you will get another error, that dany.jpg is not such file, why ? Because it is in another folder inside the folder your code is located, you can use dany.jpg when they are in the same folder, not in another folder in the same folder, in this case, it is in DirectoryAssign1 which is in Girrafe folder.
How to fix?
To fix it, change the path:
From : The current Path:
dany.jpg
To : The following path below:
DirectoryAssign1\dany.jpg
You are running the wrong file. In PyCharm, afaik you can right click on the file and run. Your PyCharm is now trying to run the file main.py.
Otherwise, you can use the terminal:
python OpenAndShow.py

Python import module give blank output

I have a file named task.py with the below code
def add(a,b):
c = a+b
return c
and another file named inputs.py with the below code
import task
a = task.add(2,2)
print(a)
When I run this code, it shows a black output with no errors. Both the files are present under the same directory.
Can anyone confirm why it does not show any outputs?
This worked after i had the Python file and Pycharm file in under C directory.
Python scripts must be in current working directory. You can check CWD using following code:
import os
os.getcwd()
The problem is specific to your system.
I have tried and it worked fine for me
Try the following :
Running again
delete pycache
run by pressing run button in your code editor
run by python inputs.py

Python can't run absolute script?

I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?
Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py
For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py

How can I execute commands through python in terminal MAC?

When I manually run this command in Terminal, it executes, but through Python it gives the error that the directory is not available in Python packages.
I am using the following command
source ~/trytry/shell.sh
This is my test shell file:
#!/bin/sh
echo hello
when I executed " source ~/test.sh ", it will print hello at console.
This is my python code:
>>> import commands
>>> commands.getstatusoutput("source ~/test.sh")
(0, 'hello')
It works without any problem. So, would you please show your code?
What it looks like to me is that you have a shell script, and not a python file which would have the .py extension instead of .sh. The error may have to do with the fact that it isn't a python file you're trying to run.

sublime text2 python error message /usr/bin/python: can't find '__main__' module in ''

I installed sublime text 2 to OSX 10.8.2.
In my Mac, python 2.7.3 is installed.
In sublime text2, I just type
print 'Hello'
but error occurred like below.
/usr/bin/python: can't find '__main__' module in ''
[Finished in 0.2s with exit code 1]
How can I fix it?
I got the same error as I didn't save the script before executing it. Check to see if you have saved it!
Note to anyone else:
If you have a directory like so, you can add a __main__.py file to tell the interpreter what to execute if you call the module directly.
my_module
|
| __init__.py
| my_cool_file.py # print "Hello World"
| __main__.py # import my_cool_file
$ python my_module # Hello World
You need to SAVE your code file with the ".py" extension. Then, on the 'Tools/Build System' menu, make sure your build system is set to either 'auto' or 'Python'. What that message is telling you is there is no valid Python file to 'build' (or, in this case just run).
First save your program file type as "example.py then run your code its working fine.
did you add the shebang to the top of the file?
#!/usr/bin/python
Don't run with a space between the directory and the filename:
python /root/Desktop/1 hello.py
Use a / instead:
python /root/Desktop/1/hello.py
The problem: The format of the file as to how it is saved.
Use a proper text editor and save it with the .py extension
and run it in terminal.
eg: file name should be saved as `example.py`
run
python example
Save the .py files before you build in sublime.Such as save the file on desktop or other document.
Make sure that you aren't clicking on "Run unnamed" from 'run' tab. You must click on "run ". Or just click the green shortcut button.
Edit the configuration and then in the box: Script path, select your .py file!
You get that error because you haven't saved your file, save it for example "holamundo.py" then run it Ctrl + B

Categories

Resources