How to run a.py file in python cmd? - python-2.7

I'm a newbie to python, so I just installed python27 on my win8 machine and set the path for C:\Python27 and C:\Python27\Scripts.
Now I want to execute a small .py file, so at the shell (python cmd) I type:
python "c:\python27\gtos.py"
File "<stdin>", line 1
python "c:\python27\gtos.py"
^
SyntaxError: invalid syntax
Any help is appreciated...
Thanks

Treat it like a module:
import file
This is good because it's secure, fast, and maintainable. Code gets reused as it's supposed to be done. Most Python libraries run using multiple methods stretched over lots of files. Highly recommended. Note that your import should not include the .py extension at the end.
Use the exec command:
execfile('file.py')
But this is likely to go wrong very often and is kind of hacky.
Spawn a shell process:
import subprocess
import sys
subprocess.check_call([sys.executable, 'file.py'])
Use when desperate.

Related

No Module named ESPN_Scraper.items

I am learning python and am trying to run the code on this github project.
https://github.com/rcfbanalysis/rcfbscraper
When I try to run a command such as the following
python "C:\Python\rcfbscraper-master\ESPN_Scraper\ESPN_Scraper\spiders\espnSpider.py"
I get the error No Module named ESPN_Scraper.items.
This is the offending line
from ESPN_Scraper.items import GameItem
From what I can tell is ESPN_Scraper items.py is not in your sys path. one quick work around for this would be to put the ESPN_Scraper directory in the same directory as your espnspider.py file.
Take a look at the docs https://docs.python.org/2/tutorial/modules.html section 6.1.2

Python Tkinter throwing Tcl error

I am learning basic GUI in Python, and I came across a sample example to read file name from file explorer on Stack Overflow.
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
This particular script is working fine when I am trying to run it in IDLE, but the same is not running if I am trying from command prompt in windows 7.
Python Version: 2.7. Here is the output error which I get.
>>> from Tkinter import Tk
>>> from tkFileDialog import askopenfilename
>>> Tk().withdraw()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1685, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
C:/Python27/lib/tcl8.5 D:/PyProj/lib/tcl8.5 D:/lib/tcl8.5 D:/PyProj/library D:/library D:/tcl8.5.2/library D:/tcl8.5.2/library
This probably means that Tcl wasn't installed properly
Any pointer to what I am missing here can be of great help.
You just need to copy two folders from tcl folder to the Lib folder
tcl8.5 and tk8.5
In case you are using Virtualenv on Windows I found a solution here: https://github.com/pypa/virtualenv/issues/93
I copied the "tcl" folder from C:\Python27\ over to the root of the new Virtualenv, Tkinter.Tk() shows a new window without throwing an exception.
I am running Python 2.7 on Windows 7.
Hit a similar problem after installing Activestate Python and TCL. I found the following page solved the problem for me: ActiveState Python install problem. The fix was to copy the contents of C:\Python27\tcl into C:\Python27\Lib.
Another potential solution (given by user i-shenl in a different ActiveState thread on the same issue) is to set the environment variable $TCL_LIBRARY to point to the tcl library folder ("C:/Python27/tcl", in the question). If you set this system-wide or account-wide (via System Properties), it will affect other programs that use a TCL Library (if any are installed). If you're using Powershell, you can set this variable in your profile to limit its affects to programs run from the shell.
I hit the same problem on Ubuntu 17.04 with virtualenvwrapper for 64 bit Python 2.7
I add tk and tcl library paths in local postactivate script
Go to your virtualenv: workon your-env-name
Edit local postactiave script with your favourite editor, for ex:
gedit $VIRTUAL_ENV/bin/postactivate
Locate tk and tcl library paths. In postactivate script, export TK_LIBRARY and TCL_LIBRARY with appropriate paths. Add this lines to your script with modified paths:
TK_LIBRARY=/home/kamil/anaconda2/pkgs/tk-8.5
TKPATH=/home/kamil/anaconda2/pkgs/tk-8.5
TCL_LIBRARY=/home/kamil/anaconda2/lib/tcl8.5
export TCL_LIBRARY TK_LIBRARY TKPATH
Restart your virtualenv: deactivate and workon your-env-name again.
If you are hitting this kind of error in a python -m venv NAME kind of virtual environment (and you actually have tcl installed in your system), then you need to export the paths similarly as suggested by Kamil Czerski in a previous post for virtualenv.
To find out what are your TK and TCL paths, run a python script outside of the venv (source):
import tkinter
root = tkinter.Tk()
print(root.tk.exprstring('$tcl_library'))
print(root.tk.exprstring('$tk_library'))
Open your venv configuration file bin/activate and find the place where they export PATH and insert after this (insert correct paths from step 1):
TCL_LIBRARY="/tcl/path/from/step/1"
TK_LIBRARY="/tk/path/from/step/1"
TKPATH="/tk/path/from/step/1"
export TCL_LIBRARY TK_LIBRARY TKPATH
Deactivate (if it was activated) and source again your venv:
deactivate
source bin/activate
The "Tcl missing"-error should be gone.
IDLE is probably setting the path required for TCL. To find out what path is being used by IDLE, compare the output of sys.path from IDLE and without IDLE. Then you can add the location of init.tcl either using an environment variable or programatically. See Xenomorph suggestion.
All you need to do is copy tcl 8.6 and tcl 8.5 from tcl file to Lib file on in python.
Python-tcl-tcl8.5 to Python-Lib
Go to directory in which all of your python dependencies are stored
Example:
Python37
-DLLs
-Doc
-etc
-include
-Lib
-libs
-Scripts
-tcl
-python.exe
Go to tcl folder, copy the tcl8.5 and tk8.5 folder
Paste these folders in the Lib folder
This solution works for Windows 10 users

