Where is the error in my Python script? - file

I scripted download script.
When it runs it throws an error.
Code:
import urllib2, shutil
ftpfile = urllib2.urlopen("ftp://user:password#domain.com/file.txt")
localfile = open("C:\\dtmp", "wb")
shutil.copyfileobj(ftpfile, localfile)
Error:
Traceback (most recent call last):
File "download.py", line 4, in <module>
localfile = open("C:\\dtmp", "wb")
IOError: [Errno 13] Permission denied: 'C:\\dtmp'

You do not have write access on the path you tried to open.
In general it's not a good style to write directly on C:\. Instead you can write in your user directory or in a temporary directory.
import os.path
homedir = os.path.expanduser('~')
with open(os.path.join(homedir, 'filename')) as localfile:
shutil.copyfileobj(ftpfile, localfile)

Related

Why does a batch file work on manual execution but fails on running it as WinSW service?

I use WinSW for a Uvicorn service which basically provides an interface from which users can call certain SQL queries.
I have written a batch file which starts the program which works without a problem on manual execution with double clicking it. But if I run it as a service with WinSW and then start a query, there is output the error message:
ImportError: DLL load failed while importing _sqlite3: The specified module could not be found.
How can it be that the same .bat file works in standalone, but not as a service? And how do I fix it?
My service XML file with {Path_to_bat_directory} being a placeholder here in this post and being in real the hard coded directory path to the batch file containing the account name:
<configuration>
<id>X</id>
<name>X</name>
<description> XXX </description>
<executable>C:\Users\{Path_to_bat_directory}\StartUvicorn.bat</executable>
<onfailure delay="10 sec" action="restart"/>
<onfailure delay="20 sec" action="restart"/>
<onfailure action="none"/>
<resetfailure>1 hour</resetfailure>
<workingdirectory>C:\Users\{Path_to_bat_directory}</workingdirectory>
<priority>Normal</priority>
<stoptimeout>15 sec</stoptimeout>
<stopparentprocessfirst>true</stopparentprocessfirst>
<startmode>Automatic</startmode>
<waithint>15 sec</waithint>
<sleeptime>1 sec</sleeptime>
<logpath>C:\Users\{Path_to_bat_directory}\logs</logpath>
<log mode="roll"> </log>
</configuration>
The batch file StartUvicorn.bat:
call %~dp0win_env/Scripts/activate.bat
uvicorn main:app --port 8590 --host 0.0.0.0
The full error output as written into a log file is:
Exception in thread {X}:
Traceback (most recent call last):
File "{Path}\scoop\apps\anaconda3\current\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "{Path}\scoop\apps\anaconda3\current\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "{Path}\Sources\{Project}\.\populate_cache.py", line 25, in query_failures_from_db
df = pd.read_sql(statement, con, params=[date_from, date_to])
File "{Path}\Sources\{Project}\win_env\lib\site-packages\pandas\io\sql.py", line 563, in read_sql
pandas_sql = pandasSQL_builder(con)
File "{Path}\Sources\{Project}\win_env\lib\site-packages\pandas\io\sql.py", line 744, in pandasSQL_builder
import sqlite3
File "{Path}\scoop\apps\anaconda3\current\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "{Path}\scoop\apps\anaconda3\current\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed while importing _sqlite3: The specified module could not be found.

Extract some basic data with beautiful soup

Recently I tried to start web-scraping with python, in order to extract some basic informations in instagram using beautiful soup.
I wrote a simple code which is showed below:
from bs4 import BeautifulSoup
import selenium.webdriver as webdriver
url = 'http://instagram.com/umnpics/'
driver = webdriver.Firefox()
driver.get(url)
soup = BeautifulSoup(driver.page_source)
for x in soup.findAll('li', {'class':'photo'}):
print (x)
but after run it, some exceptions occured:
Traceback (most recent call last):
File "C:\Users\Mhdn\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Program Files (x86)\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Program Files (x86)\Python37-32\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Mhdn\Desktop\test2.py", line 5, in <module>
driver = webdriver.Firefox()
File "C:\Users\Mhdn\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
self.service.start()
File "C:\Users\Mhdn\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
You need to download geckodriver to your local system from here
In your code you need to provide executable_path for the geckodriver
Adding executable_path to your code:
from bs4 import BeautifulSoup
import selenium.webdriver as webdriver
url = 'http://instagram.com/umnpics/'
driver = webdriver.Firefox(executable_path= 'path/to/geckodriver') #<---Add path to your geckodriver
#example: driver = webdriver.Firefox(executable_path= 'home/downloads/geckodriver')
driver.get(url)
soup = BeautifulSoup(driver.page_source)
for x in soup.findAll('li', {'class':'photo'}):
print (x)

