module in repo not found in local version of python package - python

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'>

Related

Can not import pip modules

While coding on python I faced a problem with importing modules using PIP. The thing is that I can not import a single module, for example "camelcase". Would someone help me?
import camelcase
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))
It is expected that the output will be "Hello World". But there is following an error:
Traceback (most recent call last):
File "mycode.py", line 1, in
import camelcase
ModuleNotFoundError: No module named 'camelcase'
ModuleNotFoundError: No module named 'camelcase'.Error clearly said that you need to install camelcase package
official docs of camelcase package
virtual env set up
Use the following command:
pip install camelcase
import camelcase
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))
Instead of third party package you can also do
print(txt.title())

Sha-3 in python implementation

I am trying to implement sha-3 in python.The code given below is how I implemented it.But i am getting the below error again and again.
import sys
import hashlib
arg1 = sys.argv[1]
with open(arg1, 'r') as myfile:
data=myfile.read().replace('\n', '')
import sha3
s=hashlib.sha3_228(data.encode('utf-8')).hexdigest()
print(s)
The following error is what i get when I execute it.
Traceback (most recent call last):
File "sha3.py", line 6, in <module>
import sha3
File "/home/hello/Documents/SHA-3/sha3.py", line 7, in <module>
s=hashlib.sha3_228(data.encode('utf-8')).hexdigest()
AttributeError: 'module' object has no attribute 'sha3_228'
The below link can be used for reference.
https://pypi.python.org/pypi/pysha3
There are two problems here: one from your code, and one from the documentation, that contains a typo on the function you would like to use.
You are calling a function that is not present in hashlib library. You want to call function sha3_228 from module sha3, that is shipped with package pysha3. In fact, sha3_228 does not exist, it is sha3_224 that exists.
Simply replace hashlib.sha3_228 with sha3.sha3_224.
And make sure you have installed pysha3, with command
python -m pip install pysha3
Here is an example
import sha3
data='maydata'
s=sha3.sha3_224(data.encode('utf-8')).hexdigest()
print(s)
# 20faf4bf0bbb9ca9b3a47282afe713ba53c9e243bc8bdf1d670671cb
I had the same problem. I installed sha3 by itself first. That doesn't work. Then I installed pysha3 and it still didn't work. I finally uninstalled both sha3 and pysha3. Then I reinstalled just pysha3 and it worked fine.
You likely need to include:
import sys
if sys.version_info < (3, 6):
import sha3
This is because is lower versions of python sha3 aren't included by default in hashlib.

PyPI Module not working

So today i started working on a simple python module, but i cant make it work.
the module itself works, but when i have uploaded it to PyPI and i then install it using Pip, it wont work.
Please notice that it is built for python-2.7
The source code can be seen here:
https://github.com/1m0r74l17y/FortyTwo
and it can be downloaded using:
sudo pip install FortyTwo
whenever i try to run a test program like this:
from FortyTwo import *
FortyTwo.nope()
It just gives me an error:
Traceback (most recent call last):
File "test.py", line 3, in
FortyTwo.nope()
AttributeError: 'module' object has no attribute 'fortytwo'
I would really appreciate any help, as it might lead me onto what i have to do to fix the problem.
You would need to do the following.
from FortyTwo import fortytwo
fortytwo.nope()
If you want to call nope directly from FortyTwo you would need to import that function in __init__.py.
e.g.
from FortyTwo.fortytwo import nope
def Start():
"""No Clue what to add here"""
What if you do
from FortyTwo import fortytwo
fortytwo.nope()
* credits to eandersson.

Missing classes in wxPython

In the documentation of wxPython there are some classes that are missing.
For example: TextCompleter.
But in python I get:
>>> import wx
>>> print wx.__version__
2.9.3.1
>>> from wx import TextCompleter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name TextCompleter
I have also tried with the newest version of wxPython (3.0)
As you may have noted, you are referring to the wxPython (Phoenix) documentation. You however have the classic version installed, where TextCompleter has not been wrapped in SWIG (and probably never will, because development effort is concentrated on Phoenix). This class has been wrapped in Phoenix. If you are absolutely interested in trying out Phoenix, set up a virtualenv and pip install <path-to-wheel> a snapshot build. Do yourself a favor and download the wheel matching your python to local disk before trying to install the .whl-file.

Import does work in console but not script

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.

Categories