ImportError: cannot import name 'Process' - python

I am writing a simple program to check how multiprocessing works in Python 3. And I am testing with code that is similar to what is available in Python 3.6 documentation.
However when running the code I am facing an ImportError and not able to move forward.
I have also observed some confusing outputs.
When executing the code in PYthon IDE, it does not throw an error.
from multiprocessing import Process
However, if i execute it on Linux prompt, it throws an error.
My complete code is
from multiprocessing import Process
def worker():
print("working")
if __name__ == '__main__':
jobs = []
p = Process(target=worker)
jobs.append(p)
p.start()
print(jobs)
Traceback (most recent call last):
File "C:/Users/AASRITHA/PycharmProjects/untitled/multiprocessing.py", line 1, in <module>
from multiprocessing import Process
File "C:\Users\AASRITHA\PycharmProjects\untitled\multiprocessing.py", line 1, in <module>
from multiprocessing import Process
ImportError: cannot import name 'Process'

Related

how to import module kicost and pass arguments

I'm trying to import the module kicost in the python script and call the main function with arguments.
So far, I've been unsuccessful to do it after many trials.
The module has been installed with pip.
import sys, os
from kicost import *
import importlib
spam_spec = importlib.util.find_spec("kicost")
print(spam_spec)
# sys.argv.append('-h')
main()
exit()
Here is the execution log:
ModuleSpec(name='kicost',
loader=<_frozen_importlib_external.SourceFileLoader object at
0x101278160>,
origin='/Users/sebo/Projects/python/pandas/env/lib/python3.8/site-packages/kicost/init.py',
submodule_search_locations=['/Users/sebo/Projects/python/pandas/env/lib/python3.8/site-packages/kicost'])
Traceback (most recent call last): File "test-import-kicost.py",
line 12, in
main() NameError: name 'main' is not defined
I guess there is something I don't understand with the import.
Could somebody help me? Thanks.
S/
Finally, I managed to do it:
import sys, os
import kicost.__main__ as kicost
sys.argv.append('-i=/Users/sebo/Projects/python/pandas/test.csv')
sys.argv.append('--currency=EUR')
sys.argv.append('--overwrite')
kicost.main()
exit()

ThreadPoolExecutor is not defined [python3]

I'm trying to run the following code, which was directly copied from the documentation:https://docs.python.org/dev/library/concurrent.futures.html#module-concurrent.futures :
import executor
import concurrent.futures
import time
def wait_on_b():
time.sleep(5)
print(b.result()) # b will never complete because it is waiting on a.
return 5
def wait_on_a():
time.sleep(5)
print(a.result()) # a will never complete because it is waiting on b.
return 6
executor = ThreadPoolExecutor(max_workers=2)
a = executor.submit(wait_on_b)
b = executor.submit(wait_on_a)
And I get the following output:
Traceback (most recent call last):
File "test1.py", line 16, in <module>
executor = ThreadPoolExecutor(max_workers=2)
NameError: name 'ThreadPoolExecutor' is not defined
I'm assuming that I forgot to import something, but I don't know.
Either use from concurrent.futures import ThreadPoolExecutor instead of import concurrent.futures, or leave the import as-is and use executor = concurrent.futures.ThreadPoolExecutor(maxworkers=2).
Also note that the example code you copied is designed to deadlock, so it's not going to work properly once you fix the import issue.

Curses returning AttributeError: 'module' object has no attribute 'initscr'

