I have a struggle using this editor. I tried it a year ago and after some hair-pulling, ended up leaving it for another platform. I came back today, and I want to fix my problem and go farther than just step one.
I got a picture of my code and will provide the code written out here as well. I will also be providing a picture of the book I am using (Python Crash Course 2nd Edition) with the instructions given to me.
If any more info is needed, or some clarification. Please let me know. I want to get moving past this obstacle.
Picture of my code
print("Hello Python World!")
[WinError 2] The system cannot find the file specified
[cmd: ['python3', '-u', 'C:\\Users\\2\\Desktop\\New folder\\Coding\\python\\python_work\\hello_world.py']]
[dir: C:\Users\2\Desktop\New folder\Coding\python\python_work]
[path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\2\Desktop\Python\Python310;C:\Users\2\Desktop\Python\Python310\Scripts;C:\Users\2\AppData\Local\Programs\Python\Python310\Scripts\;C:\Users\2\AppData\Local\Programs\Python\Python310\;C:\Users\2\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\JetBrains\PyCharm Community Edition 2021.3.1\bin;]
[Finished]
Picture of Directions from Book (pg 10)
{
"cmd": ["python3", "-u", "$file"],
}
print("Hello Python World!")
Hello Python World!
[Finished in 0.1s]
Once again, if any clarification or extra detail is needed, just let me know and I will clarify to the best of my ability.
Problem solved
Ended up switching the build system from Python3 to Python and removed the cmd directions from the book. Problem fixed. Big thanks!
The instructions in your book are either out of date or flat out misleading you; I would not recommend following them.
You only need to create your own sublime-build file to execute a Python program as Python version 3 if you're running Sublime Text 3 AND you are on Linux/MacOS.
The Python.sublime-build file that ships with ST3 will execute python.exe by default on Windows which (if you have Python installed and on the PATH) will work. The version of the file that ships with ST4 executes the py.exe helper on Windows, which is always available on the PATH and will find the right version of Python for you no matter where it's installed (so long as it is in fact installed).
Using the name python3 is required only on Linux and MacOS, for which the name python refers to Python 2 and not Python 3 (and it does not exist on Windows at all, which is the ultimate source of your problem). In ST3 the build always tries to execute python on these platforms, but in ST4 the default is now python3.
The build as outlined in the book doesn't set up an appropriate selector to allow the build to be auto selected or a file_regex to match errors to allow for error navigation. Perhaps later parts of the book include that; the built in build supports it directly out of the box.
In any case, the most expedient way to test out your code would be to select Python under Tools > Build Systems and then run the build again. Sublime is going to ask you if you want the Python or the Python - Syntax Check build; choose the first one in the list.
The Syntax Check variant only compiles your code but doesn't actually run it, which is almost never want you want.
If that works, you can delete your sublime-build file and change the selected build to Automatic, which will cause Sublime to pick the Python build for you automatically so long as you execute the build while you're editing a Python file.
Related
I am very new to python and I am trying to execute a python file called HelloWorld.Py. The code within the file editor is
#! Python3
print('Hello world!')
I am trying to do this from my windows command line. The correct path from what I can tell in my documents application is C:\Users\china\PythonFiles\HelloWorld.py.
As I am following a Udemy course, it is recommended that you type py.exe C:/Users/china/PythonFiles/HelloWorld.py (filled in with my example).
At first, I returned a syntax error for the use of py.exe. Reading online made me confused about what to put in front of C:, and after removing py.exe, and just typing C:/Users/china/PythonFiles/HelloWorld.py returns the error 'Unable to create process using 'Python3 "C:\Users\china\PythonFiles\HelloWorld.py" '
What am I doing wrong? I have researched online for a few hours and I am still no closer to figuring it out. Please help!
Remove the comment, or change it to #!python3. py.exe doesn't like the capitalization.
The comment isn't needed unless you have both Python 2 and Python 3 installed, and it hints to py.exe which Python to use, but requires lowercase python followed by the major and optionally minor version, e.g #!python3.6 or #!python3. If the comment is missing, the latest version of Python installed wiil be used.
I've just started with Python, I've installed Python and it's extension from Microsoft, and when I run Python file in terminal I get the results, but the ugly path is covering plenty of space in terminal (like on the photo below). I've found video how to remove that from debbuging console, but couldn't find anything related to normal terminal. Is there any solution for that?
I am using Windows 10 and newest version of VS Code.
https://i.stack.imgur.com/vjKOm.png
You need to do 3 things.
1.
You need to add Python to your PATH. Call this python2 if you want to use python 3 as well for other project.
(However if you are starting, considering using python 3 from the beginning)
2.
Just use the file name if you are already in the folder
Your command is reduced to this then:
python2 strings.py
3.
Use prompt to change the line before your command. See doc for more information: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/prompt
Excuse the awkward question wording.
I've made a script. I would like for others to download it from github, and run it by typing programName argument1 argument2, similar to any other popular app used through the terminal such as Jupyter or even opening Atom/Sublime/etc. (ex:jupyter notebook, atom .). However, unlike Jupyter or sublime, my script isn't launching another app, it's a small app meant to be used in the shell.
Currently, to use my script, one must type into the command line python programName.py arg1 etc from within the file's directory.
How do I allow others to dl it and use it from anywhere (not having to be within the directory), without having to type out the whole python programName.py part, and only having to type programName arg1?
This blog post explains step by step how to create a distribution that you can install and it would turn into an executable.
You can refer to this github repo for a sample application.
The full documentation of setuptools is available here.
In general, you should configure your setup.py in order to use the command in the entry-point option:
setup(
name = "your_app_name",
packages = ["package_name"],
entry_points = {
"console_scripts": ['cmd_name = package_name.package_name:main']
},
....
)
This solution would work on every OS where you have installed python.
Your script may need to have an interpreter, "shebang", besides being "reachable" by the $PATH
#!interpreter [optional-arg]
For example, you could have something like
#!/usr/bin/env python
or to force a specific version
#!/usr/local/bin/python2.7
Next, your script needs to be available within the $PATH, check this answer that covers that part: https://unix.stackexchange.com/q/29608/53084
You can simply add your script to PATH variable in order to launch it from anywhere.
In Linux distros, you can simply do it by using a bash command PATH=$PATH:/path/to/your/script.
Make sure you don't have the space around the "=" operator.
Now, the second thing is you don't want your script to be named as pythonProgram.py.You can simply remove the extension .py from PythonProgram.py by adding a single line to the starting of your script.
Open up your script and at the very begining type #!/usr/bin/python.This should be the first line of your code.This line is called shebang and is used to tell the bash which interpreter to be used for compiling the script.
If everything went right, you will be able to run your script as pythonProgram arg1.
In addition to mabe02: Python is a scripting language and usually not compiled, which means you will need an interpreter to run your program.
Programms made in C f.e. can be run on its own because they were compiled into machine code by a compiler.
An interpreter is similar to a compiler as it reads your script and interprets it at runntime. That is why you need to write python before your programm, to tell your computer to interpret your script on runntime, using python. This doesn't mean that there are no possibilities to compile python as can be seen in the other answer and in this link Can a python program be run on a computer without Python? What about C/C++? (py2exe and py2app).
I'm trying to switch from Notepad++ to Atom, but I just can't manage to get my scripts executed in Atom.
I followed this answer (so I already installed script) which is not really extensive and also the rest on the web doesn't offer anything comprehensible for beginners.
In Notepad++ NPPexec I used to
NPP_SAVE
cd "$(FULL_CURRENT_PATH)"
C:\Python34\python.exe -u "$(FULL_CURRENT_PATH)"
and in Sublime Text 2 I made it run by creating a new "Build System":
{
"cmd": ["C:\\python34\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Can you please guide me how to setup Atom to be able to execute Python scripts with Python 3.4 scripts with a keyboard short-cut?
I already tried to set my init-script to:
process.env.path = ["C:\Python34\python.exe",process.env.PATH].join(";")
respectively
process.env.path = ["C:\Python34",process.env.PATH].join(";")
with no success.
When I go to Packages -> Script -> Configure Script and type
C:\\Python34\\python.exe
it works. But thats not a permanent solution.
When I press Ctrl+Shift+B to run a script, without configuring it before (as it is supposed to work), I get (suggestion of ig0774's comment implemented):
(it doesn't matter whether it is C:\Python34 or C:\Python34\)
It complains that python is not in my path - but it is.
I read multiple times that Windows 7/8 64bit together with Python 3.x could cause issues with certain packages. May this be the reason in ths case as well? I have Windows 7 Pro x64.
Update
As I've switched to VSCode and probably stay there, I'm not willing/don't have the time to try out all the answers, so I let the community judge the answers and accept always the highest voted. Please ping me, if it's not correct anymore.
This can be easily solved by editing the /home/.atom/packages/script/lib/grammars.coffee file (note that the atom folder is hidden so you might have to press ctrl+H to view hidden files and folders)
Inside grammars.coffee find:
Python:
"Selection Based":
command: "python"
args: (context) -> ['-u', '-c', context.getCode()]
"File Based":
command: "python"
args: (context) -> ['-u', context.filepath]
and replace with:
Python:
"Selection Based":
command: "python3"
args: (context) -> ['-u', '-c', context.getCode()]
"File Based":
command: "python3"
args: (context) -> ['-u', context.filepath]
Save changes, restart Atom and enjoy running your scripts with python 3
EDIT: On Windows I believe the grammars.coffee file is located in
C:/Users/Your_Username/AppData/Local/atom/packages
Again, the AppData folder is hidden so you might have to change your settings to view hidden files and folders.
To expand on #matt-nona answer. You can go to his mentioned config file right from Atom. Simply go to settings then "Open Config Folder":
Then /packages/script/lib/grammars.coffee Find "Python" and make the appropriate change to python3:
Following up on Matt Nona's advice , when Atom starts-> Welcome Guide (or control+shift+T)-> 5th one down 'Hack on the Init Script'. A blank page will open and you can add that modifications in there.
Update: for any other souls looking for this answer - On my Mac I do not have a grammars.coffee file within atom script config file.
Instead, there s a grammars folder, and I have a python.coffee file in there. The same changes outlines in the screenshot (ie add '3' to the end of the two mentions of python) fixed my issue and atom automatically runs Python3 now.
Not sure if the above answers are Windows specific or if there have been dev changes since 2017.
same problem just like you. 'Packages -> Script -> Configure Script' is not permanent. So I has tryed another script runner:https://atom.io/packages/atom-runner, just found the problem is in the python script itself.
When I use atom-runner, I got error message like this:
atom-runner error
So it remind me that in the beginning of the python script: ' #!/usr/bin/env python3 '
It's obvious that the ENV_PATH is WRONG here. I should revise it in my python script.
Setting the PATH within Atom did not work, setting it with the cmd, via
set PATH=%PATH%;C:\Python34
neither, and setting it in the Windows 7 system properties failed as well.
However reinstalling Python 3.4 and check Add python.exe to Path
seems to be neccesary. Also I needed to uninstall Atom completely (inculding all packages or a least script) and reinstall it from scratch.
After all these steps:
Install Python with Add to Path
Install Atom
Install script package
it works out of the box (Ctrl+Shift+B) and no further steps are required.
I still don't know what was the reason before and I don't know which of this steps are really required. So feel free to include your procedure without reinstalling everything.
Update
Reinstalling everything is certainly not necessary, simply updating/repairing the installation with the installer is sufficient.
Use the script-runner https://atom.io/packages/script-runner/
"N.B. these keyboard shortcuts are currently being reviewed, input is welcome.
Command Mac OS X Linux/Windows
Run: Script ctrl-x alt-x
Run: Terminate ctrl-c alt-c"
And "Run Terminate" (Alt + c) to use the current python in your system.
edit your python.coffee script
$ sudo nano .atom/packages/script/lib/grammars/python.coffee
For Linux and Mac, adding environment in the script will pick correct python version. (command + I to run)
for running with python3
#!/usr/bin/env python3
First, thanks in advance for any assistance.
I'm diving into Python and would like to continue to use Sublime Text 3 as I do for other projects. As I understand it, the internal version of Python is 3.3, but I'd like to use 2.7.6. Following other examples, I created a new build system as follows:
{
"cmd": ["/usr/local/bin/python", "-u", "$file"],
"file_regex": '^[ ]*File \"(...*?)\', line ([0-9]*)",
"selector": "source.python"
}
When I switch to this new build, open the Console and then try to run the following command in the Python 2 syntax
print "Hello, world."
I get an "invalid syntax" error. However, if I try to run the same command in parentheses as required for Python 3
print("Hello, world.")
the command executes successfully. In other words, it does not appear that the Console is using the 2.7.6 build system.
What I find confusing is that if I save a new test.py file using the same Python 2 syntax as above, build it using the default Python 3 build system the Console outputs a successful execution of the print command – even though the syntax should not be compatible (as occurs in the Console). It seems I get different results running commands directly in the Console and running a build of file.
I'm sure this can be chalked up to a misunderstanding on my part, but I'd appreciate any thoughts.
Cheers.
So, one thing you may not be aware of is that
print("Hello World")
is valid in both Python 3 and later revisions of Python 2, including 2.7.6, so if you're running your file from the command line, it'll execute properly regardless of which interpreter you're using.
You may also be confusing yourself regarding the Console in Sublime and the build systems. The Console, opened by hitting Ctrl` or selecting View -> Show Console, is running Sublime's internal version of Python, a stripped-down Python 3.3.3 if you're using the latest build. You cannot run Py2 commands in the console, no matter how hard you try. It is useful for running Python API commands to control Sublime itself, but it's of no use in building non-plugin files.
Build Systems let you run your programs through external compilers, interpreters, processors, or what have you. They are activated by choosing one in the Tools -> Build System menu, and then hitting CtrlB (or CommandB on OS X) to run it.
So, to verify that your build systems are working as desired, create a new .py file with
print "Hello World"
in it. Save the file, and select Tools -> Build System -> Python (the first one in the menu, between Make and Ruby). Hit CtrlB or CommandB as appropriate for your OS, and check out the pane that opens at the bottom of Sublime. It should show "Hello World" at the top line, then another line saying [Finished in 0.05 seconds] or something similar underneath it.
Now, select Tools -> Build System -> Python 3 (or whatever you named your new build system) and hit Ctrl/CommandB, and you should now see a traceback in the build pane for invalid syntax.