Importing module from within another Script while using Azure ML - python-3.x

I was earlier using console to run scripts which worked fine. Now that I wish to shift the entire process to crontab, I m facing ModuleNotFoundError:. The structure follows as I run a main file from parent directory, which imports another .py script from different location. To tackle the path error issue I have used os.chdir(path) which seems to work when I give print(os.getcwd()) but fails to find the required scripts in this new location. Can someone please help me with this. I m new to scripting.
#TIA
cwd = os.getcwd()
try:
os.chdir(cwd + 'pathToOtherScripts')
print('#\tGet current directory:',os.getcwd())
import SomeScript as SS
I get ModuleNotFoundError: at the parent directory. This error doesn't happen when I run the same script through console.

Related

Unable to read downloaded file after converting python file to .exe

I have written some code in python with the help of selenium which is doing web crawling in which it goes to a website after entering login id and password and downloaded a file. I have written the code in jupyter notebook with file name as GSPL_Code.ipynb and with the use of !jupyter nbconvert --to script GSPL_Code.ipynb and !pyinstaller GSPL_Code.py, I have first converted .ipynb file to .py and then made a .exe file from the .py file.
If I run the .exe file which is named as GSPL_Code and it is stored in dist/GSPL_Code then a file crv_report.xlsx is downloaded in the dist/GSPL_Code folder where the executable file is also there but the problem is code does not executed after downloading a file in that location. It got stuck. Nothing is happening now. When I run in jupyter notebook then I don't face any problem but after converting it into .exe, I am facing the problem.
Please find the following python code which is part of the whole script:
def handler(driver):
curr=driver.current_window_handle
for handle in driver.window_handles:
driver.switch_to.window(handle)
if handle != curr:
driver.close()
for handle in driver.window_handles:
driver.switch_to.window(handle)
print(driver.current_url)
click_event('//img[#id="IconImg_crv_report_toptoolbar_export"]')
click_event('//div[#style="white-space:nowrap;text-overflow:ellipsis;overflow:hidden;width:249px"]')
click_event('//span[#title="Microsoft Excel Workbook Data-only"]')
sleep(5)
click_event('//td[#class="wizbutton"]')
sleep(15)
print("Report is downloaded")
PATH1="C:\\Users\\ankit19.gupta\\Desktop\\Test_GSPL\\dist\\GSPL_Code"
crv_report= "/crv_report.xlsx"
print("Reading File")
dataframe1 = pd.read_excel(PATH1+crv_report)
print("Data is stored in the dataframe")
return dataframe1
I have written some print statements to see where the error is coming. So, here I am unable to see the print statement "Report is downloaded". Before this click_event('//td[#class="wizbutton"]') is used to click a button which download the file in chrome browser and I can see this downloaded file in the path dist/GSPL_Code directory and it got executed successfully but after that it got stuck and nothing happened.
I am unable to find what is happening here. Can anyone please help me. Any help would be appreciated.
Edit: There were some delay due to which print statements "Report is not downloaded" and "Reading File" were not showing in console but now I am able to see it but it is unable to read the file crv_report which is on the path PATH1 and it got stuck here and due to which I am unable to see print statement Data is stored in the dataframe
I think code is unable to access the file because os.remove is also not working when I try to delete the crv_report file. Can anyone please help me.
OS Directory Change:
When refactoring python files, in your case changing .py to .exe, it's important to make sure everything is a part of the current working directory. Simple OS checker command:
cwd = os.getcwd()
If directories are misaligned, you will need to run an OS command to change the directory before interacting with any files:
os.chdir('/your/desired/directory')

VSCode raising error when I try to import an Image in Python

I am using Visual Studio Code Community Edition.
I am using code like below and running it:
from tkinter import *
tk = Tk()
img = PhotoImage(pathtoimage)
Button(tk, image=img).pack()
tk.mainloop()
And when I try to run this, I get this error:
_tkinter.TclError: couldn't open "Resources/ytbanner.png": no such file or directory
I have quadruple-checked that this exists. I have the script being run in the directory where Resources is and this is happening.
Here's the filetree:
Path to my desktop
Projectname
Script I'm using
Resources
PNG image I want to use
Is this some sort of VSCode bug or is it something with the directories?
I'm only 11, so please don't be toxic
This is the normal behavior of the VS Code python extension. It automatically cd into workspace root. So you have to define the path from the workspace root to the file. while this method works on VS Code, this code will break on other editors because they don't cwd into the workspace folder. And also if you open the script from VS Code but this time with an in a different workspace (maybe previous workspace folder's parent or something) it would throw an error. So the solution to this would be something like this:
import os
import sys
if sys.argv:
filepath = sys.argv[0]
folder, filename = os.path.split(filepath)
os.chdir(folder) # now your working dir is the parent folder of the script
# and your code
If your code didn't run on terminal, if statement will return False thus the indented block wouldn't work. However, usually when code isn't running on terminal cwd is the parent folder of the file as we want.

Python giving import errors