I am following the Curses programming HowTo on the Python site, but I am running into a rather bizarre issue.
My code is currently very short, doesn't actually do anything because of this error, I haven't been able to move on. Here's my code:
import curses
#from curses import wrapper
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
def main(stdscr):
begin_x = 20; begin_y = 7
height = 5; width = 40
win = curses.newwin(height, width, begin_y, begin_x)
stdscr.refresh()
stdscr.getkey()
if __name__ == '__main__':
wrapper(main)
and the Traceback:
Traceback (most recent call last):
File "curses.py", line 1, in <module>
import curses
File "/home/nate/Documents/Programming/Python/curses.py", line 4, in <module>
stdscr = curses.initscr()
AttributeError: 'module' object has no attribute 'initscr'
I commented out the from curses import wrapper because that was giving me another error,
Traceback (most recent call last):
File "curses.py", line 1, in <module>
import curses
File "/home/nate/Documents/Programming/Python/curses.py", line 2, in <module>
from curses import wrapper
ImportError: cannot import name wrapper
but I suppose that would be another question.
I am following the tutorial word for word right now, to learn curses, but currently the only thing it's making me do is use curses directed at Python :P.
I am running Python 3.3.2 on Ubuntu 13.10, so this question has nothing to do with this, as he was using Windows and I am not (thankfully :D)
Why am I not able to do this? I'm copying it directly from the Python site, so you'd think it would work!
You named your file curses.py, so Python thinks that file is the curses module. Name it something else.
mv curses.py someOtherName.py
If you get the same error, try removing any .pyc files.
In this case it would be rm curses.pyc.

AGI-BIN:ImportError: No module named asterisk

I have installed asterisk version 1.4.44 and using Python for agi scripts. I have context "9999" than I trying to call while executing this I am getting below errors.
is there any dependency to be installed to get this working?
-- Launched AGI Script /var/lib/asterisk/agi-bin/incident/SetCommonVariables.py
Traceback (most recent call last):
File "/var/lib/asterisk/agi-bin/incident/SetCommonVariables.py", line 5, in <module>
from asterisk import agitb
ImportError: No module named asterisk
-- AGI Script incident/SetCommonVariables.py completed, returning 0
-- Executing [s#IncidentInitiation:4] Goto("SIP/9999-00000000", "CheckAuthorization|1") in new stack
-- Goto (IncidentInitiation,CheckAuthorization,1)
-- Executing [CheckAuthorization#IncidentInitiation:1] AGI("SIP/9999-00000000", "incident/CheckAuthorization.py") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/incident/CheckAuthorization.py
Traceback (most recent call last):
File "/var/lib/asterisk/agi-bin/incident/CheckAuthorization.py", line 7, in <module>
from asterisk import agitb
Edit 1 :
Asterisk 1.4.44
CentOS 6.5(Final)
Python version (2.4,2.6.6,2.7)
Edit 2 :
i have already "from asterisk import agitb" in above issues file here code snipes of file
File "/var/lib/asterisk/agi-bin/incident/SetCommonVariables.py
#!/usr/bin/python
from IncidentConstants import *
import sys # system stuff
from asterisk import agitb
agitb.enable(display = False, logdir = '/var/log/asterisk')
# Global variables
from asterisk.agi import * # our agi stuff
agi = AGI()
agitb.enable(agi, False, '/var/log/asterisk')
in every file i have above content
My guess is that CGI uses the system python. Not the python that has the Asterisk module.
Check that the failing CGI script does not have
#!/usr/bin/python
And if does, change this to use the proper Python.
For each python you have try doing:
from asterisk import agitb
Then you will find which one has asterisk

error when using import command in python

I'm new to python and now learning how to to import a module or a function, but I got these posted errors. The python code is saved under the name: hello_module.py
python code:
def hello_func():
print ("Hello, World!")
hello_func()
import hello_module
hello_module.hello_func()
error message:
Traceback (most recent call last):
File "C:/Python33/hello_module.py", line 9, in <module>
import hello_module
File "C:/Python33\hello_module.py", line 10, in <module>
hello_module.hello_func()
AttributeError: 'module' object has no attribute 'hello_func'
You cannot and should not import your own module. You defined hello_func in the current namespace, just use that directly.
You can put the function in a separate file, then import that:
File foo.py:
def def hello_func():
print ("Hello, World!")
File bar.py:
import foo
foo.hello_func()
and run bar.py as a script.
If you try to import your own module, it'll import itself again, and when you do that you import an incomplete module. It won't have it's attributes set yet, so hello_module.hello_func doesn't yet exist, and that breaks.

Categories

Resources