Relative Path Error When Migrating From Eclipse (PyDev) into VS Code - python

I am completely new to the VS Code - find it very easy to use compared with Eclipse so I am trying to migrate our existing projects from Eclipse into VS Code.
By selecting the folder as the eclipse file, the interpolator seems to be selected automatically. But I do see the error as below.
Exception has occurred: FileNotFoundError
[Errno 2] No such file or directory: 'C:\\Development\\log\\Hedger.log'
File "C:\Development\Optimizer\src\globals\config.py", line 17, in <module>
hdlr = logging.FileHandler('../log/Hedger.log') # write log messages to log file
File "C:\Development\Optimizer\src\run_server.py", line 1, in <module>
import globals.config as config
Though the actual path should be "C:\Development\Optimizer\log\Hedger.
Could anyone give a guess/hint? Appreciate it.
enter image description here

Two solutions:
One: change the "../log/Hedger.log" to "./log/Hedger.log".
Two: in launch.json file setting '"cwd": "${workspaceFolder}/src",' in "configurations".
Explains:
${cwd} - the task runner's current working directory on startup.
The default setting of 'cwd' is the "${workspaceFolder}", in your project means "C:\Development\Optimizer". So, the '../log/Hedger.log' means 'C:\Development\log\Hedger.log'.

Related

Python Path.rglob failing on network error when encountering folder without permission

I am new to Python and have been using this site as a reference...thanks for everything, I have learned a ton. First question:
I am running a basic recursive file search with Path.rglob(). I am running into a error when it encounters a folder that it does not have permission to access. I am running Python 3.7 on Windows and connecting to a windows share on a network drive.
Here's my code:
scan_folder = pathlib.Path("//192.168.1.242/Media")
nfo_files = list(scan_folder.rglob("*.nfo"))
It works perfectly until I encounter a folder that I do not have permission to access, then it errors out with:
Traceback (most recent call last):
File "D:/Working/media_tools/media_tools/movies_nfo_cataloger.py", line 337, in <module>
nfo_files = list(scan_folder.rglob("*.nfo"))
File "C:\Users\ulrick65\Anaconda3\lib\pathlib.py", line 1094, in rglob
for p in selector.select_from(self):
File "C:\Users\ulrick65\Anaconda3\lib\pathlib.py", line 544, in _select_from
for p in successor_select(starting_point, is_dir, exists, scandir):
File "C:\Users\ulrick65\Anaconda3\lib\pathlib.py", line 507, in _select_from
entries = list(scandir(parent_path))
OSError: [WinError 59] An unexpected network error occurred: '\\\\192.168.1.242\\Media\\#recycle'
Process finished with exit code 1
I searched and found the following Issue for Pathlib that appears to have been fixed, however the error is different in my case as it points to "Unexpected network error" instead of permissions.
https://bugs.python.org/issue24120
I verified that this is indeed a permissions error, I do not have access to that Recycle folder as the user I am logged in as. I edited the permissions for that folder and gave myself access and the code runs fine after that.
I know I could use oswalk as it ignores these...but I figured given the bug fix I linked to above, so should path.glob however it doesn't. Also, using path.rglob() is pretty slick, one line of code and is fast (not that oswalk wouldn't be just as fast).
Any help is appreciated.

"AttributeError: '_NotSet' object has no attribute 'lower'" when a PRAW python file is converted to an exe using Pyinstaller

