I have a python module that defines some paths when imported. I want those paths to be readable by scripts in c++ and shell.
One option would be to make a text file with those paths, and write a reader in python, c++ and shell, and then start all my scripts with the respective reader, but it's a solution I dislike. I would be constantly opening the text file, and it could give me issues if more than one script tries opening it at the same time.
Another solution would be to have the python file define the paths as environment variables instead. The usual way to do environment variables in python, os.environ, only lasts until I close python. Could anyone tell me how to do this?
(Defining these variables in my .bashrc would be quite the headache, because the module will also be used by other people, and the paths have to be changed from time to time.)
Any other way to solve this problem would also be appreciated.
Related
I'm taking over a project from a coworker that involves several extensive SAS process flows. I have all the files with all the same names and a copy of the process flows they used. Since the file paths in their processes are direct references to their computer, normally I would just re-import the files with the same output names and run the process from there. In a few cases I would have to recreate a query builder as I'm using a few .sas7bdat files from another project.
However, there are quite a few files involved and I may end up having to pass this to another coworker in a few months, and since I can't get a good look at exactly what the import task is doing I'm concerned I may have some of the variables imported incorrectly. Is there an easy way to just change the file path the import or other task refers to?
Given the updates in comments, there's two possibilities I see.
If the paths you're changing are, or can be, relative to the location of the EGP, then you can right click on the Project->Properties->File References and check "Use paths relative to the project...", which means instead of storing a file in c:\my EGP folder\my code folder\code.sas it would store it as my code folder\code.sas. So then if the whole project moves to another computer (or just any other folder) then it automatically has the right path. This is mostly useful for code or similar things.
Otherwise, you're going to have to convert things to SAS code modules. There you can use macro variables to define the locations of things.
I want to embed Python 3.x in our C++ application to allow scripting of maintenance and other tasks. It so far does everything we need including file manipulation.
The problem is that to meet some specifications (like PCI), we aren't allowed to arbitrarily run shell commands such as with subprocess.call or popen.
Is there a way to prevent and similar calls from working in embedded Python?
One option is to remove all the modules that allow running arbitrary shell commands, i.e.: subprocess.py*, os.py*... and include only the modules that the end users are allowed to have immediate access to.
Unless your application is really locked down I don't think you can prevent someone from loading their own python module from an arbitrary directory (with import) so I don't think you can prevent execution of arbitrary code as long as you have python embedded.
After defining variables, functions, etc., can you save what you have done on the REPL too an text .clj file?
most people work with the repl through an editor such ad Eclipse/Emacs/vim and that editor has the ability to save the repl, though without some diligence on the developers part this will likely be an incomplete record of what happened. Some of the state of the repl may have come from loading files etc which will be in a different state.
So the short answer is typically not.
In Linux (mine = Ubuntu 16.04.2 LTS) if you are using lein then check for .lein (hidden directory) and look for repl-history. You should find the commands that you have typed or pasted into the REPL. This can be a source for later edit - I use geany...
I am answering the parenthetical part of your question. For me, the Clojure REPL is very useful for testing functions and proving out concepts that take no more than a few lines. I will often put hooks in a module that is not the main, just so I can load a file and run it through a couple of functions. I can also do this from main using the same mindset; that is write a debug function.
I found the Eclipse plugin to be quite useful, but I do not use it much these days, mostly Vim and running the module with one or more special functions and running the main. I don't know of any way to save REPL state.
this question might have been asked before, but I could not find it.
I am on a Linux box. I have py app that runs from a folder called /avt. (example)
I did not write this code, and it has about 12 modules that go with it. I was the lucky engineer to inherit this mess.
this app imports other modules that live under this dir /avt/bin
I want to be able find my modules in the /bin dir no matter where the current working dir is. sometimes the app changes dir to some other sub folders to perform some file I/O. Then should return, but seems like sometimes it does not make it back, because the code will error out with "no such file or directory" error. so I want to test for working dir each time before I do any file I/O to the /bin dir.
As an example, I want to create files in /bin, and then later open those files and read data from them. How can I test to make sure my current working dir is always /avt? and if it is not, then ch.dir to it? Note: it also has to be portable code meaning if must run on any directory structure on any Linux machine.
I tried this code, but it is not very clean I think. Python is not my main language. Is this coding proper and will it work for this? forgive me I don't know how to format it for this forum.
Avtfolder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if Avtfolder not in sys.path:
sys.path.insert(0, Avtfolder)
if Avtfolder.__contains__('/avt'):
modfilespath = Avtfolder + '/bin'
print 'bin dir is ' + modfilespath
else:
print 'directory lost...'
#write some code here that changes to the root /avt dir
I have a few notes.
First, I'm afraid you are mixing up two problems (or I couldn't tell from the question which one you're facing). These problems are:
I/O to files that can reside in different directories on different machines
Importing Python modules used by your app that can also be in slightly different locations.
The title of the question and some of the text suggests you're dealing with problem 2, whereas references to I/O and "no such file or directory" error point to problem 1.
Those are, however, separate problems and are treated separately. I won't be able to give the exact recipes on both, but here are some suggestions:
For problem 1: I don't think it's a good idea to do some I/O, create files, etc. in the folder where the user installs the Python libraries. It's a folder for Python modules, not data. Also, if the library is installed via setup.py, using pip or easy_install (if it isn't the case now, that can change in the future) then the program will probably habe insufficient permissions to write there, unless invoked as root. And that's right. Create files somewhere else.
As to "how to track the directory changes" part: I must confess I don't quite understand what you mean. Why do you even using the concept of "current directory"? In my mind you should just have some variable such as write_path, data_path, etc. and the code would be
data = open(os.path.join(data_path, 'data.foo'))
dump = open(os.path.join(write_path, 'dump.bar'), 'w')
etc.
Why do you even care where are your libraries located? I don't think it's right, I'd change that. This inspect.currentframe() stuff smells like you really need to rethink the design of the library.
Now, what the location of the libraries matters for is Problem 2. But again, the absolute path shouldn't matter (if it does, change that!). You only need all the modules to be inside one folder (or its subfolders). If they are in the same folder, you're good. import foo will just work. If some are in subfolders, those subfolders should have a file named __init__.py in them, and then they will be seen as modules by Python interpreter, so you'll be able to do from foo import bar, where foo is a subfolder with __init__.py and bar.py in it.
So, try to rewrite it so that you don't depend on where the .py files are. You really shouldn't need to use inspect there at all. On another note, don't use special methods like __contains__ directly unless you really need to. if '/avt' in Avtfolder will do the same.
My problem is that I'm having trouble specifying paths for Lua to look in.
For example, in my script I have a require("someScript") line that works perfectly (it is able to use functions from someScript when the script is run standalone.
However, when I run my app, the script fails. I believe this is because Lua is looking in a location relative to the application rather than relative to the script.
Hardcoding the entire path down to the drive isn't an option since people can download the game wherever they like so the highest I can go is the root folder for the game.
We have XML files to load in information on objects. In them, when we specify the script the object uses, we only have to do something like Content/Core/Scripts/someScript.lua where Content is in the same directory as Debug and the app is located inside Debug. If I try putting that (the Content/Core...) in Lua's package.path I get errors when I try to run the script standalone.
I'm really stuck, and am not sure how to solve this. Any help is appreciated. Thanks.
P.S. When I print out the default package.path in the app I see syntax like ;.\?.lua
in a sequence like...
;.\?.lua;c:...(long file path)\Debug\?.lua; I assume the ; means the end of the path, but I have no idea what the .\?.lua means. Any Lua file in the directory?
You can customize the way require loads modules by putting your own loader into the package.loaders table. See here:
http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders
If you want to be sure that things are nicely sandboxed, you'll probably want to remove all the default loaders and replace them with one that does exactly what you want and nothing more. (It will probably be somewhat similar to one of the existing ones, so you can use those as a guide.)