Installing file in parent directory using scons - python

After my build is finished, I want to copy the file to another directory. It works fine for child directories, e.g.
Install("temp", "test_file.txt")
where temp is the name of the child directory and test_file.txt is file I want to copy. But I haven't been able to do so for parent directories. Here's my MnWE SConstruct file:
Install("..\\temp", "test_file.txt")
Note that I am using Windows 10 in case it matters.
I have found some information in the docs, but I am unable to translate it to a working example on my machine.

Related

Where is the python workspace path located in PyCharm?

I created a Python project using PyCharm (2019.3.3) in folder F:\FolderA then I realized I meant to create it in F:\FolderB. So I copied my project from FolderA to FolderB. Now when I try to install a package, PyCharm says:
Cannot start process, the working directory 'F:\Projects\FolderA\MyProject\venv\Scripts' does not exist
I come from C# background, and everything project related is saved in .csproj file. Does Python have a similar file where it writes project/workspace information? So I can point to the correct path?
Inside /venv/scripts folder there are three "Activate" files. It holds the path to the old directory.
Replace the old paths with new paths
Restart PyCharm
Install package
Here's a screenshot of the path to the files:

Pyinstaller onedir option - exe file outside the directory

Recently I was experimenting with pyinstaller to create an executable file from my Python script. Everything works as expected.
I tested two options: --onefile, which takes quite a long time (like 20-30sec) to start because it depacks everything into a temporary directory.
The --onedir option is much faster (4sec) to start but it's not very comfortable to use. When I move exe file outside this directory program no longer works.
My question is: is there a possibility to make the exe file point to this directory location? I want to keep all the pyinstaller files in one place and allow users to have the exe file in any location they want.
Thanks for help.
Let's just see a real-life production case. Whenever you download say a pirated game, or and original copy of software, generally they are compressed together. When you unzip them, a new folder is extracted and inside that folder there are a lot of other folders. What you do to run the software is you simply double click the .exe file.
Your situation is the same. If you move the exe file outside the original extracted folder then it simply doesn't work. So, the work around way is to create a shortcut to the exe file.
Hope this clarifies your doubt :)
Shortcut. Create .exe shortcut. This way original .exe will be still in parent directory but shortcut can be placed anywhere
Most of the answers are about creating shortcuts, but its not the true solution. What we want is a clean folder having one dir and the exe outside that dir.
Unfortunately this is not possible at the moment. This issue is there since 2010 and was not fixed till date. Here is the link to that issue:
https://github.com/pyinstaller/pyinstaller/issues/1048
All they say is to create your own bootloader.
Nobody was able to give the PR for that.
I also found a blog on separating the exe from onedir with a hook file. I tried this but was unsuccessful with the latest version of Pyinstaller.
At last you have to do something hacky.
I found a way to do this:
Make a folder Modules in the same directory where executable is present.
Copy and paste all the heavy modules inside that folder.
Add the search path for that module folder inside your program:
# add this code in the top (before imports)
if getattr(sys, 'frozen', False):
app_path = os.path.join(os.path.dirname(sys.executable),"Modules")
sys.path.append(app_path) # search this path for modules
Under pyinstaller option --exclude-module, write the names of all those modules you excluded.
Use the one-file option but don't pack any other assets like images. Add all those external assets/folders outside.
Add this option: --runtime-tmpdir "Temp". With this, the executable will unpack the required files in the same directory under a new folder "Temp".
That's all, now you will get a very small sized exe file with mainly two required folders "modules" and the "temp". The booting time will also increase, and it will look a lot cleaner.
I think you should have some other files which is being required by that exe file & hence when you move exe file outside of directory it's giving you error. One of the example can be that the exe program require chrome driver & you have placed it within that directory. If you move exe program outside then you need to place the chrom driver also in the new position. I hope it will help you to detect , otherwise we can use exe program anywhere if it does not require any dependency of other files.
A workaround allowing a clean folder structure for the user that only contains the main exe and the libs folder is to create a second Python program containing only one instruction : call the executable of the main Python program within the over-crowed folder with libs. The main program being distributed without the --onefile option, the execution remains faster.
The principle is simple : create a new Python project with a single script your_program_launcher.py with this content :
import os
if __name__ == '__main__':
os.chdir(".{0}your_program_folder".format(os.sep))
os.system("your_program.exe")
The script is very simple and limit itself to move on the main program folder (to avoid resource access problems) and call the main program executable.
You just have to distribute this launch program with --onefile and possibly an icon (pyinstaller -i icon.png --onefile your_program_launcher.py) but this script doesn't use any libs (except os) so this executable will be very light and its execution immediate. Then you will have to put this program in the parent folder so you get a clean folder with this launch executable and the folder containing libraries and main program exe, without using a .bat file which is less natural.
dist
| your_program_launcher.exe
|
|____your_program_folder
| |_____lib1
| |_____lib2
| |_____libn
| | your_program.exe
Since the file is still an executable that needs the files in the subfolder, the user won't be able to move this executable anywhere he wants, but at least the user won't be lost in the folder containing all the libraries.

