Calling python compiled files in Python 3 - python

I have multiple Python versions installed (2.7 and 3.4)
I want to run a .pyc with specified version of Python
#! C:\python34\python
import sys
print("Hello",sys.version.split()[0])
input()
This sheebang works fine on Windows because I use pylauncher
So I can compile like that
c:\python34\python -m compileall print.py -b
But the sheebang is not recognized when I execute the pyc file.
This works, but I wouldn't like to repeat the C:\python34\python
Because the current script will be already running under the Python version I asked in the shebang.
Therefore I would like to make the sub program start with the same version of the Python.
So far, I tried:
#! C:\python34\python
import os
os.system("C:\python34\python print.pyc")
This would be perfect, but doesn't like pyc files. And the following doesn't works either:
exec( open('print.pyc').read() )
Does someone knows how to call the pyc files in the code?

#! C:\python34\python
import print # imports print.pyc
#now you can use the pyc as a module.
DoSomething()

Related

python gives ImportError: No module named "" when triggered in a perl program

In my perl program which runs the python script
I have provided the PYTHONPATH env param with the path for the lib and i have run the python script. I am getting
ImportError: No module named "....."
my $script = "/path/pythonscript.py";
$ENV{'PYTHONPATH'} = "/path/lib";
system("python $script");
Whereas when i run the same python script on command line in the same directory where the script executes in my perl program, it is working.
Can anyone give me some pointers on why this is happening.
Try printing the contents of sys.path and compare the difference e.g. change your python script to
import sys
print(sys.path)
Most likely there is a difference here and this is causing the module to not be found.
I once had a similar problem. I solved it by creating an executable script (chmod) and making that script run instead of the python script. The script simply contained a cd to the directory and a python3 program. py

python command line options to a compiled python file (py2exe)

I, like many python programmers find the command line arguments for python described here very useful. specifically "-i" which keeps the python interpreter running after a program finishes for a stack trace. How can I use these with an exe file compiled with py2exe? Not to be confused with regular argument parsing in an exe file. If you came looking for that, find it here.
My first thought was to try:
pyprogram.exe -i -foo -bar
but that didn't work.
it should be noted that
pyprogram.exe -foo -bar
does in fact work for me.
what I am looking for is the .exe equivalent of
python -i pyprogram.py foo bar
Failing to find an implementation that works for all of the python command line options, what could I do just to make the "-i" argument work? it is the most important to have as an option in my executable.
I did not find anything on the py2exe wiki about passing arguments like -i (to enter interactive mode after execution).
One might be able to discover information about the argument handling in the py2exe source files.
Update: It indeed looks like py2exe does not handle any command line options like the normal interpreter does, instead it just passes them to the script. But it does handle the respective environment variable, which can be used as shown below.
However, as a workaround, you could try to set the PYTHONINSPECT Environment variable:
If this is set to a non-empty string it is equivalent to specifying the -i option.
E.g. run set PYTHONINSPECT=TRUE before running the program.
But, probably even better, this can be done from within the Python script:
This variable can also be modified by Python code using os.environ to force inspect mode on program termination.
Here's a little test script for os.environ (also os.putenv):
import os
one = os.environ
os.putenv("PYTHONINSPECT", "TRUE")
two = os.environ
os.environ["PYTHONINSPECT"] = "TRUE"
three = os.environ
print(one)
print(two)
print(three)
print( set(one.items()) ^ set(two.items()) )
print( set(one.items()) ^ set(three.items()) )
The behaviour is a little weird: there does not seem to be a difference, and it seems to only last until you exit the interactive mode:
G:\>py test.py > test.txt
>>> exit()
G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined
The contents of test.txt are:
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
set()
set()
But it seems to work either way (double check the documentation for yourself to ensure you are not corrupting your environment variables), so you could even implement an -i argument for yourself like:
import sys, os
if len(sys.argv) > 1 and sys.argv[1] == '-i':
os.putenv("PYTHONINSPECT", "TRUE")
#os.environ["PYTHONINSPECT"] = "TRUE"
print("interactive")
else:
print("normal")
which runs as follows
G:\>py test.py
normal
G:\>py test.py -i
interactive
>>> quit()
G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined
Trying with py2exe and Python 3.4.3 (newer versions are apparently not supported and you get an IndexError):
setup.py:
from distutils.core import setup
import py2exe
setup(console=['test.py'])
Get py2exe
G:\>c:\Python34\Scripts\pip.exe install py2exe
You are using pip version 6.0.8, however version 10.0.0b2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting py2exe
Using cached py2exe-0.9.2.2-py33.py34-none-any.whl
Installing collected packages: py2exe
Successfully installed py2exe-0.9.2.2
Run py2exe
G:\>c:\Python34\python.exe setup.py py2exe
running py2exe
3 missing Modules
------------------
? readline imported from cmd, code, pdb
? win32api imported from platform
? win32con imported from platform
Building 'dist\test.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\windows\system32\python34.dll to dist
Copy c:\Python34\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy c:\Python34\DLLs\pyexpat.pyd to dist\pyexpat.pyd
Copy c:\Python34\DLLs\select.pyd to dist\select.pyd
Copy c:\Python34\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy c:\Python34\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy c:\Python34\DLLs\_socket.pyd to dist\_socket.pyd
Copy c:\Python34\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy c:\Python34\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy c:\Python34\DLLs\_bz2.pyd to dist\_bz2.pyd
Test
G:\>dist\test.exe
normal
G:\>dist\test.exe -i
interactive
>>> sys.exit()
Does not seem to have changed the environment variables permanently:
G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined
Also works with single exe:
from distutils.core import setup
import py2exe
setup(
options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
console = [{'script': "test.py"}],
zipfile = None,
)

