I am trying to run Python from VS Code. I have already activated python through the terminal. However, in the terminal selector in the lower right of the screen, I cannot find Python terminal option there:
I wish to ask how could I make Python terminal visible here so that the next time I wish to switch terminal to Python, I do not have to load a .py file but could just selecting Python terminal directly. Thank you.
Python terminals aren't really a thing. If you want to run a .py file, do $ python [filename].py or $ python3 [filename].py. If you want to just run commands, open a Python shell by doing $ python or $ python3
There is no Python terminal. If you are asking about loading Python shell you just have to type python into your terminal to load the Python shell
Related
This command runs under bash on Linux:
python file.py variables
But when I write it to the IPython console in Spyder I get:
SyntaxError: invalid syntax
Q: How can I run a python script using the IPython console in Spyder?
(Spyder maintainer here) To run a Python file in Spyder, you just need to open it in its editor and the go to the menu
Run > Run file
or press F5. That basically reads the contents of the file and executes it with exec (as it was suggested in the answer by Jeremy Hue).
If you want to pass arguments to your script, please see my answer for that here.
Your IPython console is already running Python, whereas the command python file.py in bash is basically saying 'run file.py using Python'.
Check out this solution if you want to run file.py explicitly via the IPython console
run program in Python shell
I've scrolled the internet all over and theres no simple, clean explanation for this. How can I open the Python shell, but inside a terminal window such as CMD? I have an application for it, but want to run it inside a terminal, so I can practice Python code inside my IDE like VS code.
If you have a reasonably new version of Python you need to run py.exe (or just py) instead of python.exe, because the real python executable (python.exe) is not in your path environment variable by default, whereas py.exe is in the /windows directory and thus automatically in path.
So to run your script type py yourscript.py from the directory of your script. If you just run py it will start the interactive interpreter.
Look e.g. at https://www.python-course.eu/python3_execute_script.php
Under the title Start a Python script (almost at the beginning)
there is an instruction. Just write from the command prompt:
python,
a space,
and the filename containing your script, with .py extension.
As in each command, press Enter and your script will be executed.
Of course, I assume that you have installed Python on your computer.
If this is not the case, start from installing it.
I have quite a new Python version installed (3.6.5) and start Python
writing just python. Writing just py also does the job, but (IMHO)
it is not necessary.
If you want to start Python interpreter and play around with some
Python commands, write just python and Enter. The interpreter will
start and display its own command prompt (>>>).
I am trying to run a python program called compare.py with the linux nohup command which keeps the program running until it is done without interruption. my python program has packages which can only run on python 2.7 and when i use nohup command program is run in python 2.6. how do i change the version of python when using nohup?
Example: nohup python compare.py $
I tried doing:
alias python=python2.7
before starting program and version of python isn't switched. how do i switch the version of python to 2.7 when i run nohup?
The easiest way would be to use a shebang line to specify the interpretter. At the start of your Python file, put something like
#!/usr/bin/python2.7
# This should be a path to an interpreter that you know for sure is Python 2.7
Then, use chmod +x file.py to make the Python file itself executable, and omit the python part of your nohup command, eg. nohup ./compare.py.
I had the same issue with anaconda python. While using nohup python, it was using python 2.7 but generic python command in terminal was taking me to 3.6.
nohup ~/anaconda3/bin/python scriptname.py
Providing full path to python after nohup command will solve the issue
Really frustrated with this as one thing works at one time. Sometimes import filename.py works. But in the tutorials all I see is python filename.py. But when I try to give that, I am facing an error like invalid syntax.
I have edited all the environment variables and I have C:\Python27 folder in the same location. To be able to run the files using python filename.py what are the conditions that must be met? What should be the current working directory? Should the .py files be there in the same working directory?
import name is a python keyword for use within a python script and/or the python interactive interpreter (repl).
python filename.py is a shell command for use at a command prompt or within a shell script to run the python interpreter on a given python script file.
The working directory does not matter other than for whether the file listed in python filename.py can be found.
So for python filename.py to work you must be in the same directory as filename.py but you could just as readily use python c:\users\user\some\other\path\filename.py in which case the current directory isn't used to find the file.
If you get python syntax errors from attempting to run python on a python file that's a python error in the code and you will need to look at the python file to see what the error is.
Just to be clear, typing python filename.py only works from the Terminal (i.e. cmd.exe, Windows PowerShell, the "Terminal" application on a Linux kernel, etc.), not from the Python interpreter (i.e. python.exe), and only works if you have used the cd command to change into the directory in which the file is saved (so that the terminal knows where to look for filename.py). import filename can be used from the Python interpreter, but is not the ideal method as it creates a compiled version of filename.py and can only be used once (you would have to restart the interpreter to do so again). I'm not sure whether this works in the official Python distribution available from the Python website, but at least in the Anaconda distribution, you can run a file from the Python interpreter using runfile("C:/Users/CurrentUser/Subfolder/filename.py").
I recently installed Python 2.7.6 for 64 bit Windows7. I am trying to run few commands like cd, pwd etc and it is not running either on Windows Command Prompt, or Python Commnad Line. and i am getting the follwoing error:
>>cd Desktop
File "<stdin>", line 1
cd Desktop
^
I am also not able to execute any .py codes
(I cannot post image as i dont have enough reputations)
The Python interpreter doesn't accept shell/batch commands, but only valid python code. What you are trying to do in your example would translate like this in python:
import os
os.chdir("Desktop")
What you did is start the Python interpreter in interactive mode (or maybe did you start IDLE). To run a python script saved in a file from the command line, try
C:\Python33\python.exe your_script.py
(change the path to python.exe according to your installation). If you installed Python fully, you should be able to launch scripts by double clicking on .py files.
If you use IDLE, see the official documentation. You can open a script from the menu and run it.
(Note that pwd is not a valid command for the Windows shell either (use cd without argument instead.)