- Can’t get mod_python and web.py to work, AttributeError: ‘module’ object has no attribute ‘main’
- pip.main install fails with ‘module’ object has no attribute ‘main’
- 5 Answers 5
- Python -> AttributeError: ‘module’ object has no attribute ‘main’
- !/usr/bin/env python
- 2 Answers 2
- Locally installed package-ImportError: module ‘__main__’ has no attribute ‘main’
Can’t get mod_python and web.py to work, AttributeError: ‘module’ object has no attribute ‘main’
I didn’t do the Apache setup for this it was done by the hosting company and apparently they don’t know that much about Python and mod_python, so I’m stuck I only have access to a .htaccess file and my own code. .htaccess
AddHandler python-program .py PythonHandler wsgiref.modpython_gateway::handler PythonOption wsgi.application code::main
#!/usr/bin/python import web urls = ( '/(.*)', 'hello' ) main = web.wsgifunc(web.webpyfunc(urls, globals())) class hello: def GET(self, name): if not name: name = 'Pussy' return 'Hello, '+ name +'!' #if __name__ == "__main__": #main = web.wsgifunc(web.webpyfunc(urls, globals()))
MOD_PYTHON ERROR ProcessId: 30954 Interpreter: 'example.dk' ServerName: 'example.dk' DocumentRoot: '/var/www/example.dk/web' URI: '/code.py' Location: None Directory: '/var/www/example.dk/web/' Filename: '/var/www/example.dk/web/code.py' PathInfo: '' Phase: 'PythonHandler' Handler: 'wsgiref.modpython_gateway::handler' Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch default=default_handler, arg=req, silent=hlist.silent) File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1229, in _process_target result = _execute_target(config, req, object, arg) File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1128, in _execute_target result = object(arg) File "/usr/lib/python2.7/wsgiref/modpython_gateway.py", line 206, in handler app = getattr(module, objname) AttributeError: 'module' object has no attribute 'main'
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64
pip.main install fails with ‘module’ object has no attribute ‘main’
Is there a folder/file in your workspace with name pip. It might be possible that some other module/file is being imported. Check what is the output of pip.__file__ . It should point to the right location of pip.
@serbia99 I call a script with my own python interpreter which internally imports the module which has above snippet. So, I have my own pip and python both
i have similar problem recently , same thing happened following pip update to 10.0.1. i deleted the python/site-packages/pip/__pycache__ folder and downgrade it to 9.0.1. and it worked. you might want to have a go.
5 Answers 5
pip Developers do not recommend calling pip from within the program. And the pip.main() method has been removed from pip v10. As an alternative method it is recommended to execute pip in subprocess.
try: import requests except: import sys import subprocess subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'requests==2.0.1', 'PyYAML==3.11']) import requests
I had the same issue and just running the below command solved it for me:
The pip.main function was moved, not removed by pip devs. The highest voted solution here is not good. Going from python -> shell -> python is not a good practice when you can just run the python code direct. Try from pip._internal import main then you can use that main function to execute your pip calls like before.
The short answer is don’t do this. Use setup.py or a straight import statement.
pip affects the whole environment. Depending on who is running this and why, they may or may not want to install requests in the environment they’re running your script in. It could be a nasty surprise that running your script affects their python environment.
Installing it as a package (using python setup.py or pip install ) is a different matter. There are well-established ways to install other packages using requirements.txt and setup.py . It is also expected that installing a package will install its dependencies. You can read more in the python.org packaging tutorials
If your script has dependencies but people don’t need to install it, you should tell them in the README.rst and/or requirements.txt . or simply include the import statement, and when they get the error, they’ll know what to do. Let them control what environment installs which packages.
Python -> AttributeError: ‘module’ object has no attribute ‘main’
I am working on a Raspberry Pi 3 and I am trying to visualize some values of sensors on Munin. I am using Python in order to execute scripts on Munin. I found a script to test and I am trying to execute it but I got the following error :
Traceback (most recent call last):
File «cpu_field», line 23, in munin.main() AttributeError: ‘module’ object has no attribute ‘main’
This is the script : https://github.com/CooledCoffee/python-munin/ Of course, I added at the beginning :
!/usr/bin/env python
But, what I didn’t understand is that others scripts are working like this one : https://gist.github.com/tomoconnor/813813
2 Answers 2
Would be nice if you could put the code in the question as well.
Anyways. The python-munin you use is entirely different and provides no main() function (as it called in line 23). Names for python modules are not protected and ‘munin’ is a obvious choice used by more than one developer. The first script should run with the module you get with
The other script uses this python-munin module and you probably get it directly from the git repository. They are not compatible.
there is no code nor right/false. you must decide which module you want to use and write/use matching code to it. You can’t mix it like you did.
So, this is the code I am using :
> #!/usr/bin/env python > > import munin > > category = 'system' fields = [ > 'load1', > 'load5', > 'load15', ] vlabel = 'load' > > def values(): > with open('/proc/loadavg') as f: > data = f.read() > load1, load5, load15 = [float(s) for s in data.split()[:3]] > return < >'load1': load1, > 'load5': load5, > 'load15': load15, > > > > if __name__ == '__main__': > munin.main()
This is the answer I got with sudo python xxx, I got the same answer with sudo munin-run xxx :
pi@dex:/etc/munin/plugins $ sudo python first Traceback (most recent call last): File "first", line 24, in munin.main() AttributeError: 'module' object has no attribute 'main'
I thin you are right because when I installed munin with
it worked. But, then I installed this python-munin module and it didn’t work anymore. I removed the folder python-munin but I still got the same error. How can I remove properly the previous folder ?
Locally installed package-ImportError: module ‘__main__’ has no attribute ‘main’
I need a little help with python packaging. I known similar questions has been already asked, but I could not find the solution for my problem. Here is the output of tree:
. ├── env ├── prala │ ├── __init__.py │ └── __main__.py └── setup.py
from setuptools import setup, find_packages setup( name='prala', version='0.5', description='Practice Language', url='http://github.com/*/*', author='*', author_email='*@*.com', license='MIT', classifiers =[ "Programming Language :: Python", "Programming Language :: Python :: 3", ], packages=find_packages(), entry_points = < 'console_scripts': ['ppp=__main__:main'], >, zip_safe=False)
def main(): print("hello world") if __name__ == "__main__": main()
- I activated the virtualenv in the root: $ source env/bin/activate
- I built the dist and installed it: (env) $ python setup.py install
- I run the entry point: (env) $ ppp
Unfortunately I got error instead of the ‘hello world’ message:
Traceback (most recent call last): File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2332, in resolve return functools.reduce(getattr, self.attrs, module) AttributeError: module '__main__' has no attribute 'main' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/akoel/Projects/python/delete/env/bin/ppp", line 11, in load_entry_point('prala==0.5', 'console_scripts', 'ppp')() File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 480, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2693, in load_entry_point return ep.load() File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2324, in load return self.resolve() File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2334, in resolve raise ImportError(str(exc)) ImportError: module '__main__' has no attribute 'main'
Can anyone help me showing what did I miss?