I have some code with the following structure:
+main-folder
+Foo-folder/
+foo
+bar
-file1.py
-file2.py
+bar-folder
+bars
I'm trying to do a simple import like so:
#Some module in +foo
from foo-folder.file1 import some-module
so in Pycharm it works fine but when I run it from the command line. I get moduleNotFoundError.
I'm using python 3.7 so I have no init.py files in them. Any idea about this?
Thanks,
When some module import works from PyCharm but not from the command line, it's usually because PyCharm automatically adds your project files to the Pythonpath.
In any case, also check your run configuration in PyCharm. What does it say is your working directory? Which directory are you starting it from when running from the command line?

How to create a Python Executable that can run other python-files stored in a folder?

Is it possible to create a Python Executable file (using PyInstaller or similar) that can in its code access other Python-files stored in a specific folder?
The reason for that it MUST be an Executable is that the script sometimes must be run from computers that has not it's own Python installed. I have no problem creating an executable (with PyInstaller for example) and they work fine. The python script itself loads various kinds of data into a database. But everytime there is a new kind of data that has to be loaded into the database I have to recreate the hole exe-file. I'm looking for a way for the executable to access python-files (with Data Load Instructions) so that the actual pyton load-files can be altered instead of the exe-file.
I have tried to find a solution using this:
import os, time
from subprocess import call
for file in os.listdir('.'):
if file == 'loadInstructions.py':
call(['python', file])
print(file)
cwd = os.getcwd()
print(cwd)
input('To EXIT progran press ENTER.')
time.sleep(3)
It works running it from a python editor but when I turn this into an exe-file it does not. If I creat an exe-file with "call(['python', file])" commented out the rest works which I interpret that the exe-file can find the file in question but not run it.
I would be gratefule for any help.

__file__ does not exist in Jupyter Notebook

I'm on a Jupyter Notebook server (v4.2.2) with Python 3.4.2 and
I want to use the global name __file__, because the notebook will be cloned from other users and in one section I have to run:
def __init__(self, trainingSamplesFolder='samples', maskFolder='masks'):
self.trainingSamplesFolder = self.__getAbsPath(trainingSamplesFolder)
self.maskFolder = self.__getAbsPath(maskFolder)
def __getAbsPath(self, path):
if os.path.isabs(path):
return path
else:
return os.path.join(os.path.dirname(__file__), path)
The __getAbsPath(self, path) checks if a path param is a relative or absolute path and returns the absolute version of path. So I can use the returned path safely later.
But I get the error
NameError: name '__file__' is not defined
I searched for this error online and found the "solution" that I should better use sys.argv[0], but print(sys.argv[0]) returns
/usr/local/lib/python3.4/dist-packages/ipykernel/__main__.py
But the correct notebook location should be /home/ubuntu/notebooks/.
Thanks for the reference How do I get the current IPython Notebook name from Martijn Pieters (comments) the last answer (not accepted) fits perfect for my needs:
print(os.getcwd())
/home/ubuntu/notebooks
If you want to get path of the directory in which your script is running, I would highly recommend using,
os.path.abspath('')
Advantages
It works from Jupyter Notebook
It work from REPL
It doesn't require Python 3.4's pathlib
Please note that one scenario where __file__ has advantage is when you are invoking python from directory A but running script in directory B. In that case above as well as most other methods will return A, not B. However for Jupyter notbook, you always get folder for .ipyn file instead of the directory from where you launched jupyter notebook.
__file__ might not be available for you, but you can get current folder in which your notebook is located in different way, actually.
There are traces in global variables, if you will call globals() you will see that there is an element with the key _dh, that might help you. Here how I managed to load the data.csv file that is located in the same folder as my notebook:
import os
current_folder = globals()['_dh'][0]
# Calculating path to the input data
data_location = os.path.join(current_folder,'data.csv')
In modern Python (v3.4+) we can use pathlib to get the notebook's directory:
from pathlib import Path
cwd = Path().resolve()
# cwd == PosixPath('/path/to/this/jupyter/ipynb/file's/directory/')
# or this way, thanks #NunoAndré:
cwd = Path.cwd()
# cwd == PosixPath('/path/to/this/jupyter/ipynb/file's/directory/')
Update
#ShitalShah I cannot reproduce the error you are reporting. Jupyter notebook seems to work fine, regardless of the current working directory the application was started.
Example: file ~/dir1/dir2/untitled.ipynb and Jupyter notebook started in ~/dir1:
Jupyter notebook started in ~/dir1/dir2:
It's not possible to get the path to the notebook. You may find a way to get it that only works in one environment (eg os.getcwd()), but it won't necessarily work if the notebook is loaded in a different way.
Instead, try to write the notebook so that it doesn't need to know its own path. If doing something like getting the pwd, then be sure to fail fast / print an error if this doesn't work, vs. just silently trying to continue on.
See also: https://github.com/ipython/ipython/issues/10123
I'm new to python, but this works for me.
You can get os.path.dirname(__file__) equivalence by:
sys.path[0]
On new version of python and notebook __file__ works... If you have older versions, to get __file__ you can use
import inspect
from pathlib import Path
module_path = Path(inspect.getframeinfo(inspect.currentframe()).filename).resolve()
But... This is much slower... On the other hand, it will not return currrent working directory, but path to module, even if module is imported from elsewhere.

Resources