add Python path to PATH system variable automatically under Windows - python

I'm creating one-click python installer (integrated with my application). Is there any way to force Python MSI installer to add python's path to SYSTEM PATH variable?
I'm using MSI installer because it is very easy to specify (using command line) how it should interact with the user.

User variables are stored in the Windows Registry under HKEY_CURRENT_USER\Environment
I would use winreg in a post install script to set or add to the PATH there.
http://docs.python.org/library/_winreg.html

There has to be a way, but what some people do is provide batch files that set up the environment before invoking Python. That's what BZR does, anyway. If you can write that batch file somewhere that's already normally in the path, so much the better.
If you're just worried about invoking Python, the normal Python installer does file associations, so you can work it that way.

Related

Install modules in Python locally to script

I have the following problem:
I have to run some test/diagnostic Python script on a Windows system. Due to explicit requirement, the system has no default system-wide Python instance, but there are two different Python instances installed, used locally by applications running on the system. However, both these instances lack some basic modules my script uses (like logging, urllib, configparser etc.).
I want to run %PYTHONPATH%\python.exe myscript.py where %PYTHONPATH% points to one of the installed Python instances, but install the required additional modules "somewhere" outside %PYTHONPATH% (preferrably, in the same directory where my script is installed) so that my script can use them.
As my script is a test tool, it should not modify the OS or installed software, so the Python installation under %PYTHONPATH% should not be changed in any way.
It is also expected that the installation can be fully automated, ie. the best way to install would be just have the modules in the same .zip file with my script which is unpacked onto the target path.
It is also important that the system has no Internet access, so I have to download required files on another machine and copy them to the target system.
Can you guide me how to do it?
I found an answer myself - it is quite simple:
obtain the zip file containing standard modules from the appropriate Python version distribution (in my case it was the file python38.zip, it is inside the main zip file downloadable from Python site)
Unpack the contents of this file to c:\mydir\Python38\site-packages, where c:\mydir is the directory containing my script
set the environment variable PYTHONUSERBASE=c:\mydir before running my script
Now I can run the script and it finds all "missing" standard modules in c:\mydir\Python38\site-packages.
I think what youy are seeking for is a python virtual environment.
( internet needed )
Check :
https://docs.python.org/fr/3/library/venv.html
Then for the installation, you can creat a .exe file containing all dependencies.
(no internet needed)
Check:
https://www.pyinstaller.org/

Python path on windows

I have two installations of Python on a corporate Windows computer. One from the Anaconda distribution:
C:\Users\Me\AppData\Local\Continuum\anaconda3\python3.exe
Another one from a corporate installer:
C:\Users\Me\AppData\Local\Downloaded Apps\WinPython\python-3.4.3\python.exe
If I type "python" in the default "cmd" terminal or in the Git Bash, it says "command not found". Probably for the same reason, Jupyter does not allow me to create a Python 3 notebook. How do I set the paths so that Python is available system-wide? I prefer the Anaconda distribution's Python.
EDIT: Creating a new user variable PYTHONPATH and setting it to C:\Users\Me\AppData\Local\Continuum\anaconda3\ via Windows System Properties does not have an effect.
This answer describes step-by-step an approach that worked for me. However, as eryksun notes in the comment, the additional variable should not be named PYTHONPATH. I renamed it PYTHON, which works.
Strangely, adding the paths directly to the PATH variable did NOT work.
You have to add the path of your installation to the Environment variables.
Simply go to the System Properties / Environment Variables /
From there, create a new system variables and add your python path.
In the system variable section select the Path variable.
Add new variable C:\Users\Me\AppData\Local\Continuum\anaconda3\ as environment variable in your advanced System Settings.
This is from where your system will invoke the python interpreter.
For more details see this answer

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.

How to import modules from alternate locations when using Python IDLE?

