I am working through Practical Maya Programming, and trying to set a 'development root' on my PC, I have followed the instructions (below) exactly but it is not working - At the point where I type 'mayapy.exe' I get the warning "'mayapy.exe' is not recognized as an internal or external command, operable program or batch file."
From the book:
Let's decide where we will do our coding. We'll call this location the development root for the rest of the book. To be concise, I'll choose C:\mayapybook\pylib to house all of our Python code.
Create the development root folder, and inside of it create an empty file named minspect.py.
Now, we need to get C:\mayapybook\pylib onto Python's sys.path so it can be imported. The easiest way to do this is to use the PYTHONPATH environment variable. From a Windows command line you can run the following to add the path, and ensure it worked:
> set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
> mayapy.exe
>>> import sys
>>> 'C:\\mayapybook\\pylib' in sys.path
True
>>> import minspect
>>> minspect
<module 'minspect' from '...\minspect.py'>
EDIT
This is how it is working for me at the moment:
PS C:\Users\Me> set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
C:\mayapybook\pylib : The term 'C:\mayapybook\pylib' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:29
+ set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\mayapybook\pylib:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
So the code from the book is not working but the code from the post by DrHaze seems to:
PS C:\Users\Me> setx PATH "%PATH%C:\mayapybook\pylib\"
SUCCESS: Specified value was saved.
But when I run the Maya Python Interpreter and check if C:\mayapybook\pylib\ is in sys path it returns false:
>>> 'C:\\mayapybook\\pylib' in sys.path
False
This error "'mayapy.exe' is not recognized as an internal or external command, operable program or batch file." means that the path where mayapy.exe is located is not included in the PATH environment variable. Your system tries to look in all the folders included in the PATH variable but can't find an executable called mayapy.exe.
The executable mayapy.exe is generally located here:
C:\Program Files\Autodesk\Maya(VERSION)\bin\mayapy.exe
on my computer it's located here: C:\Program Files\Autodesk\Maya2014\bin\mayapy.exe
To add the mayapy.exe location to your path, use one of the following commands:
setx PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\" if you
want to change it permanently
set PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\" only works for the current instance of the cmd session.
EDIT
The error you show in your edit is the source of the problem. Windows failed to set the environment variable PYTHONPATH. Hence, when you execute 'C:\\mayapybook\\pylib' in sys.path it returns False. sys.path is in fact containing the value of PYTHONPATH. That's why it returns False.
Now, why did it fail to set this environment variable?
First I can see that you are using Windows Powershell, keep this in mind.
The command I gave you is:
set PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\"
You wrote:
set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
What it should be:
set PYTHONPATH "%PYTHONPATH%;C:\mayapybook\pylib\"
The syntax is a bit different and this last command should work.
As an explaination, your book gives you some commands to type in the vintage/old-style windows terminal: cmd.exe
As you are using Windows Powershell, some commands might have a different syntax.
Now what you can do is:
Use cmd.exe (Right click on the title bar -> Properties to custom it)
Use Powershell but keep in mind that the syntax might be a bit different than in your book
If you are using Powershell, there are different commands and strategies for managing environment variables.
You can set a variable permanently with SetEnvironmentVariable
You can set for the current shell session with: $env:VARNAME =
VARVALUE
You can put the commands to set variables in a powershell profile
file.
I would go with the third option. All three are detailed below:
Option 1. To append the directory "C:\mayapybook\pylib\" to the existing
PYTHONPATH permanently for your account:
[Environment]::SetEnvironmentVariable("PYTHONPATH", $env:PYTHONPATH +";C:\mayapybook\pylib\", "User")
Option 2. To append the Maya bin folder to your PATH for only the current
shell session:
$env:PATH += ";C:\Program Files\Autodesk\Maya2016\bin\"
Option 3. Create a powershell profile and set your env vars there.
First you'll need to make sure powershell scripts can run locally:
Hit the windows button, start typing powershell, right click and open as administrator. Enter:
Get-ExecutionPolicy
If it is says Restricted or AllSigned, set it to RemoteSigned like so:
Set-ExecutionPolicy RemoteSigned
Close that shell. Now in another powershell (not admin) type:
cd ~\Documents
md WindowsPowerShell
cd WindowsPowerShell
New-Item -path "profile.ps1" -type file
notepad.exe profile.ps1
Paste into the file any commands you want to run whenever a new powershell is opened:
Write-Host "Hello From Your Profile"
$env:PATH += ";C:\Program Files\Autodesk\Maya2016\bin\"
$env:PYTHONPATH += ";C:\mayapybook\pylib\"
Now whenever you open a powershell, you'll get a silly message and those paths will be set. You can test by typing:
Write-Host $env:PATH
or to list all env vars:
Get-ChildItem Env:
You should now be able to run commands from the maya bin directory. For example, type: maya to start maya.
Some other useful powershell env var commands here.
Related
I thought it will be as simple as adding these locations to Path or PYTHONPATH. I added them to PYTHONPATH and added PYTHONPATH to Path.
When running SET of window's terminal I can see my newly set paths;
E:\Tests> SET
Path=E:\Tests\PythonTests
PYTHONPATH=E:\Tests\PythonTests
(I simplified the list for readability)
I then create a very simple python file test.py inside E:\Tests\PythonTests with a single line:
print ("Hello world")
Now, if I cd \Tests\PythonTests I can run it successfully:
E:\Tests\PythonTests> python test.py
Hello world
If I cd \Tests I can:
E:\Tests> python pythonTests/test.py
Hello world
But if I try
E:\Tests> python test.py
python: can't open file 'test.py': [error 2] No such file or directory
Python version:
E:\Tests\PythonTests>python --version
Python 3.8.0
Am I'm missing something? What am I doing wrong?
The PYTHONPATH env var does not control where the python command searches for arbitrary Python programs. It controls where modules/packages are searched for. Google "pythonpath environment variable" for many explanations what the env var does. This is from python --help:
PYTHONPATH : ':'-separated list of directories prefixed to the
default module search path. The result is sys.path.
Specifying a file from which to read the initial Python script is not subject to any env var manipulation. In other words, running python my_prog.py only looks in the CWD for my_prog.py. It does not look at any other directory.
I have some Python code, developed for Linux, from a repository that I would prefer not to change.
The code uses the subprocess Python module to call a command line tool named colmap.
To get this code working under Windows, I installed colmap, added the bin folder containing colmap.exe to the PATH as well as the parent folder which contains the file COLMAP.bat. I then also had to change the Python code to run colmap.bat instead of colmap via subprocess.run.
The latter step I would like to avoid, as it would make the Python code incompatible with Linux/Mac and I would like to avoid adding an OS check via platform.system().
Is there a way to add colmap.exe to the PATH, so that it can be called via colmap by cmd.exe?
Like I said, the bin folder is already on the PATH. I already unsuccessfully tried to create a shortcut to COLMAP.bat with the name colmap as well as renaming COLMAP.bat to colmap.
This is the content of the batch file:
set SCRIPT_PATH=%~dp0
set PATH=%SCRIPT_PATH%\lib;%PATH%
set QT_PLUGIN_PATH=%SCRIPT_PATH%\lib\plugins;%QT_PLUGIN_PATH%
set COMMAND=%1
set ARGUMENTS=
shift
:extract_argument_loop
if "%1"=="" goto after_extract_argument_loop
set ARGUMENTS=%ARGUMENTS% %1
shift
goto extract_argument_loop
:after_extract_argument_loop
if "%COMMAND%"=="" set COMMAND=gui
"%SCRIPT_PATH%\bin\colmap" %COMMAND% %ARGUMENTS%
if "%COMMAND%"=="" set COMMAND=gui
set "colmapexe="
for /f "delims=" %%b in ('where colmap.exe') do if not defined colmapexe set "colmapexe=%%b"
if defined colmapexe ("%colmapexe" %COMMAND% %ARGUMENTS%) else echo Colmap.exe not found&pause
should work by locating colmap.exe and installing it in the variable colmapexe
#!/usr/bin/python
import requests, zipfile, StringIO, sys
extractDir = "myfolder"
zip_file_url = "download url"
response = requests.get(zip_file_url)
zipDocument = zipfile.ZipFile(StringIO.StringIO(response.content))
zipinfos = zipDocument.infolist()
for zipinfo in zipinfos:
extrat = zipDocument.extract(zipinfo,path=extractDir)
System configuration
Ubuntu OS 16.04
Python 2.7.12
$ python extract.py
when I run the code on Terminal with above command, it works properly and create the folder and extract the file into it.
Similarly, when I create a cron job using sodu rights the code executes but don't create any folder or extracts the files.
crontab command:-
40 10 * * * /usr/bin/sudo /usr/bin/python /home/ubuntu/demo/directory.py > /home/ubuntu/demo/logmyshit.log 2>&1
also tried
40 10 * * * /usr/bin/python /home/ubuntu/demo/directory.py > /home/ubuntu/demo/logmyshit.log 2>&1
Notes :
I check the syslog, it says the cron is running successfully
The above code gives no errors
also made the python program executable by chmod +x filename.py
Please help where am I going wrong.
Oups, there is nothing really wrong in running a Python script in crontab, but many bad things can happen because the environment is not the one you are used to.
When you type in an interactive shell python directory.py, the PATH and all required PYTHON environment variable have been set as part of login and interactive shell initialization, and the current directory is your home directory by default or anywhere you currently are.
When the same command is run from crontab, the current directory is not specified (but may not be what you expect), PATH is only /bin:/usr/bin and python environment variables are not set. That means that you will have to tweak environment variables in crontab file until you get a correct Python environment, and set the current directory.
I had a very similar problem and it turned out cron didn’t like importing matplotlib, I ended up having to specify Agg backend. I figured it out by putting log statements after each line to see how far the program got before it crapped out. Of course, my log was empty which tipped me off that it crashed on imports.
TLDR: log each line inside the script
I know this looks like a question answered thousands of time, but none of the traditional answers concerning the environment PATH are working.
I want to run the following in Windows 7, in Powershell:
python mycode.py
However, Powershell returns an error, stating that python not recognized as an applet, function, application,...
These are my path variables:
Users variables:
PATH
C:\Python27\Lib\site-packages\PyQt4;
C:\Python27;
C:\Python27\DLLs;
C:\Python27\Scripts;
C:\Python27\Lib\site-packages\vtk;
C:\Python27\gnuplot\binary;
C:\Program Files (x86)\pythonxy\SciTE-3.3.2-3;
C:\Program Files (x86)\pythonxy\console;
C:\MinGW32-xy\bin;
C:\Program Files (x86)\pythonxy\swig;
C:\Program Files (x86)\pythonxy\gettext\bin
PATHEXT
.PY;.PYW
PYTHON_INCLUDE
C:\Python27\include
PYTHON_LIB
C:\Python27\libs\python27.lib
and System variables:
PATH
C:\Python27\Lib\site-packages\PyQt4;
C:\Python27;
C:\Python27\DLLs;
C:\Python27\Scripts;
C:\Python27\Lib\site-packages\vtk;
C:\Python27\gnuplot\binary;
C:\Program Files (x86)\pythonxy\SciTE-3.3.2-3;
C:\Program Files (x86)\pythonxy\console;
C:\MinGW32-xy\bin;
C:\Program Files (x86)\pythonxy\swig;
C:\Program Files (x86)\pythonxy\gettext\bin;
C:\WINDOWS\system32;
C:\WINDOWS
I tried the following:
$env:Path = $env:Path + ";C:\Python27\"
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
without success. (I tried starting a new Powershell session, of course, and even tried to reboot my PC)
Could it be that PowerShell doesn't properly read the path variable, or I don't have some permission? I am lost, especially knowing that this work fine on another Windows 7 install. I note that typing:
python.exe
...opens a Python terminal as expected.
Edit : Ok I tried the following test.py code :
# -*- coding: utf-8 -*-
print "Hello"
input()
python.exe test.py
open a new terminal with "Hello" in it and wait for my input
but I don't want that, I expect the normal behaviour, with "Hello" printed in PowerShell, error message in PowerShell and so on.
Edit2 : I noticed that the "Path" variable given in PowerShell by:
Get-ChildItem Env
Is not equal to the one in the W7 options "System -> Advanced System settings -> Environment variable" . it was only :
;C:\Python27
Like if my previous command line
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27")
just wrote in it. Fixing this didn't solve my problem either.
Running any executable using Powershell (not just Python.exe) is possible using multiple approaches, but what has worked best for me is iex.
The basic steps I follow are:
Find the path to executable
Construct command string
Run iex on it. Use & to account for spaces in file paths.
To find the executable, I generally use get-command. This searches PATH:
if (get-command curl.exe) {
$exePath = "curl.exe"
}
$Cmd = '"'+ $exePath + '"' + ' args'
iex "& $curlCmd"
Hope this helps.
Adding "C\Python27" in "System -> Advanced System settings -> Environment variable" to the system variables solved my issue. For me it was only in user variables.
I have a batch file that's supposed to set PATH/PYTHON path variables and then invoke my python script (myscript.py), that's designed as an interactive console. I tried with the following:
#echo off
setlocal
if not defined PYTHONHOME (echo warning: PYTHONHOME environment variable is not defined. Using C:\Python24 by default.
SET PYTHONHOME=C:\Python24
if not exist "C:\Python24" ( echo warning: C:\Python24 does not exists. Please specify PYTHONHOME variable manually.))
color 1e
set PYTHONSTARTUP=%~dp0%myscript.py
set PYTHONPATH=%~dp0;%PYTHONPATH%
path %PYTHONHOME%;%PATH%
set PATHEXT=%PATHEXT%;.PY
cd %~dp0%
cmd.exe /k title Interactive Python Console 1.0
cls
%~dp0%myscript.py"
:done
endlocal
Before setting the colorpair (1e)for the console, I have appended directory containing myscript to path, python path and python24 is set as python home.
My problem is:
I am able to change the default font/background color of console, set the current window's title, but then neither cls (clearscreen) works, nor does my script is invoked. At the console, my pwd is script's directory. But when i enter 'python' at the prompt, myscript is invoked and I can see interactive console of my script.
Anything missing from batch, that would automatically clear off the console after setting color/title, and invoke myscript.py?
This doesn't really have anything to do with Python. cmd /k doesn't "set the window title", it starts a new command shell and leaves you in it, thus stopping your script midway through. Why don't you just do title My New Title? There's no need to use cmd.