As the title says.
When I execute the converted python file (the .exe) I get the following output:
Traceback (most recent call last):
File "background.py", line 10, in <module>
File "site-packages\praw\reddit.py", line 129, in __init__
File "site-packages\praw\config.py", line 72, in __init__
File "site-packages\praw\config.py", line 98, in _initialize_attributes
File "site-packages\praw\config.py", line 31, in _config_boolean
AttributeError: '_NotSet' object has no attribute 'lower'
[1692] Failed to execute script background
I did not use a praw.ini file, instead hardcoding the values for the logon as such:
import praw
import praw.models
import urllib.request
from random import randint
from os import getcwd
import ctypes
r = praw.Reddit(client_id='client',
client_secret='secret',
user_agent='user')
sub = r.subreddit('earthporn')
choose = []
for i in sub.hot(limit=20):
a = str(i.url)
if "i.redd" in a or "i.imgur" in a:
choose.append(i.url)
x = choose[randint(0,len(choose)-1)]
resource = urllib.request.urlopen(x)
output = open("daily_image.jpg","wb")
output.write(resource.read())
output.close()
direc = getcwd() + "\\daily_image.jpg"
ctypes.windll.user32.SystemParametersInfoW(20, 0, direc , 0)
The above file works in just python but not when converted to an exe. (obviously with the client,secret and user id's set, feel free to steal it idrc)
Any help is really appreciated!
I had this error and found that to resolve it you need to have a copy of the praw.ini in the directory where you a running the executable (your_app.exe) from. You can find your praw.ini in your installed \praw directory.
Right.
So, pyinstaller isn't a perfect .py to .exe converter, so some things get lost in translation.
I first commented out all updating in praw as this caused a crash in the .exe created by pyinstaller (note everything I did here caused errors in the .exe but never in the .py).
I then had to manually set some variables along the way because they weren't set when they were called in the .exe version. Either threading is used in PRAW and in the .exe version it can't keep up, or there's some seriously weird shit going on.
Yeh so I basically just modified code all throughout praw so this thing would run. If anyone comes across this issue like me and can't find the answer anywhere (because trust me I scoured planet Earth and it's no where to be found) message me and I can send you my praw version.
May god forbid you get this error
I can't imagine anyone is still looking at this, but I solved this by adding the praw.ini as a data file using a pyinstaller .spec file. The steps I used were:
Create a .spec file by using the normal pyinstaller <name>.py with whatever flags you like (e.g., --onefile).
Modify the generated .spec file by adding the following:
import os
import importlib
proot = os.path.dirname(importlib.import_module('praw').__file__)
datas = [(os.path.join(proot, 'praw.ini'), 'praw')]
a = Analysis(['<name>.py'],
...
datas=datas,
Run pyinstaller against the newly edited .spec file: pyinstaller <name>.spec
Have no fear! In lieu of a properly-packaged praw.ini, the configuration it contains can also be explicitly provided as args to the Reddit constructor:
reddit = praw.Reddit(
client_id="CHANGEME",
client_secret="CHANGEME",
user_agent="CHANGEME",
check_for_updates=False,
comment_kind="t1",
message_kind="t4",
redditor_kind="t2",
submission_kind="t3",
subreddit_kind="t5",
trophy_kind="t6",
oauth_url="https://oauth.reddit.com",
reddit_url="https://www.reddit.com",
short_url="https://redd.it",
ratelimit_seconds=5,
timeout=16,
)
The above code works great for me, even after being packaged with PyInstaller. Note that future PRAW updates may add more defaults to praw.ini, so you'll have to copy them over or you'll get similar errors about unset config options.
See this GitHub issue: https://github.com/praw-dev/praw/issues/1016.

In python, Error opening location of tmpfile: No such file or directory exist

I'm studying from 'Coding the matrix' by Philip Klein. In chapter two, there's an example for plotting complex numbers
from plotting import plot
S = {2+2j, 3+2j, 1.75+1j, 2+1j, 2.25+1j, 2.5+1j, 2.75+1j, 3+1j, 3.25+1j}
plot(S, 4)
plotting module: http://resources.codingthematrix.com
When I run the code directly through python in the terminal, it works fine, but when I run it as seperate file "$ python example.py", I get this error:
gvfs-open: file:///tmp/tmpOYFVs8.html: error opening location: Error
when getting information for file '/tmp/tmpOYFVs8.html': No such file
or directory
Not sure how to resolve this. Tried to play with module code a bit, but got nowhere.
I checked the code of plotting.py and found out that there is atexit event registered at the end of the code which basically deletes the file when your programs exits.So when you invoke it as script python intrepreter exits which will intern calls atexit register to delete the file.
def remove_at_exit(path):
atexit.register(os.remove, path)
you can directly comment out the call to remove_at_exit method in plotting.py at line no 92
open plotting.py then patch this
hpath = os.getcwd() + "/something.html"
Instead or a line after this
hpath = create_temp('.html')

crontab no such file or directory

I am trying to bring up a cron job that executes a python script every 5 minutes looking like this:
echo '2-57/5 * * * * $HOME/raspberry_pi/temp_test.py >> $HOME/raspberry_pi/temp_test.log 2>&1' | crontab -
Looking into the generated logfile I am getting this error:
Traceback (most recent call last): File
"/home/pi/raspberry_pi/temp_test.py", line 204, in
create_graph(temperature, rrd_db) File "/home/pi/raspberry_pi/temp_test.py", line 156, in create_graph
'GPRINT:temp0:LAST:Letzter Messwert: %2.1lf °C') rrdtool.error: opening 'db_test_temp.rrd': No such file or directory
my rrd-database and the python-script that should be executed are in the same directory and I already set the rights of the rrd-file to 777.
I tried out many things while digging in the www ( generating a local cmd-file in the root directory to execute the job, even setting a "cd" in front of the path) but nothing worked. Maybe it's completely obvious and I'm not seeing through because I'm a complete newbie but I would really appreciate any advice.
Thank u very much
The error message is pretty clear: the file db_test_temp.rrd does not appear to exist, though you think it does.
This could be due to several reasons -
The file really doesnt exist
It does exist, but it is in a different location
The process has no permissions on parent directories
The most likely is that you have given the file with no path, which implies it is in the current directory. Most likely, the current directory is not what you are expecting. Unless you are explicitly changing current directory in your script, you are probably somewhere else.
Try specifying the RRD file with full path -- IE, /path/to/file/file.rrd rather than just file.rrd. This will likely solve your problem.

Python script from batch file won't run in task scheduler

Hi folks so I got the following problem,
I have the following code in a batch file:
..\python-2.7.10.amd64\python.exe ./bin/bla.py ./conf/config.conf > ./logs/output.txt
This works like a charme by double clicking the batch. Next my plan was to automate the call of this batch by adding it to the task scheduler in windows. So I changed all the relative paths to absolute paths:
D:\path\to\python-2.7.10.amd64\python.exe D:\path\to\bin\bla.py D:\path\to\conf\config.conf > D:\path\to\logs\output.txt
This also still works by double clicking the batch file.
So my next step was adding the batch to the task scheduler but when I run it from there I get this error message:
Traceback (most recent call last): File "D:\path\to\bin\bla.py", line 159, in logging.config.fileConfig(logFile) File "D:\path\to\python-2.7.10.amd64\lib\logging\confi eConfig formatters = _create_formatters(cp) File "D:\path\to\python-2.7.10.amd64\lib\logging\confi reate_formatters flist = cp.get("formatters", "keys") File "D:\path\to\python-2.7.10.amd64\lib\ConfigParser. raise NoSectionError(section) ConfigParser.NoSectionError: No section: 'formatters'
So for some reason the python script can't find the conf file by the absolute path I think but I don't understand why. I also tried it with the relative paths in the task scheduler it obviously also doesn't work.
Does anyone of you have a clue why it works straight from the batch but not from the task scheduler ?
Thank you guys for your help. It was indeed "just" the working directory I had to set to the location of the bat file

Categories

Resources