import modules in python - python

when i write import win32print in my python command line (IDLE) it works fine and it is imported...
but when i write import win32print in my py file then it gives an error
says no module named win32print
why am i not able to load the module when writing the same statement in a py script...im using windows XP

Check if you are running on the same installation. I'd do that by looking the path:
import sys
print sys.path
And compare the output on IDLE and when you run the .py

Related

Import on shared object fails with ModuleNotFound only when script is executed from command line

ran into a weird problem where there is a shared-object import error only when I run the script from command line. It succeed if i run the script in the python console using exec(...)
I have a class that needs a shared object: foo.py:
import os
cur_dir = os.curdir()
os.chdir('/tmp/dir_with_shared_object/')
import shared_object_class
os.chdir(cur_dir)
class Useful:
... # use the shared object import
Then there is a script like this:
from foo import Useful
If I enter python console and run:
exec(open('script.py').read())
Everything works fine.
If I run this on command line:
python script.py
I will get
ModuleNotFoundError: No module named 'shared_object_class'
The python is the same. It is 3.7.3, GCC 7.3.0 Anaconda. Anyone knows what is causing this discrepancy in behavior for shared object import?
A standard way of importing from a custom directory would be to include it in the PYTHONPATH environmental variable, with export PYTHONPATH=/tmp/dir_with_shared_object/.
update
It could also be done dynamically with
import sys
p = '/tmp/dir_with_shared_object/'
sys.path.insert(0, p)
PS
I think I have an explanation for why OP's original code didn't work. According to this python reference page, the import system searches, inter alia, in "[t]he directory containing the input script (or the current directory when no file is specified)." So the behavior in the REPL loop is different from how it is when running a script. Apparently the current directory is evaluated each time an import statement is encountered, while the directory containing the input script doesn't change.

Python: ImportError for a module in a script launched from command line

There is a custom module "ETPython" generated by SWIG (consists of ETPython.py and binary _ETPython.so) and a simple script that invokes this module
sample.py
import ETPython
...
There aren't any problems if the script is run in an IDE (pycharm, internal python's IDLE, squish by froglogic so on). But if I'm trying to launch it in python shell in interactive mode or via terminal using
python3 sample.py
there is an error message like:
File "<path_to_file>/example.py", line 12, in <module>
import ETPython
File "<path_to_file>/ETPython.py", line 15, in <module>
import _ETPython
ImportError: dlopen(<path_to_file>/_ETPython.so, 2): Symbol not found: _NSLog
Referenced from: <path_to_file>/_ETPython.so
Expected in: flat namespace
Searching topics, I found that problem is related to wrong paths. So I added some code:
import os, sys
os.chdir("<path_to_dir>")
sys.path.append('<path_to_dir>')
os.system('export PYTHONPATH=<path_to_dir>:$PYTHONPATH')
This code helped to import the module in python shell in interactive mode but launching in terminal is still failing.
So the question is how to make it to work?
I found solution. The SWIG module was compiled incorrectly.
There was option for CMake that suppressed errors for undefined symbols
set(PLATFORM_LIBS "-undefined dynamic_lookup" )
That why the module doesn't contain NSLOG symbol.
The module was recompiled with additional
"-framework Foundation"
in swig_link_libraries statement. And now the module is imported correctly

python: py2exe paramiko showing no results

I am using python 2.7.11 on Windows 7 64bit, I am facing a problem with py2exe when I import paramiko module, the .exe file runs but does not show any results, after executing it closes immediately knowing that i did not get any error during the conversion.
import paramiko
print raw_input("press enter to exit")
the setup.txt file:
from distutils.core import setup
import py2exe
setup(console=['test.py'])
I have found that in the "dist" folder there are some dll files such as API-MS-Win-Core-ErrorHandling-L1-1-0.dll - API-MS-Win-Core-LibraryLoader-L1-1-0.dll - API-MS-Win-Core-LocalRegistry-L1-1-0.dll, such files I don't usually face them on another machine, so could anyone help please.
I was able to solve this after importing some modules:
import appdirs
import packaging
import packaging.version
import packaging.specifiers
import packaging.requirements
hopefully that could be useful for someone.

pylab imports from shell but not when running .py file from cmd

I am trying to run a *.py file from the cmd which contains an import pylab statement, and I get an import error saying 'No module named pylab', however when running the same statement from the python shell it works fine. What could be wrong?

Python import error with Sublime Text 2 with same PYTHONPATH and Python version as terminal

I just installed gdata with pipinstall
while I can import when starting python from the command line:
import gdata.docs.service
I get an
ImportError: No module named docs.service
when I do it with the sublime python build. I realize that on Python.sublime-build I can specify the python version and the PYTHOPATH.
I checkt both and made sure to have both the same as on the command line. I also checked that the folder from where I start the python console does not include gdata. What am I missing?
my PYTHONPATH check:
import os
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
print user_paths
and the Python version check:
import sys
print (sys.version)
I also checked that $which python is the same path as the "cmd" value in the sublime build config file.
I finally find the answer.
The named the file gdata.py and this is why the import gdata.docs.service was resolved with the file itself and could obviously not find "docs" in it.

Categories