Forums

Python Script Not Running

I am working on a Django project. It includes a functionality in which when admin clicks on a certain button, it runs a Python script and redirect it to some other page after complete process. Before hosting, it was working fine. Now that I hosted it here, the script doesn't run at all. It just redirects to another page without providing any error. Why's that? Also, my script includes chromedriver, in case it has anything to do with it.

Kindly help me.

This might be the error. Why doesn't it recognize it?

Code

out = run([sys.executable, '//home//maisum279//traffic//traffic//trafficapp//traffic.py', "--domain", url, "--threads", '5', "--max-clicks", maximum, "--min-clicks", minimum, "--stay", stay, "--requests", requests])

Server Log
2020-09-21 11:05:02 /usr/local/bin/uwsgi: unrecognized option '--domain' 2020-09-21 11:05:02 getopt_long() error

I'm guessing the script you're trying to run is a Python script, but you seem to be running it with uwsgi. If you're using sys.executable to find the binary, that will not give you a Python interpreter, it will invoke uwsgi, which is running your web app.

I manage to run it by repalcing

sys.executable  with ['/usr/bin/env', 'python3', ...].

I think it's working since I got a different error now which is

driver = webdriver.Chrome('chromedriver',options=options)
TypeError: __init__() got an unexpected keyword argument 'options'

Even though it was working fine before hosting. Can you guide me?

I have also tried this:

pip3.8 install --user --upgrade selenium

but got an error:

ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

After going through some posts, I guess I will need to have an upgraded virtualization system. Can you do that please?

Also, I am not using this line:

chrome_options.add_argument("--disable-gpu")

Will this be necessary to add since I didn't use it earlier on my localhost?

No problem. I have enabled it for your account. New consoles will use the new system and your web app will use it after it has been reloaded.

Yes, you need that argument.

ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

I am still getting this error.

pip3.8 install --user --upgrade selenium

Also, I am using python 3.8 so that is not the issue as far as I know

If you're installing into a virtualenv, do not use the --user flag.

I have tried it that way earlier and now too. It says the requirement is already satisfied but I still get the same error.

Then make sure that you're running with the virtualenv activated when you're starting the subprocess. You can do that by running bin/python in the directory where your virtualenv is.

It is activated. I have made sure of that.

(myproj) 15:35 ~/traffic/traffic (master)$ pip3.8 install --upgrade selenium

myproj is my virtual environment. This is what I get after running the command.

Looking in links: /usr/share/pip-wheels                                                                                               
Requirement already up-to-date: selenium in /home/maisum279/.virtualenvs/myproj/lib/python3.8/site-packages (3.141.0)                 
Requirement already satisfied, skipping upgrade: urllib3 in /home/maisum279/.virtualenvs/myproj/lib/python3.8/site-packages (from selenium) (1.25.10)

When you call the subprocess, that activation goes away. You need to change the way that you call your subprocess for the virtualenv to be activated in the subprocess.

Can you give me an idea of how should I call it? I have the same issue while I was running it at localhost and it got resolved by simply doing this, why not here:

pip3.8 install --user --upgrade selenium

It doesn't work because you are using a virtualenv. This page has details about how to execute a virtualenv Python. It's for scheduled tasks, but the idea is the same.

I am getting this error now.

2020-09-22 04:38:42 Exception in thread Thread-4:#012Traceback (most recent call last):#012  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner#012    self.run()#012  File "/usr/lib/python3.8/threading.py", line 870, in run#012    self._target(*self._args, **self._kwargs)#012  File "/home/maisum279/traffic/traffic/trafficapp/traffic.py", line 88, in generate_traffic#012    driver = webdriver.Chrome(options=options)#012  File "/home/maisum279/.virtualenvs/myproj/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__#012    RemoteWebDriver.__init__(#012  File "/home/maisum279/.virtualenvs/myproj/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__#012    self.start_session(capabilities, browser_profile)#012  File "/home/maisum279/.virtualenvs/myproj/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session#012    response = self.execute(Command.NEW_SESSION, parame
2020-09-22 04:38:42 subprocess 189 exited with code 0
2020-09-22 04:38:42 Exception in thread Thread-2:#012Traceback (most recent call last):#012  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner#012    self.run()#012  File "/usr/lib/python3.8/threading.py", line 870, in run#012    self._target(*self._args, **self._kwargs)#012  File "/home/maisum279/traffic/traffic/trafficapp/traffic.py", line 88, in generate_traffic#012    driver = webdriver.Chrome(options=options)#012  File "/home/maisum279/.virtualenvs/myproj/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__#012    RemoteWebDriver.__init__(#012  File "/home/maisum279/.virtualenvs/myproj/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__#012    self.start_session(capabilities, browser_profile)#012  File "/home/maisum279/.virtualenvs/myproj/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session#012    response = self.execute(Command.NEW_SESSION, parame

I was reading one of forum posts and found this:

It looks like the version of chromedriver that you have doesn't match the version of Chrome that it's running. The easiest way to fix that would be for us to move you over to our most recent system image, which has mutually compatible versions of Chrome and chromedriver built in. Would you like us to do that? All of your files and data will be kept safe, but because of the changes to the point releases of Python, any virtualenvs you have might break -- and if you're not using virtualenvs, the pre-installed Python modules will be upgraded so that might break any code you have that relies on the old installed versions.

Please help if that's the case.

That looks like you are trying to use threads in a web app. You can only have one thread (the main one) in a web app, so if you try to create more, it will fail.

Yes, I am. So PythonAnywhere doesn't support multithreading or is it only for unpaid accounts? If you guys don't support multithreading, what can be the alternative of running multiple threads?

We do not support multithreading in web apps and it doesn't matter whether it's a paid account or not. You can use threads in tasks and in consoles. If you really need threads in a web app, you can create threads in a subprocess.