Directory in kivy going to home directory rather than to the directory of the main.py in mac but not in Windows - kivy

I have an unusual situation. I have a perfectly functional kivy app. Essentially, it uses FileChooserIconView and takes the root
`FileChooserIconView:
filters: [root.selected]
size_hint: (1, 0.4)`
so that with this function
`def selected(self, directory, filename):
# This function extracts the selected folder using
#the information from FileChooser
# This function takes as input:
# -filename: The filename of each file in each directory
# This function gives the output:
# -the full path to the directory with the selected folder
self.ids.mypath.text = os.path.join(os.path.dirname(filename), '') `
I can extract the directory of interest (rather than every single file in the directory).
The app extracts the directory of interest with FileChooser and uses it to extract all files there recursively and process them.
It works perfectly in Spyder in Windows. I package it in Windows and it works perfectly as an independent exe (finds all files that it needs for uploading and works perfectly).
It works perfectly in Spyder in Mac. However, I package it in Mac and it is completely unable to find the local files. This is my problem. Once packaged in mac, instead of looking for the local files in the dist folder, it looks for them in the mac home directory. It requires to read a file that is actually in the dist folder, but the app does not look for it there. It looks for it in the mac home directory.
I have tried to put the current directory where the main.py is (in the dist folder) using several methods, including
filename = inspect.getframeinfo(inspect.currentframe()).filename
path = os.path.dirname(os.path.abspath(filename))
from the question
How to properly determine current script directory?
but still it looks to the mac home directory instead of the directory where the main.py is.
I tried to change directory using the path above, but still goes to the mac home directory.
When I use print(os.getcwd()) in the main.py file it prints the correct directory when used in Spyder, but it goes to the mac home directory after packaging. I am really stuck and I could not find a similar question.
Any help much appreciated.

Thank you very much for all the comments, especially John Anderson who put me in the right track. They made my search much easier.
Finally, I solved it.
When you package an app you should reference the location of the main.py. I was using __file__ and, when the app is not packaged, that works and that is why it worked in Spyder. However, when the app is packaged, the correct way to find the main.py folder is sys._MEIPASS
That it is, as simple as using sys._MEIPASS
For more detail, the link https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#using-file-and-sys-meipass provided by John Anderson is great.

Related

Juypterlab | ipyfilechooser ignores start_path option with UNC paths and also local paths

I started working with Jupyterlab to analyse bunches of CSV files.
It is installed and run within an virtual environment on a windows PC (Windows 10, Python 3.9, JupyterLab 3.5.2).
As the filenames often change and our paths are quite deep I thought a file dialogue would be a good starting point to get data in.
I searched a while and found that ipyfilechooser could be my friend.
So I created a Jupyter Notebook and started with this code block:
import ipywidgets
from ipyfilechooser import FileChooser
chooser = FileChooser(
accept='.csv', # only show CSV files
multiple=False, # allow only one file
# optional starting path
start_path=r'\\sglx-srv1\geschaeft\MESSPLATZ'
)
display(chooser)
After executing this block a chooser widget appeared and I could select a folder or file, also from a UNC path (network drive).
But the setting of the start_path was always ignored and the dialogue always started from the folder the notebook itself is stored in.
I tried any variant I could find to quote the path, but with no luck.
It was not possible to let the file chooser start from any specified path neither local nor UNC.
What is wrong here? Is it a bug in my code or elsewhere?

Using Atom with Hydrogen. The working directory for Atom is one folder up from the current file's location. How do i change it to the current folder?

In both Hydrogen and using a plug-in terminal platform, the default directory is one folder up from where my code file resides.
E.g., I’ll be working on a file with path, say, parent/code/file.py. When I run pwd in the plug-in's terminal or the equivalent via Hydrogen in the python script I get parent/, but I need it to be parent/code/ to import files etc.
Perhaps the default directory for Atom is the project that is folder of the project that is open?
Any ideas how to change the default current directory for Atom (or is it package specific) to the file i’m working on/executing in Hydrogen?
In the hydrogen settings you can choose the location where the kernel should be started.
The default is the 'First started project's directory'. You can choose 'Current directory of the file' there, which should give you what you want.

Chrome Cordova Translation Issue