No module named kivy.app

So I thought I'd toy around and try and learn Kivy, as it looks interesting. I have just started trying to get one of their examples working:
from kivy.app import App
from kivy.uix.widget import Widget
class MyPaintWidget(Widget):
pass
class MyPaintApp(App):
def build(self):
return MyPaintWidget()
if __name__ == '__main__':
MyPaintApp().run()
I get the following error:
C:\Kivy-1.8.0-py2.7-win32>python paint.py
Traceback (most recent call last):
File "paint.py", line 1, in <module>
from kivy.app import App
ImportError: No module named kivy.app
I have installed the latest version of Kivy. I see "app.py in the C:\Kivy-1.8.0-py2.7-win32\kivy\kivy folder.
Also, here is my PYTHONPATH:
>>> import sys
>>> for n in sys.path:
... print n
...
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\setuptools-2.0.1-py2.7.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\pywin32-218-py2.7-win32.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\plyer-1.1.2-py2.7.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\kivy_garden-0.1.1-py2.7.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages\requests-2.2.1-py2.7.egg
C:\Kivy-1.8.0-py2.7-win32\Python27\python27.zip
C:\Kivy-1.8.0-py2.7-win32\Python27\DLLs
C:\Kivy-1.8.0-py2.7-win32\Python27\lib
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\plat-win
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\lib-tk
C:\Kivy-1.8.0-py2.7-win32\Python27
C:\Kivy-1.8.0-py2.7-win32\Python27\lib\site-packages
Any help would be greatly appreciated. Thank you.
I ran into this error message when I named the script kivy.py, because python looks first in the current directory to fill the dependency, so it never sees the real kivy package.
Renaming the script fixed it for me.
The name of the folder or file cannot be kivy or kivy.py. You need to rename it to another name and it will run ok.
So, I figured it out....I was not being very smart. I'm new to Kivy and I'll answer this for anyone else that is as green as I am. You can't just run this as a python program, doh.
Follow instructions [here] (http://kivy.org/docs/installation/installation-windows.html#start-a-kivy-application) and all will be right with the world.
If you installed kivy using pip you need to add the packages to your environment. In Windows Command Prompt do
pip show kivy
~Take note of the Location of the module.
Next you can do 1 of two things.
Configure any python files you write to refer to the location of your module with sys, use this tutorial for more info: https://kivy.org/docs/guide/environment.html
Add the location you found above to your PATH (Windows): https://www.computerhope.com/issues/ch000549.htm
2 Worked for me.
I was also facing the same issue while running sample app.
I followed simple steps given in : https://kivy.org/doc/stable/gettingstarted/installation.html#install-pip
It worked for me.
Create the virtual environment
Activate the virtual environment.
Install Kivy
run your .py file
I know I am too late but I hope this will help others, I just did not run it from the command prompt, I opened the python code in my python shell and then pressed run 'F5' and it worked for me.
If you're using a virtual environment (venv), make sure you're targetting the the correct Python interpreter. See here for how to to change to the venv interpreter in IntelliJ

No module named os.path : wrong Python being called by bash

OS: CentOS 6.6
Python 2.7
So, I've (re)installed Canopy after it suddenly stopped working after an abrupt shutdown. It worked fine immediately after the install (I installed as my default Python). But after one reboot, when I try to open it with /root/Canopy/canopy (the icon under applications no longer works, either), I get the following error:
(Canopy 64bit) [xxuser#xxlinux ~]$ /root/Canopy/canopy Traceback (most recent call last): File "/home/xxuser/qiime_software/sphinx-1.0.4-release/lib/python2.7/site-packages/site.py", line 73, in <module>
__boot() File "/home/xxuser/qiime_software/sphinx-1.0.4-release/lib/python2.7/site-packages/site.py", line 2, in __boot
import sys, imp, os, os.path ImportError: No module named path
I found this link: Python - os.path doesn't exist: AttributeError: 'module' object has no attribute 'path', but both of my os.py and os.pyc were 250 and 700 bytes, respectively. There was another file called site.py which was 0 bytes and site.pyc was about 100 bytes. What are these files? And would deleting them hurt anything (which is what they did)? And why is this happening after reboot? (using reboot command).
I also found this: https://groups.google.com/forum/#!topic/spyderlib/hKB15JYyLqM , which could be relevant. I've updated my python path before with sys.path.append('/..')
My guess is that for some reason os.path isn't in sys.path? and __boot can't find it? But I'm new to Python and Linux and want to know what I'm doing before I go modifying any boot files, paths, etc.
Thanks in advance.
More information (saw that I'm supposed to update new info in an edit to original question. New to this.)
From one of the comments:
This is what I got:
import os.path
import posixpath
os.path
module 'posixpath' from '/home/xxuser/qiime_software/python-2.7.3-release/lib/python2.7/posixpath.pyc'
posixpath
module 'posixpath' from '/home/xxuser/qiime_software/python-2.7.3-release/lib/python2.7/posixpath.pyc'
Looks like os.path is there.
Could this have to do with a permissions error? I have it installed to /root/Canopy/canopy and I found this: docs.python.org/2/library/os.html#module-os (section 15.1.4). Does that make sense?
I'm also not sure if the following is related, but it might possibly. I can no longer seem to update my path with sys.path.append('/file/path/here'). It works until I close the terminal, then I have to re-append the next time I want to call a module from the new directory. Are sys.path and os.path related in any way?
Just fixed this on OSX with:
brew uninstall python
brew install python
No idea why, never seen it in 5 years of working with Python :S
It turns out that I was onto something with my last comment.
I'd downloaded a bunch of biology modules that depend on python, and so many of them came with their own install. When I added the modules to ~/.bashrc, my bash began calling them in advance of my original CentOS install. Resetting ~/.bashrc and restarting (for some reason source ~/.bashrc didn't work) eliminated all of the extra stuff I'd added to my $PATH and Canopy began working again. I'm going to go through and remove the extra installations of python and hopefully the issue will be behind me. Thanks to everyone who posted answers, especially A.J., because that's what got me thinking about .bashrc .
Edit: With some better understanding, this was all because of using python in a virtual environment. Canopy was resetting my path every time I opened it. I'm using a self-installed virtual environment now and have configured my path.
Try seeing if you have posixpath by typing import posixpath:
>>> import os.path
>>> os.path
<module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> ^D
bash-3.2$ python
>>> import posixpath
>>> posixpath
<module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>

Python 2.7 : Import a module situated in a subdirectory from a different subdirectory

Here's my code configuration:
__init__py
./module_1/foo.py
./module_1/__init__.py
./module_2/bar.py
./module_2/__init__.py
My goal is to be able to import the foo module from bar.py, but I'm getting quite confused on how to do it.
Something such as:
from ..module_1.foo import *
Will raise the following error:
ValueError: Attempted relative import in non-package
EDIT:
Ideally I'd like to be able to run my script in the following fashion:
python ./module1/foo.py
You haven't shown how you are invoking the script, but you need to ensure that your scripts are actually packages in your python path. That's basically what the error message is telling you, you were trying to import a "non-package". You probably don't have your top-level in the python path. For example ...
If your top-level module is called app and your configuration is
<path-to-app>/app/__init__py
<path-to-app>/app/module_1/foo.py
<path-to-app>/app/module_1/__init__.py
<path-to-app>/app/module_2/bar.py
<path-to-app>/app/module_2/__init__.py
You can run your script as follows.
cd <path-to-app>
PYTHONPATH=$PWD python -m app.module_2.bar
Works for me.