Python run file from command line - python

Yesterday I installed Python (with Numpy & Scipy) on CentOS, everthing works fine when I'm using the CLI and do some math. But now I try to execute a file, and then a weird error appears, searched all over the place but can't find the solution to fix this.
I'm running two Python versions, the version I'm using is 3.4, and I installed it at: /usr/local/bin
Then I made a file called test.py in the same directory, with this code:
import numpy
When I try to run it with:
./python3.4 -m test.py
I get this error:
/usr/local/bin/python3.4: Error while finding spec for 'test.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__')
I hope somebody can lead me into the right direction, thanks in advance!

Running things like
python -m <something>
invokes Python, which then tries to import a Python Module.
From your short example it doesn't appear to be a valid Python module
Try running with python <your python code file> instead.

Related

pyDEA running as a python script

I am attempting to run pyDEA. The main_gui approach does not work from wha appears to be the
ValueError: cannot use LOCALE flag with a str pattern
issue with the latest version of Python (I am running Python 3.8.5, Anaconda on MacOS).
Has anyone run it directly in a script? If so, do they have any sample code to show what they needed to import and call to run the DEA analysis?

Unable to import Tkinter

I am using Git Bash to run some code involving Tkinter, but have been unable to run the code and have gotten the error ModuleNotFoundError: No module named 'Tkinter'. I have #!/usr/bin/env/python3 as the first line of my code, but this does not seem to help. When i type import Tkinter and import tkinter into my bash line, it returns with bash: import: command not found. When I attempt to use sudo, it responds with bash: sudo: command not found. I am not sure what to do at this point, as I have already reinstalled both git bash and python and neither seem to help.
Python's import statement is case sensitive, so it just might work if you write import tkinter instead of import Tkinter.
The reason some some tutorials use the latter is because that's how it used to be in python 2. It wasn't considered very pythonic however, since all package and module names should be lowercase, so it was changed in python 3.
Regarding the first error message joelhed's anwser should solve your problem.
You receive the second error message because you
can't directly run python commands in the bash line.
To run python commands in the command line run python first.

A python module is found normaly when the python script runs from terminal but its not found when the same script is called in a bash script.Why?

(newbee here,i couldnt find something similar)So, I have a python script that sents an email with attachments to some recipients and works fine when I run it straight from the terminal. But when I call this script in a bash script I get an error that the Module below is not found. Why is that? (on a Mac if that matters)
ModuleNotFoundError: No module named 'email.MIMEMultipart'
So on my mac I had two versions of python:
Python 3.6 lives in:
/Users/name/anaconda/bin/python
While Python 2.7 lives in:
/usr/bin/python
My python_script.py , that was running fine when it was called on its own in the terminal, was calling Python 2.7 and had the following shebang/firstline:
#!/usr/bin/python
In my bash script, the line that was calling python_script.py was a follows:
python /directory/directory/python_script.py
The code was not working on this implimentation because when you type python in the terminal it acually starts Python 3.6 instead of Python 2.7 that my python_script.py was using (I had the wrong idea that the default system Python would be Python 2.7 that my mac came with). The solution (as per Stack suggestion above) was to cd in directory I had my python_script.py and then run it from there by just typing:
./python_script.py
Hope this helps someone, someday.
(I should have probably deleted this post because I keep getting downvotes for some reasion but i will leave it for other newbees like me)

Python importing module goes wrong

If I import a module in a python (2.7.13) script e.g
from winappdbg import breakpoint
and execute it with the IDLE Python Shell,
I get an error:
ImportError: cannot import name breakpoint
If I run from the commandline:
>> python
>> from winappdbg import breakpoint
everything is fine.
Same problem if I run the script via Commandline:
>> python C:\path\to\script.py
results in the same error
The Python Version displayed by the IDLE Shell is exactly the same like the one displayed by the Commandline Command.
I checked the PYTHONPATH and also tried to add the script manually to the PYTHONPATH with no effect.
I also tried different Python Versions - still the same problem.
I do not know how to continue - so maybe someone have an idea or got the same problem?

Can't get Python IDLE to recognize OGR/GDAL Module

Folks,
Just getting started using OGR and Python for a variety of geospatial tasks. I'm working outside of OSGEO4w, and have installed GDAL w/ Python Bindings as well as Python v. 2.7.8 on my machine.
That said, I can run python and import gdal from a command-line interface but am unable to import the module when I use the IDLE environment. It returns an error telling me that the module doesn't exist. My install must be sound given that it works in the cmd prompt, so what's the deal?
This is all new to me, I'd appreciate any help or advice ya'll can impart; all previous questions I've read were concerned w/ merely installing (which I've done successfully) and getting Python to recognize the module from the command prompt, which it does.
Cheers,
Mike
Mike,
You can open IDLE with your python installation from the cmd line through:
>>>from idlelib import PyShell
>>>PyShell.main()
This should open IDLE from your current python, and you should be able then to import gdal and ogr from there.
Alternatively, you should have a windows batch file here
C:\YOURPYTHONPATH\Lib\idlelib\idle.bat
Running this should achieve the same.
Martin
Consider creating the following batch where PyInst references the Python installation folder from which IDLE is launched (i.e. idle.bat) and QgisInst references the folder containing "bin\o4w_env.bat":
#echo off
set PyInst=C:\Python27
set QgisInst="C:\Program Files\QGIS Brighton"
call %QgisInst%\bin\o4w_env.bat
set PYTHONPATH=%PyInst%\DLLs;%PyInst%\Lib;%PyInst%\Lib\lib-tk
set TCL_LIBRARY=%PyInst%\tcl\tcl8.5
python %PyInst%\Lib\idlelib\idle.pyw

Categories