running bash script from python file

I have a bash script which changes the path on my command line,
This one,
#!/usr/bin/env python
cd /mnt/vvc/username/deployment/
I have a python script which i wish to run after the path changes to the desired path,
The script,
#!/usr/bin/env python
import subprocess
import os
subprocess.call(['/home/username/new_file.sh'])
for folder in os.listdir(''):
print ('deploy_predict'+' '+folder)
I get this
File "/home/username/new_file.sh", line 2
cd /mnt/vvc/username/deployment/
^
SyntaxError: invalid syntax
Any suggestions on how can i fix this?thanks in advance
You need to explicitly tell subprocess which shell to run the sh file with. Probably one of the following:
subprocess.call(['sh', '/home/username/new_file.sh'])
subprocess.call(['bash', '/home/username/new_file.sh'])
However, this will not change the python program's working directory as the command is run in a separate context.
You want to do this to change the python program's working directory as it runs:
os.chdir('/mnt/vvc/username/deployment/')
But that's not really great practice. Probably better to just pass the path into os.listdir, and not change working directories:
os.listdir('/mnt/vvc/username/deployment/')

"python myscript" ignores "#!/usr/bin/env pythonX" where pythonX doesn't exist

Why doesn't test.py throw error env: python3: No such file or directory when Python 3 is not installed?
My system (Mac OS X) has Python 2.7 installed, but not Python 3:
$ /usr/bin/env python -V
Python 2.7.12
$ /usr/bin/env python3 -V
env: python3: No such file or directory
File test.py:
#!/usr/bin/env python3
import sys
print sys.executable
Executing test.py:
$ python test.py
/usr/local/opt/python/bin/python2.7
I thought that since Python 3 does not exist on my system, having the shebang line #!/usr/bin/env python3 will throw an error and terminate the script. But env actually selected the Python 2.7 interpreter.
The shebang is interpreted by OS when it tries to execute the script. Whey you type python test.py, the OS executes python and python executes the script (and python is found based on the current PATH) as opposed to being processed by the OS.
If you make the script executable (chmod +x test.py) and then try to execute it directly (e.g. ./test.py), the OS will be responsible for running the script so it will look at the shebang to figure out what program is responsible to run the script. In this case, it is /usr/bin/env which will look for python3 and try to use that. Since python3 isn't there (or not findable on your PATH) you'll see the error.
The shebang is only processed when you do test.py, running the file directly instead of running python with test.py as an argument. When you do python test.py, Python completely ignores the shebang line.

Any pyinstaller detailed example about hidden import for psutil?

I want to compile my python code to binary by using pyinstaller, but the hidden import block me. For example, the following code import psutil and print the CPU count:
# example.py
import psutil
print psutil.cpu_count()
And I compile the code:
$ pyinstaller -F example.py --hidden-import=psutil
When I run the output under dist:
ImportError: cannot import name _psutil_linux
Then I tried:
$ pyinstaller -F example.py --hidden-import=_psutil_linux
Still the same error. I have read the pyinstall manual, but I still don't know how to use the hidden import. Is there a detailed example for this? Or at least a example to compile and run my example.py?
ENVs:
OS: Ubuntu 14.04
Python: 2.7.6
pyinstaller: 2.1
Hi hope you're still looking for an answer. Here is how I solved it:
add a file called hook-psutil.py
from PyInstaller.hooks.hookutils import (collect_data_files, collect_submodules)
datas = [('./venv/lib/python2.7/site-packages/psutil/_psutil_linux.so', 'psutil'),
('./venv/lib/python2.7/site-packages/psutil/_psutil_posix.so', 'psutil')]
hiddenimports = collect_submodules('psutil')
And then call pyinstaller --additional-hooks-dir=(the dir contain the above script) script.py
pyinstall is hard to configure, the cx_freeze maybe better, both support windows (you can download the exe directly) and linux. Provide the example.py, In windows, suppose you have install python in the default path (C:\\Python27):
$ python c:\\Python27\\Scripts\\cxfreeze example.py -s --target-dir some_path
the cxfreeze is a python script, you should run it with python, then the build files are under some_path (with a lot of xxx.pyd and xxx.dll).
In Linux, just run:
$ cxfreeze example.py -s --target-dir some_path
and also output a lot of files(xxx.so) under some_path.
The defect of cx_freeze is it would not wrap all libraries to target dir, this means you have to test your build under different environments. If any library missing, just copy them to target dir. A exception case is, for example, if your build your python under Centos 6, but when running under Centos 7, the missing of libc.so.6 will throw, you should compile your python both under Centos 7 and Centos 6.
What worked for me is as follows:
Install python-psutil: sudo apt-get install python-psutil. If you
have a previous installation of the psutil module from other
method, for example through source or easy_install, remove it first.
Run pyinstaller as you do, without the hidden-import option.
still facing the error
Implementation:
1.python program with modules like platform , os , shutil and psutil
when i run the script directly using python its working fine.
2.if i build a binary using pyinstaller. The binary is build successfully. But if i run the binary iam getting the No module named psutil found.I had tried several methods like adding the hidden import and other things. None is working. I trying it almost 2 to 3 days.
Error:
ModuleNotFoundError: No module named 'psutil'
Command used for the creating binary
pyinstaller --hidden-import=['_psutil_linux'] --onefile --clean serverHW.py
i tried --additional-hooks-dir= also not working. When i run the binary im getting module not found error.

Categories

Resources