I've been trying to figure this out for more than 2 days, screening the internet and the tutorial, but yet I don't have solved my problem. I'm a real newb and don't yet really know what I'm doing..
Software I use:
Mac OS X 10.6
Python v3.2.2
Interactive interpreter (IDLE)
Problem:
IDLE's default directory is /Users/ME/Documents/. Files with the extention .py can only be opened when located in this directory. However, I made a folder where I would like to save all the .py files etc that have to do with this software. Currently, IDLE cannot load .py files from the chosen directory by me.
What I did first was I added to IDLE:
import sys.
sys.path.append('Users/Mydir/')
sys.path
However, in an already existing thread from 2010 I read sys.path is for the Interpreter ONLY, and that if I am to change this I need to modify the PYTHONPATH environment variable:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
However, I'm confused how to use this and cannot find answers to my following questions:
1) PYTHONPATH (.py?) is already existing on my computer when I installed the program?
If YES, where is it? I cannot find it anywhere.
If NO, I need to create one. But where and what should be the content so that IDLE can load files from a non-default directory? Should it contain only the words in bold?
I hope I made my problem clear.
Cheers
It's not totally clear to me what you mean by load. That could mean Open and Close files in the IDLE editor. Or it could mean being able to use the Python import statement to load existing Python modules from other files. I'll assume the latter, that by load you mean import.
There are two general ways to launch IDLE on Mac OS X. One is from the command line of a terminal session; if you installed Python 3.2 using the python.org installers, by default typing /usr/local/bin/idle3.2 will work. The other way is by launching IDLE.app from /Applications/Python 3.2, i.e. by double-clicking its icon. Because you say the default directory for files is your Documents folder, I'm assuming you are using the second method because IDLE.app sets Documents as its current working directory, which becomes the default directory for *Open*s and *Save*s and is automatically added as the first directory on Python's sys.path, the list of directories that Python uses to search for modules when importing.
If you want to add other directories to sys.path, as you've noted you can use the PYTHONPATH environment variable to do so. The standard way to do this is to add an export PYTHONPATH=... definition to a shell startup script, like .bash_profile. However, if you use IDLE.app, no shell is involved so commands in .bash_profile have no effect.
While there are ways to modify the environment variables for OS X GUI apps, in this case, a simpler solution is to use the other method to invoke IDLE, from the command line of a shell session, using either /usr/local/bin/idle3.2 or, if you've run the Update Shell Profile command in the /Applications/Python 3.2 folder (and opened a new terminal session), just idle3. Then, a PYTHONPATH environment variable you set up will be inherited by that IDLE.
BTW, there is no direct way to modify the initial current working directory of IDLE.app from Documents other than modifying the code in IDLE. If you start IDLE from a command
line, it inherits the current working directory of the shell.
[UPDATE] But rather than fooling around with defining PYTHONPATH, here is another even simpler, and probably better, approach that should work with either IDLE.app or the command line idle. It takes advantage of Python path configuration (.pth) files and user site-package directories. Assuming you are using a standard Python framework build of 3.2 (like from a python.org installer) on Mac OS X, create a path file for the directory you want to permanently add to sys.path. In a terminal session:
mkdir -p ~/Library/Python/3.2/lib/python/site-packages
cd ~/Library/Python/3.2/lib/python/site-packages
cat >my_paths.pth <<EOF
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_1
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_2
EOF
Now, whenever you run that Python 3.2 or IDLE under your user name, the directories you have added to the .pth file will automatically be added to sys.path.
BTW, the exact path location of the user site-packages directory for versions of Python earlier than 3.2 or 2.7 may be slightly different. Also, on other Unix-y systems, the default location for the user site-package directory is ~/.local/lib/python3.2/site-packages.
PYTHONPATH is an environment variable (see here and here). I don't have a Mac, but from the threads I have linked to you would type something like
launchctl setenv PYTHONPATH=/Me/Documents/mydir:$PYTHONPATH
on the command line to allow you to run Python scripts from /Me/Documents/mydir. Alternatively, put this line in a file called .bashrc in your home directory (~) and this path will be set each time each time you open a terminal. See here for a short introduction to .bashrc and other .bash* files. Hope that helps.
EDIT See also this question.

Cygwin paths for Windows python

I have access to a machine with a minimal cygwin installation, and the Windows version of python. I need to run some python scripts there, however python requires Windows paths. I can use cygpath -w, on the arguments that I provide, however further unix/cygwin paths are included in numerous other scripts which are subsequently invoked.
Is there a way to tell Windows python to accept unix/cygwin paths?
No, Windows python has no suport for Cygwin paths, but Cygwin does have its own Python. If you can't add that to the existing Cygwin install, you might want to consider doing a user-specific Cygwin install into a directory that you're allowed to write to.
There is a way to obtain limited Cygwin path support for Windows programs, although I suspect this isn't an option for you either: install Cygwin into C:\, so that a Cygwin /path is equivalent to C:\path. This relies on the fact that the Windows API (albeit not all Windows programs) accepts both backslashes and slashes as path separators, and that it considers absolute paths without drive letter as referring to the system drive (i.e. C:).
Obviously this won't work for Cygwin paths that point to other drives (via the Cygwin mount table). It also won't work for any programs that use / (rather than -) to introduce options, which includes most built-in Windows command-line tools. But it does usually work for cross-platform tools such as Python.
Yet another option is to use MSYS instead of Cygwin, which is a fork of an old Cygwin version, whereby its most distinctive feature is that it automatically translates POSIX paths to Windows paths when invoking Windows programs. Note however, that this approach has its pitfalls too, because it isn't always clear whether an argument is a path or not. Hence, sometimes it will fail to translate a path or wrongly change an argument that isn't a path.
Create a file named wpython and save it somewhere for example in /bin:
#!/bin/bash
path=$1
shift # Remove filepath from other args to pass them further
python.exe $(cygpath -w $path) $# # Call python.exe with Windows path and passed args
So then use it like this:
#!/usr/bin/env wpython
print(123)
Don't forget to make both file executable with: chmod +x filename
P.S.: Soultion based on this blog post.

Categories

Resources