I'm currently trying to install a package on python that can only be installed on Python 3. I have both 3.6 and 2.7. I'm on a Windows machine. Whenever I type "python" into a newly opened command prompt it returns python 2.7. Then whenever I type "python" it says
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
I believe python 3.6 is set as an environment variable on my path. Can someone offer some advice on how to switch these over? I've read py.exe from python 3's installion should switch between python 2 and 3, but I do not see how I am supposed to run that command other than clicking on it in my File Explorer and that does nothing.
You type python in python repl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
You should open a new command prompt or type ctrl+Z or quit() in the python repl
For switching python 2 and 3
Use
py -3
py -2
Once you type in python, you go into Python's interpreter mode, where you can type Python code and get the result. You can type quit to leave that mode. If you want to run a script, you need to run, instead of just python, python filename.py, with the appropriate filename.
But you want to do that outside of the interpreter mode (otherwise known as REPL).
Note that the above will probably cause Python 2.X to be used to run your script, so if you want to run Python 3.X you will want to include this at the top of your script
#!/usr/bin/env python3
and then just run it from a newly opened command prompt (or any command prompt that is not in Python's interpreter mode) like filename.py.
See this question for more information.
Related
I have made a tester file to check my other files are implementing correctly, however I can only get an output by clicking run on the file (top right green arrow). When I type 'python Test.py' in the terminal I get :
Fatal Python error: initsite: Failed to import the site module
ModuleNotFoundError: No module named 'site'
Current thread 0x0000000102c0ce00 (most recent call first):
I assume this is an environment issue for my terminal? I am in the correct folder (one above Test.py).
Any ideas how I can change this?
Thanks!
It looks like the python interpreter you are taking when you run python Test.py in the terminal is different from the one when you click the Run Python File in Terminal(green triangle) button.
Could you type this command in the VSCode terminal(CMD: where python; Powershell: get-command python) to check which python you are using when you take python Test.py command to run your python file? And compare it with another one.
I am new to Python and currently doing a basic python course to learn. I have been running code all day via the Command Prompt and it has been working fine. For some reason though it has stopped working and python files I try to run are returning the following error:
Traceback (most recent call last):
File "", line 1, in
NameError: name 'hello' is not defined
As per the screen shot and the numbers on it, I performed the following steps in an attempt to run the file:
change to the folder where file is saved
run 'dir' to list all files. I am trying to run 'hello.py'. This contains the code: print('hello simon!')
I can run the file by just typing hello.py into the command prompt, this works ok
I can also run with: python hello.py - this works ok
when I activate Python by typing Python --> Enter, this starts the interpreter ok. However if I then try to run by typing hello.py I get the error message.
This has worked ok all day, I have not changed anything on my PC (to the best of my knowledge!) but just started to get this error a few hours ago. I have looked all over the internet for solution but found nothing. I have uninstalled and re-installed Python, restarted etc... all to no avail.
I am running Python 3.6.5 on a Windows 7 64 bit PC.
It won't let me attach a picture so here is link to screenshot of Command Prompt and error: https://i.stack.imgur.com/BBUe5.jpg
I hope someone can help me with this please
Thankyou
You are not supposed to execute hello.py in the Python Interpreter. It won't work. When you type in python and hit Enter in your Command Prompt, Just type this,
>>> print('hello simon!')
And hit Enter, it would definitely work. Because the interpreter is supposed to execute a code line by line. So if you want to run a Python Script then do not execute it in the Interpreter.
The problem is that when you write python (alone), the command line calls python shell and the following commands are run inside the python shell and not in the command line anymore. Calling a script from the shell has a different format (look it up). You can enter exit() to exit the shell back to command line again
What you are trying to achieve is you are running Hello.py inside Python.
Not with Python.
You need to run Hello.py with Python. As python is interpreter over here.
>>>python
means you are going inside python shell
>>>print('hello simon!')
Is equivalent to your program.
You are running your Python Script as you should and it's working. If you added Python to your path you can run Script you only need to call the Script "hello.py". If you have more than one intepreter, or you didn't added it to your path then you can call it like this "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" you can enven pass arguments to it "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" --argument
When you type python in a shell, then interactive mode is activated. This is like a shell where you type commands and got interpreted right away, in the same way as cmd and powershell works, but for python. This way you can test snippets, or just do simple stuff overly complicated like this
import os
ls = os.listdir(os.path.abspath('c:/'))
def print_dir():
for file in ls:
print(file)
Wich in cmd would be dir c:\ or in powershell ls c:\. The point is that you can test libraries, explore objects, replace the shell or just have fun.
I am having trouble using the command line. I have a script test.py (which only contains print("Hello.")), and it is located in the map C:\Python27. In my system variables, I have specified python to be C:\Python27 (I have other versions of Python installed on my computer as well).
I thought this should be enough to run python test.py in the command line, but when I do so I get this:
File "<stdin>", line 1
python test.py
^
SyntaxError: invalid syntax
Looks like your problem is that you are trying to run python test.py from within the Python interpreter, which is why you're seeing that traceback.
Make sure you're out of the interpreter, then run the python test.py command from bash or command prompt or whatever.
Don't type python test.py from inside the Python interpreter. Type it at the command prompt, like so:
You can simply type exit() in the Python terminal to exit the Python interpreter. Then when you run the code, there will be no more errors.
I faced a similar problem, on my Windows computer, please do check that you have set the Environment Variables correctly.
To check that Environment variable is set correctly:
Open cmd.exe
Type Python and press return
(a) If it outputs the version of python then the environment variables are set correctly.
(b) If it outputs "no such program or file name" then your
environment variable are not set correctly.
To set environment variable:
goto Computer-> System Properties-> Advanced System Settings -> Set Environment Variables
Goto path in the system variables; append ;C:\Python27 in the end.
If you have correct variables already set; then you are calling the file inside the python interpreter.
In order to run scripts, you should write the "python test.py" command in the command prompt, and not within the python shell. also, the test.py file should be at the path you run from in the cli.
Running from the command line means running from the terminal or DOS shell. You are running it from Python itself.
Come out of the "python interpreter."
Check out your PATH variable c:\python27
cd and your file location.
3.Now type Python yourfilename.py.
I hope this should work
I am new to python.
I am trying to run my first script... I think this is what is called.
I have following in a python doc called "intro.py":
print('hello world')
This is saved under my Desktop (running Windows).
When I go to cmd, I type:
>>> Desktop/intro.py
When I do this I get a response that says:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Desktop' is not defined
Can someone please explain to me my issue? Thanks
Windows shell refers to what you see, when you hit windows-button + r and then type cmd and hit enter.
To start your script: hit windows-button + r and then type cmd and hit enter
After that navigate in the command line to your desktop using cd and then type python init.py to execute your script.
It seems to me like you were already in the python shell, when you tried to start your script.
To see the difference open the command line like before: hit windows-button + r and then type cmd and hit enter, remember how your terminal looks like now, then type in python and note the three >>> at the beginning of the line. This is then called the Python shell.
You tried to execute windows commands in this python shell, which obviously does not work. However you could also theoretically write your script in this python shell. But this is really awkward.
I suggest you to use an IDE. If I'm not wrong IDLE comes with the python 3 interpreter already or I can recommend my personal favorite PyCharm.
I work in PyDev and quite suddenly, I cannot run my python programs from within Eclipse's PyDev (version 2.1.0) anymore.
Any python program I have ran through Run As > Python Run fails wioth
Fatal Python error: Py_Initialize: can't initialize sys standard streams
Traceback (most recent call last):
File "C:\Python32\lib\io.py", line 60, in <module>
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I can still run my program in command Line, only Eclipse * I have only one version of Python (3.2)
I haven't changed my python files since last time they worked. They are encoded UTF-8.
I haven't upgraded Eclipse nor PyDev since last time they worked
Python is installed in c:\python32 (defined in the environment variable PYTHONPATH)
My XP system has been updated today for KB2536276 and
Any idea?
I have finally found out where the problem came from: I had a file called stat.py and this is apparently creating a conflict with Lib/stat.py
Unfortunately the error message was very obscure.
And I just don't understand why pyDev behaves differently than python.exe