Running a tester file in the VSC terminal - ModuleNotFoundError - python

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.

Related

python library loads and works properly from command line, but 'ImportError: No module' if I try to run at boot

I’m trying to run a couple of python programs at boot time (in connection with some halloween costumes I’ve been making with my kids; I don’t want somebody to have to log in remotely to start the software in their costumes if they have to reboot for some reason).
The software works perfectly if run from the command line, whether launched from its own directory or any other. However if I try to run at launch by putting the script in rc.local, nothing happens and I log the following error:
Traceback (most recent call last):
File "/home/pi/sound.py", line 4, in <module>
from pad4pi import rpi_gpio
ImportError: No module named pad4pi
Additional information:
Some additional color: which python returns '/usr/bin/python’ and I have fully specified in rc.local that this is the python to use (/usr/bin/python /home/pi/sound.py &)
I installed with pip install pad4pi, and checking a moment ago it confirms that
pad4pi in ./.local/lib/python2.7/site-packages (1.1.5)
Why would the pad4pi library (which is needed in both projects I’m working on) be available from the command line but not at launch? And more importantly, what can I do about it?
[Note, solution below. I will accept the answer when stackOverflow lets me.]
The library was not appearing in $Pythonpath at boot time.
I added
export PYTHONPATH=$PYTHONPATH:/home/pi/.local/lib/python2.7/site-packages to my rc.local file, before it ran my python code, and all was well.

Can't run Python file via Command Prompt, File is not defined error

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.

Running Python Programs from a Terminal

I am working on page 16, chapter 1, in the book "Python Crash Course" by Eric Matthes. I am trying to run my hello_world.py python program from my Windows terminal.
The directions say to:
Open a new terminal window and issue the following commands to run hello_world.py:
C:\> **cd Desktop\python_work**
C:\Desktop\python_work> **dir**
hello_world.py
C:\Desktop\python_work> **python hello_world.py**
Hello Python world!
python_work is my python work folder on my desktop. In my Windows terminal, I have tried entering the code with and without the unbolded C:\>. It has given me an error message: The system cannot find the path specified. I'm probably doing something wrong, but I don't know what's wrong.
Assuming your getting your error when you execute "python hello_world.py", Python might not be installed correctly. More likely is that you are not in the correct directory. Do the following commands:
cd "C:\Desktop\python_work"
python hello_world.py
If that does not work, I can only assume one of two things:
Either the folder or the .py file is not named what you think it is and you should rename whichever one, or python is not installed correctly and you should reinstall it.
use
cd C:\Desktop\python_work
to change your working directory and then type
hello_world.py
to launch your program or if that doesn't work try
python hello_world.py
alternatively (what I do), assuming the first option worked, you can create a .bat file with the following code.
#echo off
"%~dp0\hello_world.py" %*
pause
and then just put that bat file in the same folder as "hello_world.py" and when you run the bat file it will launch the script. :)

How can I execute python scripts from another directory

I am trying to follow a bad instructions website and because of that i'm getting troubles in executing scripts.
The website URL:
http://www.tripleoxygen.net/wp/2014/01/sagemcom-modem-fst-2764-gv-power-box-gvt-desbloqueio/
The following commands is the ones i'm getting trouble with:
./unlocker.py --mode=install
The steps i'm doing:
Open CMD
D:(my flash drive directory)
python
unlocker.py --mode=install
What I get:
File "<stdin>", line 1
SyntaxError: can't assign to operator
but I don't think i'm executing the script, because there is no stdin in the file...
So what I'm asking is:
Are my steps wrong?
How can I execute the script correctly from another directory?
Thanks in advance.
That website doesn't say to start python then enter unlocker.py. It says to type ./unlocker.py from the command prompt, not the Python prompt. You may need to do python unlocker.py, but again, you're doing that from the command prompt.

PyDev: Fatal error Python32\lib\io.py, line 60, in <module>

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

Categories