Im trying to translate my chrome app to ios. I run the command cca create myapp --copy-from=manifest.json Some errors exist cp: copyFileSync: could not write to dest file (code=ENAMETOOLONG): $PATH_TO_PROJECT/project/platforms/ios/www/platform/..../www/platform/config.xml
where .... is 40 repeated directories of /www/platform
The translation succeeds and im able to run my project with android however, when trying to run in xcode i see an almost identical error saying File name too long.
Anyone have simmilar issues? Anyone able to get ios translations to run?
This is the result of a bug in the way we "import" your project when you specify --copy-from or --link-to arguments to cca create when your import directory is the same as your output directory.
Since the import in your example is a parent folder of the output, it copies itself over and over, recursively until the system kills the process. We have added protection against doing this (by warning during cca create if we detect this case), but that version hasn't released to npm just yet (sorry!).
The fix for the issue is simple: just create your project in another directory that is outside of your packaged app! i.e., either:
# From within packaged app directory
cca create ../myapp --copy-from=./manifest.json
or
# From outside packaged app directory
cca create myapp --copy-from=$APP_DIR/manifest.json

Sublime 2 can not find file in the path (LaTeX plugin)

I have a project in directory A and files that I use in all my projects are in directory B.
When I moved a .sty file from A to B, the main .tex file does not compile anymore.
The error is that the .sty file was not found. I am puzzled because:
Directory B is included in the path of the project.
I cleaned (deleted manually) all the auxiliary files used in the previous compilations.
I refreshed the project folders .
Did anyone had similar problems? Suggestions?
The file LaTeX.sublime-build, within the Sublime Text folder . . . /Packages/LaTeXTools, contains a $PATH for different operating systems.
For example, Sublime Text 2 on an OSX operating system, has a file located at ~/Library/Application Support/Sublime Text 2/Packages/LaTeXTools/LaTeX.sublime-build. The relevant line of code for a MacTeX TexLive 2012 installation is "path": "$PATH:/usr/texbin:/usr/local/bin",. The plugin LaTeXTools looks in that path for *.sty files that are a part of the TexLive installation. While it may be possible (under some circumstances) to place the *.sty files within the working directory of the *.tex file, this particular plugin looks to the path mentioned hereinabove. So one option would be to add additional locations to the $PATH to suit the needs of the user, or simply place the *.sty files within the path that is pre-defined by the plugin developer.

LuaEdit can't find module when Lua files all in the same folder

I downloaded LuaEdit to use as an IDE and debug tool however I'm having trouble using it for even the simplest things. I've created a solution with 2 files in it, all of which are stored in the same folder. My files are as follows:
--startup.lua
require("foo")
test("Testing", "testing", "one, two, three")
--foo.lua
foo = {}
print("In foo.lua")
function test(a,b,c) print(a,b,c) end
This works fine when in my C++ compiler when accessed through some embed code, however when I attempt to use the same code in LuaEdit, it crashes on line 3 require("foo") with an error stating:
module 'foo' not found:
no field package.preload['foo']
no file 'C:\Program Files (x86)\LuaEdit 2010\lua\foo.lua'
no file 'C:\Program Files (x86)\LuaEdit 2010\lua\foo\init.lua'
no file 'C:\Program Files (x86)\LuaEdit 2010\foo.lua'
no file 'C:\Program Files (x86)\LuaEdit 2010\foo\init.lua'
no file '.\foo.lua'
no file 'C:\Program Files (x86)\LuaEdit 2010\foo.dll'
no file 'C:\Program Files (x86)\LuaEdit 2010\loadall.dll'
no file '.\battle.dll'
I have also tried creating these files prior to adding them to a solution and still get the same error. Is there some setting I'm missing? It would be great to have an IDE/debugger but it's useless to me if it can't run linked functions.
The issue is probably that your Lua files are not on the path in package.path (for C files this is package.cpath).
My guess is that the LuaEdit program is not launched in the directory you have your files in, and hence does not have a match for eg .\foo.lua.
You have 3 simple solutions to this (from dumb to smarter):
Find out what path LuaEdit considers as ./ and put your files there.
Open up a terminal in the right directory (the one containing your files), and run LuaEdit from there.
Add the path the files are on to package.path and package.cpath before doing any require's
You may need to put:
lua
package.path = package.path..";c:/path/to/my/files/?.lua"
at the beginning of your files before any require (as jpjacobs indicated). I couldn't find any way to provide this from LuaEdit itself. It appears it runs the script using its full path, but keeps its current dir set to whatever it was when the IDE was started. If you run LuaEdit using full path from your application folder, it should work fine even without package.path changes.
While the IDE itself works fine with its own modules/libraries, it doesn't mean it makes them available to the application it runs.
(shameless plug) If you're still not happy with LuaEdit, I'd offer ZeroBrane Studio Lua IDE as an alternative, It's based on the same wxLua framework, but provides a bit more functionality and doesn't have this particular issue you're facing. It also supports remote debugging, so you should be able to debug your Lua scripts directly from your application.

Resources