using a "temporary files" folder in python - python

I recently wrote a script which queries PyPI and downloads a package; however, the package gets downloaded to a user defined folder.
I`d like to modify the script in such a way that my downloaded files go into a temporary folder, if the folder is not specified.
The temporary-files folder in *nix machines is "/tmp" ; would there be any Python method I could use to find out the temporary-files folder in a particular machine?
If not, could someone suggest an alternative to this problem?

Python has a built-in module for using temporary files and folders. You probably want tempfile.mkdtemp().

Perhaps the tempfile module?

Related

How are the .exe files in Scripts folder for python generated?

I don't know if my question is ambiguous or not but, I noticed that in Scripts folder inside the python installation folder there are executable files. Each file about a 100kb in size.
FYI: when I open it (or them) using 7Zip I often find a init.py file inside.
Thanks
I have tried researching but can't seem to find the answer.
The setuptools package builder is able to
Automatically generate wrapper scripts or Windows (console and GUI) .exe files for any number of “main” functions in your project. (Note: this is not a py2exe replacement; the .exe files rely on the local Python installation.)
(ref. from setuptools documentation)
Unfortunately the way it is actually done is considered an implementation detail and is not documented.

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:

How to package a Pycharm python project for upload to AWS Lambda?

Using pycharm on Windows.
I have created a zip file for upload to AWS Lambda the manual way:
1) Install the modules manually into a directory other than the default directory.
2) Create my .py code file
3) Zip the contents of the project folder
4) Upload that zip folder to Lambda
I am new to Pycharm and with a project I see that there are a whole bunch of files and folders that I do not understand.
I tried to zip the entire Pycharm project contents and upload - that did not work. It looks like I need to run some kind of setup that creates the proper folder structure and files that have the correct content.
Any help would be appreciated.
For all those still stuck with this, I have a few suggestions which could possibly resolve the issue altogether:
Use pip's -t option to specify the Application Directory
Using Pip's -t option, one can specify the Application directory. It's better than using the pycharm's package installer, as we can specify the installation directory with this.
Zip the complete Application directory (Answer's your question)
Go inside your Pycharm project directory -> select all -> Right Click -> send to compressed (zip). This may result in the inclusion of some unneeded directories (__pycache__, .idea), but would not affect the program execution. If needed, you may skip those two directories while creating the zip.
I believe you were zipping the project directory, rather than compressing the contents of the Project directory.
As I also answered here Jetbrains now offers the AWS Toolkit which allows local and remote development of Lambda functions.
Despite some lingering issues it works quite well. Still finding my way with it.
It includes packaging and deploying.
Toolkit page on Jetbrains website

Python: wrapper for C++ requires access to .dll files

I made a Python wrapper for an SDK written in C++ using SWIG and built using Microsoft Visual Studio 2010 in Windows 7.
It's been a success, with the module.py and _module.pyd being generated. However, this wrapper heavily depends on a couple .dll files located in the SDK folder outside PythonXY folder.
After I moved the module.py and module.pyd into PythonXY/Lib/site-packages/, and tried running import module, the following error occurs:
ImportError: DLL load failed: The specified module could not be found.
Then, I tried to change the Python working directory to the directory where the .dll files exist, using os.chdir() method from Python module os. This time, running import module output no error. I continued to access the wrapped C++ classes and functions and they worked just fine.
My questions are:
How can I "link" the path containing the .dll files so whenever I want to use module, I don't have to change the working directory?
Is there any way to append additional working directory aside from Python default working directory from within the Python source file? (i.e. not from PATH configuration on Windows)
Is there any way I can link the wrapper with those .dll files dynamically? Meaning that let say the files are copied to another machine and the .dll files are stored in different absolute path directory, the import module would still work?
The point is I want to make this wrapper as portable as possible, across multiple Windows machine.
Thank you.
I found a solution to let module.py find _module.pyd.
Basically, create a new file called module.pth and save it in the same directory as module.py. In this case, the directory is PythonXY/Lib/site-packages/.
Let say you place your _module.pyd inside a directory called _modulelib (PythonXY/Lib/site-packages/_modulelib/), then the content of module.pth should be like this:
_modulelib

Noob Question: How to import some imodule in Python?

I am using Python 2.5 on CentOS 5.5
I have got a file called MultipartPostHandler.py, I am supposed to use it like this:
import MultipartPostHandler
But I dont know where should I put the file MultipartPostHandler.py so I can use it. Thanks a lot!
http://docs.python.org/tutorial/modules.html#the-module-search-path
When a module named spam is imported,
the interpreter searches for a file
named spam.py in the current
directory, and then in the list of
directories specified by the
environment variable PYTHONPATH. This
has the same syntax as the shell
variable PATH, that is, a list of
directory names. When PYTHONPATH is
not set, or when the file is not found
there, the search continues in an
installation-dependent default path;
on Unix, this is usually
.:/usr/local/lib/python.
Actually, modules are searched in the
list of directories given by the
variable sys.path which is initialized
from the directory containing the
input script (or the current
directory), PYTHONPATH and the
installation- dependent default. This
allows Python programs that know what
they’re doing to modify or replace the
module search path.
So, you have to put it into the current directory or use sys.path to show your program where the searched module is.
The easiest is to put it in the same folder as the code you're writing.
If you got it from somebody who provides an installer, then to be safe, just run the installer.
If it's just something you grabbed specifically for this project, put it in the same folder as the .py files that you're writing.
Otherwise (if you're planning to use it for a few projects and don't want to copy/paste the file), the safest place to put it is probably in the /lib/site-packages sub-directory of the directory where Python is installed.
The easiest - yes, is to put it in the same directory where you have the importer python code. Also, you can export PYTHONPATH environment variable which points to the directory (or directories separated by :) where you have MultipartPostHandler.py. Another option: make it distributable. This will be useful if you are going to publish your code.

Categories

Resources