I have written a python27 module and installed it using python setup.py install.
Part of that module has a script which I put in my bin folder within the module before I installed it. I think the module has installed properly and works (has been added to site-packages and scripts). I have built a simple script "test.py" that just runs functions and the script from the module. The functions work fine (the expected output prints to the console) but the script does not.
I tried from [module_name] import [script_name] in test.py which did not work.
How do I run a script within the bin of a module from the command line?
Are you using distutils or setuptools?
I tested right now, and if it's distutils, it's enough to have
scripts=['bin/script_name']
in your setup() call
If instead you're using setuptools you can avoid to have a script inside bin/ altogether and define your entry point by adding
entry_points={'console_scripts': ['script_name = module_name:main']}
inside your setup() call (assuming you have a main function inside module_name)
are you sure that the bin/script_name is marked as executable?
what is the exact error you get when trying to run the script? what are the contents of your setup.py?
Please check your installed module for using condition to checking state of global variable __name__.
I mean:
if __name__ == "__main__":
Global variable __name__ changing to "__main__" string in case, then you starting script manually from command line (e.g. python sample.py).
If you using this condition, and put all your code under this, it will be be work when you will try to import your installed module from another script.
For example (code from module will not run, when you will import it):
testImport.py:
import sample
...another code here...
sample.py:
if __name__ == "__main__":
print "You will never see this message, except of case, when you start this module manually from command line"
Related
I am running some python scripts which pertains with a django application.
So I manually made a folder (named scripts) that contains some python scripts but is not made using ./manage.py startapp scripts. Along with this folder there is a modelsapp django app (which contains my models) and then project folder containing settings.py and urls.py.
I run ./manage.py shell < scripts/script.py to run my script.
Now here is a sample code of my scripts.py
from modelsapp.models import *
print(Room.objects.all())
This works pretty well. Now consider a second case. I now just run ./manage.py shell and then put the following commands-
>>>from modelsapp.models import *
>>>def init():
... print(Room.objects.all())
...
>>>init()
This also works pretty well. But now when I run the above code through the file in the first case, it says NameError: name 'Room' is not defined
It looks like the model classes which I just imported aren't accessibly inside a function.
This behavior is a bit weird.
Please help. In case I missed anything to mention, do comment below.
Edit: Since this is a bit weird behavior and am not able to find any answers to it, I am just going to pass the class Room directly to the function init() as a parameter to get the work done as of now.
after you run the django shell, you can run your script with:
>>>execfile('filename.py')
if you want to run it in a normal python shell (not using python manage.py shell) you have to do:
import django
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project(name.settings")
django.setup()
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
I am new to twisted and I have a twisted unit test in python, and I want to debug in pycharm with trial.
I can run the tests in command line fine (for e.g. like :~ nathan$ trial smoke_tests ) but would like to step through the test in an IDE
in another question
How debuging twisted application in PyCharm
It has been suggested that "configure the "Script" setting to point to that twistd" . so for 'trial' I tried pointing to /Library/Python/2.7/site-packages/twisted/trial/runner.py , but that fails.
Script
Assuming your working directory is /home/myself/mypythonproject/myworkingdirectory ,
Create a python file in your working directory with name trial_try.py. This is a copy of /usr/local/bin/trial. So use a copy of the version you have.
import os, sys
try:
import _preamble
except ImportError:
try:
sys.exc_clear()
except AttributeError:
# exc_clear() (and the requirement for it) has been removed from Py3
pass
# begin chdir armor
sys.path[:] = map(os.path.abspath, sys.path)
# end chdir armor
sys.path.insert(0, os.path.abspath(os.getcwd()))
from twisted.scripts.trial import run
run()
Configuration
Create a new Run Configuration in Pycharm.
for Script Enter
/home/myself/mypythonproject/myworkingdirectory/trial_try.py
for Parameters Enter you Parameters that you would use when running trial on the command line
for e.g. test_smoke
for Working directory Enter
/home/myself/mypythonproject/myworkingdirectory
You should be all Set !
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
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.