Path to python.exe - python

I created a script for hundreds of users. It includes this line:
os.execute("C:\\InstallPython\\python.exe "Path-to-current-folder-with-python-script")
This line is written in Lua language, but language doesn't matter.
User can install Python in any folder. It might be on Disc C or D. I don't know this.
How to create a universal version that suits all users?
Maybe I can get the path to python.exe by a program?

Check out this: https://www.lua.org/pil/22.2.html
Usually path to python is in PYTHON_PATH environment variable.
So you can try this in your Lua script before running the python script:
os.getenv("PYTHON_PATH")
Once you print this variable, you can see that it indeed includes the path where python was installed, the only thing you might do, is to add a python.exe to this string, as it might point to a directory, not to the python executable.
In my case PYTHON_PATH value is: C:\Python27\ when using Python2.7

import sys
sys.executable
Above should return the path to the executable.
Python Docs

Related

how to place ".py" file into the search path of your python installation

I have two files say Geometry.py and PeptideBuilder.py for their installation They need to be placed into the search path of my python installation on ubuntu linux machine.
how can I know the search path of my python installation and how can I place them .
If you use this for a project, update python path in your main script:
import sys
sys.path.insert(0, '.path/to/folder/with/scripts')
If you need them for any instance of python, you should have sudo rights and put them in site-packages(check first PYTHONPATH for your py version):
import sys
print(sys.path)
Usual, the standard module path is like „/usr/lib/pythonX.Y/site-packages”.
(that's standard in my Arch, but may be different on Ubuntu, that's way is better to check)
EDIT: if scripts are strictly related to your project, just puthem on same level with script that import's them, but I think you allready do that.

Where cgi script's "#!/usr/ " points to?

First of all I know The path points to the executable file. What I'm asking is in which directory it points?
I'm very new at python. I works on PHP, and now I'm giving Python a try.
I'm configuring my apache-2.2 with python. I have configured my cgi-bin directory. My problem is I installed Python2.7 on a very different location, and dont know how to point that "#!usr/" path to the exact location where python.exe is.
Thanks,
Kamil
The directory it points to is... /usr/? you define the path and THEN the executable, like so: #!/usr/bin/python3 where python3 is the executable and the path is /usr/bin/, standard python directory if you think about it.. most binaries are stored in /bin or /usr/bin.
I'm assuming you're rocking a windows machine tho since you mentioned .exe.
So do: #!E:/python-installed/python.exe -u
Some docs:
http://www.imladris.com/Scripts/PythonForWindows.html
Provided running python or python2 does the right thing (preferably test this as the user the cgi script will run as if you can), the convention is to use the program env - which can search $PATH for an executable instead of hard coding a path. env is a standard program on any Unix-like system, which is always in /usr/bin unless your base system is very strange, so you can do this:
#!/usr/bin/env python
According to this previous answer, this would cause the line to be ignored on a Windows machine, which may be what you want - it will cause Apache to revert to other ways to find an appropriate interpreter, which seems to include the value of a registry key (at HKEY_CLASSES_ROOT\.cgi\Shell\ExecCGI\Commandfor cgi files, and change the extension as appropriate), and likely includes Windows' standard lookup rules if that key is unset.

What is the purpose of the environmental variable PYTHONPATH

On windows 7, I currently don't have a python path. Can I safely make one? If so, how do I do it?
Upon making this variable, I can no longer load Spyder (IDE) without it crashing. Does anyone know why?
I would like to edit my existing python path if possible, but just don't know why it isn't already there in environmental variables.
I would ultimately like to be able to run "python myscript.py" and have myscript be in a different directory from the call directory.
PYTHONPATH adds new paths to the ones Python uses by default. The path in total determines where Python will look for modules when you import them.
Look at sys.path to see the combination of the defaults with your PYTHONPATH environment variable. It's likely that Spyder is loading a module that exists in two different places and the wrong one comes first.
When you import modules in python, python searches for the module in the directories in PYTHONPATH, in addition to some other directories.
In order to be able to run your script as > myscript.py, you want to put your script somewhere on PATH (here are some instructions for viewing or updating PATH), this is where the OS looks for scripts and programs when you give it a command. I believe that in windows the .py extension must be associated with python for windows to know that myscript.py should be run using python. This should happen automatically when python in installed, but maybe someone with more windows knowledge can comment on this.
it has role similar to path. this variable tells the python interpreter where to
locate the module files imported into a program. it should include the python source library directory and the directories contain in python source code

on Windows, virtualenv is not being used with i run a python program

I have virtualenv installed on windows.
In cmd, i run python and look at sys.path and see the virtualenv path included.
but when i run manage.py (for django), I don't see the virtualenv path,
so virtualenv is not working with django server.
Why?
the solution is to explicitly invoke python by using: python file.py
as described in http://www.velocityreviews.com/forums/t727997-problems-running-virtualenv-under-windows.html
for some reason, the python registered with .py in windows does not invoke virtualenv.
Virtualenv modifies the PATH to include a Python with the correct setup. It's a completely separate program from the system Python.
The PATH is used to look up programs by name: the first program of a given name that's in the PATH gets executed.
When you “run a file”, Windows uses the extension of the file to look up a program to run. It doesn't look it up by the name of the program, and so doesn't check the PATH.
The solution is to explicitly invoke Python from the command line (python manage.py) while a virtualenv is active. This way, Windows will search PATH for what you meant by “python”, and find the correct one.
have you done:
> source ../path-to/bin/activate
?

How do I use my standard python path when running python scripts from xcode macros

I'm trying to run Python scripts using Xcode's User Scripts menu.
The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which means I can't do things like
#!/usr/bin/python
import myScript
myScript.foo()
Where myScript is a module in a folder I've added to my path.
I can append a specific path to os.sys.path manually easily enough, but I have to do it in every single script for every single path I want to use modules from
Is there a way to set this up so it uses the same path I use everywhere else?
EDIT: After looking into things a bit more, it seems like scripts executed from Xcode use a completely different PATH than normal. The path I get by running a script in Xcode is:
PATH=/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin
and I'm sure my regular path doesn't have /Developer/usr/bin in it. Does anybody have any idea where this path is coming from?
On the mac, environment variables in your .profile aren't visible to applications outside of the terminal.
If you want an environment variable (like PATH, PYTHONPATH, etc) to be available to xcode apps, you should add it to a new plist file that you create at ~/.MacOSX/environment.plist.
See the EnvironmentVars doc on the apple developer website for more details.
A quick but hackish way is to have a wrapper script for python.
cat > $HOME/bin/mypython << EOF
#!/usr/bin/python
import os
os.path = ['/list/of/paths/you/want']
EOF
and then start all your XCode scripts with
#!/Users/you/bin/mypython
Just add the paths to sys,path.
>>> import sys
>>> sys.path
['', ... lots of stuff deleted....]
>>> for i in sys.path:
... print i
...
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC
>>> sys.path.append("/Users/crm/lib")
>>> for i in sys.path:
... print i
...
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC
/Users/crm/lib
>>>
I tend to use pth files. From the docs.
The most convenient way is to add a
path configuration file to a directory
that’s already on Python’s path,
usually to the .../site-packages/
directory. Path configuration files
have an extension of .pth, and each
line must contain a single path that
will be appended to sys.path. (Because
the new paths are appended to
sys.path, modules in the added
directories will not override standard
modules. This means you can’t use this
mechanism for installing fixed
versions of standard modules.)
So the simplest thing to do is to do the following:
echo "/some/path/I/want/to/add" > /Library/Python/2.5/site-packages/custom.pth
HTH
Forgive me if my answer seems ignorant, I'm not totally familiar with Mac and I also may have misunderstood your question.
On Windows and Linux, when I want to refer to a script I've written, I set the PYTHONPATH environment variable. It is what os.sys.path gets its values from, if I remember correctly.
Let's say myScript.py is in /Somewhere. Set PYTHONPATH to:
PYTHONPATH = /Somewhere
Now you should be able to "import myScript".
If you start doing sub-folders as python packages, look into usage of init.py files in each folder.
If you plan on re-using this and other scripts all the time, you should leave PYTHONPATH set as an environment variable.
Not sure if Xcode counts as launching the script through Finder or not, but if it does, apparently Finder doesn't read .profile or .cshrc files when it starts for a user the way Terminal does.
If your question is unanswered still, check out Apple's knowledge base: QA1067 and set up your environment using the plist.

Categories

Resources