Where is my _pycache_ folder and .pyc byte code files?

I'm running Python 3.4.1 on Windows 7 and thought that after running my .py script in the command line, a directory named _pycache_ would be created in the same directory that my script ran in. It is not there, even after I made sure that 'Show hidden files, folders, and drives' was checked. I looked around here and on Google but can't seem to get an answer that makes this visible.
Can someone help? I'm new to Python and would like to look over the byte code files.
The directory is called __pycache__ (with double underscores).
It'll only be created if Python has permission to create a directory in the same location the .py file lives. The folder is not hidden in any way, if it is not there, then Python did not create it.
Note that .pyc bytecode cache files are only created for modules your code imports; it is not created for the main script file. If you run python.exe foobar.py, no __pycache__/foobar.cpython-34.pyc file is created.

how to copy files to users home directory or current directory using distutils?

I am writing a python package (svnplot). I want to copy a shell script or batch files in users home directory or current directory, so that user can conveniently execute the commands in the package.
Currently to execute svnplot command, user have to call something similar to
python /svnplot/svnplot.py
If I copy a shell script or batch file in users home directory, then user just have to call
svnplot.sh
However, I am not able to figure out how to copy the files to users home directory.
Look at distutils scripts option or entry_points/console_scripts in setuptools.
If there is a single main command then you could use PyInstaller, py2exe to pack your package into a single executable. In simple cases it might be enough just to zip the package and execute that.

Hudson unable to navigate relative directories

I have a Python project building with Hudson. Most unit tests work correctly, but any tests that require writing to the file system (I have a class that uses tarfiles, for example) can't find the tmp directory I have set up for intermediate processing (my tearDown methods remove any files under the relative tmp directory).
Here is my project structure:
src
tests
fixtures (static files here)
unit (unit tests here)
tmp
Here is an example error:
OSError: [Errno 2] No such file or directory: '../../tmp'
I assume this is happening because Hudson is not processing the files while in the directory unit, but rather some other working directory.
What is Hudson's working directory? Can it be configured? Can relative paths work at all?
Each job in Hudson has it's own working directory, at /path/to/hudson/jobs/[job name]/workspace/
For individual jobs, you can set the "Use custom workspace" option (under "Advanced Project Options") to define where the workspace will be.
I guess it would depend on how your tests are being run, but if you inspect the job's workspace you should be able to find where Hudson is writing the files to.
I don't know how you're initializing your workspace, but typically it's done by checking your project out of version control into the workspace. If this is true in your case, the easiest thing to do is to add your tmp directory to version control (say, with a README file in it, if your version control system doesn't support directories). Then, the tmp directory will get checked out into your workspace and things should work again.
I don't know anyhting about Hudson, but this is what I do to ensure, that relative path are working right:
os.chdir(os.path.dirname(sys.argv[0]))

Categories

Resources