ffmpeg plugin imageio on Python

I simply try to call
from moviepy.editor import VideoFileClip
but I receive this error
File "/Users/macbook/python/main_video.py", line 3, in <module>
from moviepy.editor import VideoFileClip
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/moviepy/editor.py", line 22, in <module>
from .video.io.VideoFileClip import VideoFileClip
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/moviepy/video/io/VideoFileClip.py", line 3, in <module>
from moviepy.video.VideoClip import VideoClip
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/moviepy/video/VideoClip.py", line 20, in <module>
from .io.ffmpeg_writer import ffmpeg_write_image, ffmpeg_write_video
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/moviepy/video/io/ffmpeg_writer.py", line 19, in <module>
from moviepy.config import get_setting
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/moviepy/config.py", line 38, in <module>
FFMPEG_BINARY = get_exe()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/imageio/plugins/ffmpeg.py", line 86, in get_exe
raise NeedDownloadError('Need ffmpeg exe. '
imageio.core.fetching.NeedDownloadError: Need ffmpeg exe. You can download it by calling:
imageio.plugins.ffmpeg.download()
And if I try to call this one
imageio.plugins.ffmpeg.download()
Answer is
Imageio: 'ffmpeg.osx' was not found on your computer; downloading it now.
Error while fetching file: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>.
Error while fetching file: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>.
Error while fetching file: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>.
Error while fetching file: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>.
Traceback (most recent call last):
File "/Users/macbook/python/test.py", line 29, in <module>
imageio.plugins.ffmpeg.download()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/imageio/plugins/ffmpeg.py", line 55, in download
get_remote_file('ffmpeg/' + FNAME_PER_PLATFORM[plat])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/imageio/core/fetching.py", line 121, in get_remote_file
_fetch_file(url, filename)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/imageio/core/fetching.py", line 177, in _fetch_file
os.path.basename(file_name))
OSError: Unable to download 'ffmpeg.osx'. Perhaps there is a no internet connection? If there is, please report this problem.
What I can to do?
TRY
import imageio
imageio.plugins.ffmpeg.download()
Include above lines in your code . I had face same problem see the given picand this solved it.
Otherwise Check your internet connection.

GAE Launcher (Python) could not start

I believe this is related a number of python packages I have recently installed environmental variables that I have changed. I have re-installed Numpy and GAE, which did not help. So any suggestions on this? Thanks!
The GAE log indicated that the failure was lined to file import
2013-12-11 11:45:20 Running command: "['C:\\Python27\\pythonw.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=8094', '--admin_port=8004', 'D:\\Dropbox\\ubertool_src']"
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 197, in <module>
_run_file(__file__, globals())
File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 193, in _run_file
execfile(script_path, globals_)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 27, in <module>
import tempfile
File "C:\Python27\Lib\tempfile.py", line 34, in <module>
from random import Random as _Random
File "C:\Python27\Lib\site-packages\numpy\random\__init__.py", line 102, in <module>
ranf = random = sample = random_sample
NameError: name 'random_sample' is not defined
2013-12-11 11:45:21 (Process exited with code 1)
Update
just did a little test.
I can run from numpy import random
but if I run import random
The error msg is:
>>> import random
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\site-packages\numpy\random\__init__.py", line 102, in <module>
ranf = random = sample = random_sample
NameError: name 'random_sample' is not defined
Update
The problem is solved after removing C:\Python27\Lib\site-packages\numpy (I am not sure how this was added) from PYTHONPATH in Environmental Variable

django call_command syncdb failed: unable to open database file

I want to add some python codes after syncdb, so I decide to write a build.py which does everything including syncdb.
I write something in build.py as:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import call_command
from django.contrib.auth.models import User
call_command('syncdb', interactive=False)
But when I run build.py, it said:
Traceback (most recent call last):
File "/home/csimstu/PycharmProjects/TeenHope/TeenHope/build.py", line 5, in <module>
call_command('syncdb', interactive=False)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 161, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 385, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 56, in handle_noargs
cursor = connection.cursor()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 324, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 306, in _cursor
self._sqlite_create_connection()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 296, in _sqlite_create_connection
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
I've tried ./manage.py syncdb and use call_command in interactive shell mode, and both ways worked perfectly okay. How could it be?
If the database file isn't found that's probably because you didn't use a full path in your settings.
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.realpath(os.path.join(os.path.dirname(__file__), "relative_path_here", "database.db")),
Out of topic: I'd recommend to use Fabric for this kind of commands.

Resources