Import does work in console but not script - python

I try to run Sympy, a library for Python but I got a problem...
When I import Sympy in the console
>>> import os
>>> from os import chdir
>>> chdir("C:/sympy-0.7.2")
>>> import sympy
>>>
It works, but if I make a script with this content...ERROR!
Why ?
This error
Traceback (most recent call last):
File "**", line 4, in <module>
import sympy
ImportError: No module named sympy

try this..
import sys
sys.path.append("C:/sympy-0.7.2")
import sympy

Run the script from C:/sympy-0.7.2.
Better yet, install sympy. It will go into your site-packages directory
and will be available from anywhere. Going into C:/sympy-0.7.2 and typing python setup.py install should work.

Related

How to import the python package from NFS share

Few my python packages are available in NFS share which gets mounted on my machine
Ex:
mount server:/exports /localmount
python package module PATH: /localmount/dir1/dir2/mypackages/hub
When I try to import the package from "/localmount/dir1/dir2/mypackages/hub", it says.
ImportError: No module named localmount
But If change the directory to "/localmount" its working
Please note: All directories have __init__.py file recursively.
code snippet:
>>> from localmount.dir1.dir2.mypackages.hub import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named localmount.dir1.dir2.mypackages.hub import *
>>>
>>> import os
>>> os.chdir('/localmount')
>>>
>>> from dir1.dir2.mypackages.hub import *
>>>
Please advise how to import from package without moving the dir : localmount
Here is the fix :
Added the NFS mount path to os.path, the issue got resolved.
>>> import os
>>> os.path.append('/localmount/dir1/dir2/mypackage')
>>> from hub import p1

How to fix "from pexpect_serial import SerialSpawn" error with Python on Windows?

I have had an old script which utilises pexpect, pyserial modules. I had this running absolutely fine before i reinstalled windows. I now cannot get it to function without displaying the following;
Traceback (most recent call last):
File "C:\Program Files\Guidance Automation Ltd\kingpiN Programming\KingpinProgramming.py", line 7, in <module>
from pexpect_serial import SerialSpawn
File "C:\Program Files\Python37\lib\site-packages\pexpect_serial\__init__.py", line 1, in <module>
from .serial_spawn import SerialSpawn
File "C:\Program Files\Python37\lib\site-packages\pexpect_serial\serial_spawn.py", line 24, in <module>
from pexpect import spawn
ImportError: cannot import name 'spawn' from 'pexpect' (C:\Program Files\Python37\lib\site-packages\pexpect\__init__.py)
I have tried multiple combinations of installing the pexpect_serial, pexpect, and pyserial modules, and multiple versions of python but still no avail.
It appears to be an issue with the pexpect-serial module.
The start of the file appears as this;
import sys
import time
import os
import serial
import pexpect.fdpexpect
import pexpect.popen_spawn
from pexpect_serial import SerialSpawn
import paramiko
from config import *
So, in answer to my specific question...
I did not need that module! I simply modified my code to;
import sys
import time
import os
import serial
import pexpect.fdpexpect
import pexpect.popen_spawn
#from pexpect_serial import SerialSpawn
import paramiko
from config import *
Hence it ran all the way through no issues!
So in hindsight, always check modules are actually needed before frustratingly trying to force them to work.
Further to this, I believe "from pexpect_serial import SerialSpawn" would not function at all on Windows.

module in repo not found in local version of python package

I would like to use the physics.quantum.TensorProduct' method insympy`. Looking at the repo this method definitely exists. However when attempting to import into my python session I get the following:
>>> from sympy import *
>>> physics.quantum.TensorProduct(v1,v2)
Traceback (most recent call last):
File "<pyshell#271>", line 1, in <module>
physics.quantum.TensorProduct(v1,v2)
AttributeError: module 'sympy.physics' has no attribute 'quantum'
I installed sympy seamlessly using pip as pip install sympy. If I try to upgrade pip install sympy --upgrade I get the Requirement already up-to-date message.
Why is this that this script is not included? How can I get it so that it is downloaded from the repo and recognized in my python session?
Thanks
It seems that the physics package is not imported in sympy.__init__.__all__ so you cannot access it in your local scope with a simple from sympy import *
>>> from sympy import *
>>> 'physics' in dir()
False
Instead you could import the class you want manually. For example :
>>> import sympy.physics.quantum.tensorproduct
>>> sympy.physics.quantum.tensorproduct.TensorProduct
<class 'sympy.physics.quantum.tensorproduct.TensorProduct'>

package on sys.path, with init importing names, can be used internally, but not from outside it

I have a package my_scripting_library I want to use anywhere on machine. It has an init.py:
from silo_functions import *
and looks like
my_scripting_library
-__init__.py
-silo_functions.py
-test_stuff.py
test_stuff.py looks like:
#!/usr/bin/env python
from silo_functions import *
lines = read_lines('filepath.py')
print lines
in bashrc:
export PYTHONPATH="${PYTHONPATH}:$LIBRARY"
where LIBRARY is a correct filepath to my_scripting_library
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'/usr/bin',
'/usr/lib/python2.7/site-packages/lxml-3.3.3-py2.7-linux-x86_64.egg',
'/home/cchilders/scripts/python/my_scripting_library',
...
'/home/cchilders/.ipython']
running test_stuff with from .silo_functions import * causes:
Traceback (most recent call last):
File "./test_stuff.py", line 3, in <module>
from .silo_functions import *
ValueError: Attempted relative import in non-package
running test_stuff with from my_scripting_library.silo_functions import * causes:
Traceback (most recent call last):
File "./test_stuff.py", line 3, in <module>
from my_scripting_library.silo_functions import *
ImportError: No module named my_scripting_library.silo_functions
but running test_stuff with from silo_functions import * works:
it prints the lines
Of course I can't use this package from other folders, which is the real issue- I don't want to be forced into throwing all scripts in this one place. This is causing huge problems as I am constantly reusing dozens of functions each script, and over 5 tutorials on making a folder a python package never have worked yet. Why is something on the python path with an init not a package? Thank you
May be it is because you've added '.../python/my_scripting_library' to your path. But there are no 'my_scripting_library.py' at this folder.
If you want to use 'my_scripting_library.silo_functions', try to add '/home/cchilders/scripts/python' (not '/home/cchilders/scripts/python/my_scripting_library') to path.
Because 'my_scripting_library' is module. Python will find this folder, find __init__.py in this folder and mark it as module.

Python module import error

when i am importing some modules from python shell, import work fine:
for example: (link for propy module https://code.google.com/p/protpy/downloads/list)
>>> import propy
>>>
but when i write script with python Default IDLE or with other IDE and save it to as .py script , import statements not work and generate error like this
python fragment_generator.py
>>>
Traceback (most recent call last):
File "J:\acetylome scripts\New folder\fragment_generator.py", line 1, in <module>
import propy
ImportError: No module named propy
>>>
please solve it
thanks in advance:
When you run the shell from the same directory the files are in the import will work. You are probably working from another directory when the import fails.
you can add a directory to the python path like this.
befor your import:
import sys
sys.path.append('/path/to/your/folder')
import propy
good luck.

Categories