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.
Related
current goal is to import code from 1 file and run it in another but i getting this error
Job "runF5 (trigger: interval[1 day, 0:00:00], next run at: 2023-01-31 00:24:00 PKT)" raised an exception
Traceback (most recent call last):
File "E:\pyton\Lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "e:\completed zameen project\combined.py", line 11, in runF5
m5.runF5(playwright)
File "e:\completed zameen project\m5.py", line 131, in runF5
browser = playwright.chromium.launch(headless=False, slow_mo=600)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'playwright' has no attribute 'chromium'
code in file 1
i intend to import the function runF5 from file 1 to file 2
from playwright.sync_api import Playwright, sync_playwright, expect
#import playwright
import random
import time
import sys
import pyautogui
def runF5(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False, slow_mo=600)
context = browser.new_context()
page = browser.new_page()
page.goto('https://www.google.com/', timeout=0)
#time.sleep(1)
code in file 2 which is importing the code from file 1
import playwright
import schedule
import datetime
import time
import m5
import schedule
from apscheduler.schedulers.background import BackgroundScheduler
def runF5():
# code to run at 03:30
m5.runF5(playwright)
scheduler = BackgroundScheduler()
scheduler.add_job(runF5, 'interval', days=1, start_date='2022-01-01 03:31:00', timezone='Asia/Karachi')
scheduler.start()
# Keep the program running
while True:
pass
i was expected at the given time the code will run and open google but i keeping getting the error
i always change the time so the codes runs 2mins from now- for testing
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'
I have been learning working with classes in python after learning OOPs in c++.
I am working on a project, where I have a class defined in one file, and an important function to be used in the class in the seperate file.
I have to call the class in the first file, but I am getting the ImportError.
Great, if you could help.
try1.py
from try2 import prnt
class a:
def __init__(self):
print("started")
def func1(self):
print("func1")
prnt()
try2.py
from try1 import a
b = a()
b.func1()
def prnt():
b.func()
As for eg, in the above example, when I am running try1.py, I am getting an ImportError: cannot import name 'prnt'.
You absolutely need to redesign your project. Even if you did manage to get away with the cyclic imports (ie by moving the import to inside the function - but don't do it) you will still get a NameError: name 'b' is not defined since b is not define in prnt.
Unless prnt can't be defined in class a (why?), consider defining prnt in a third, "utils" file and import it in both try1.py and try2.py and pass an object to it so it can access all of its attributes.
Just run your code, read the error, and deduct something from it.
When you run it, here is the error message :
Traceback (most recent call last):
File "C:\Users\Kilian\Desktop\Code\Garbage\tmp.py", line 7, in <module>
from temp2 import prnt
File "C:\Users\Kilian\Desktop\Code\Garbage\temp2.py", line 1, in <module>
from tmp import a
File "C:\Users\Kilian\Desktop\Code\Garbage\tmp.py", line 7, in <module>
from temp2 import prnt
ImportError: cannot import name prnt
Your script is trying to import something it already has tried to import earlier on. Python is probably deducing that it can't import it. :)
I have this:
import sys, struct, random, subprocess, math, os, time
from m_todo import ToDo
(rest)
Which results in:
Traceback (most recent call last):
File "6.py", line 2, in <module>
from m_todo import ToDo
ImportError: cannot import name ToDo
My m_todo module:
import os
class ToDO:
'''todo list manager'''
def __init__(self):
pass
def process(self):
'''get todo file ready for edition'''
print(os.path.exists('w_todo.txt'),'\t\t\tEDIT THIS')
I read some similar questions, which suggested something about circular references, but it is not the case.
I also saw a suggestion about using relative imports, but trying that resulted in another error:
Traceback (most recent call last):
File "6.py", line 2, in <module>
from .m_todo import ToDo
SystemError: Parent module '' not loaded, cannot perform relative import
This is like the third time I use Python, so it might be a silly mistake, but it's causing me some confusion since I'm importing other modules in the same way without any issues.
So... what's going on here?
Your class is called ToDO (note the capitalisation), not ToDo.
Either fix your import:
from m_todo import ToDO
or the classname:
class ToDo:
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.