Can't access full python module from file, but can from shell - python

Maybe I am completely missing something here but when I run this code form the shell it works:
import nltk
tokens = nltk.word_tokenize("foo bar")
and returns:
['foo','bar']
But when I but this into a file and execute it with python -u "path/to/file/myfile.py" it returns
AttributeError: 'module' object has no attribute 'word_tokenize'
I've tried reinstalling and every thing i can think of. Let me know if you need any more information.
Thanks in Advance!

You have more than likely called your file nltk.py so python is trying to import from that as opposed to the actual nltk module. Just rename your .pyfile.

Related

Need help understanding how to use a python package I made

I recently created and published a python package called ADCT.
Link to package as zip download so you can see what I mean: https://pypi.org/project/ADCT/#modal-close
I went ahead and published it and I was able to pip install it on my local machine. In the package, there is an object itself called ADCT. What code snippet, other than "import ADCT" do I run to call the object ADCT? Do I have to rename the object to something else since it could be a collision error? I know this is embarrassing since its my package but any help would be appreciated.
The package and the object will likely to end up in different namespaces. Example:
from ADCT import DataObj
adct_instance = DataObj.ADCT()
Another way to deal with duplicated names (not very helpful in this case, just for the reference) is to use import .. as, e.g.:
import ADCT as ADCT_package
adct_instance = ADCT_package.DataObj.ADCT()

After using "del np.dtype", should re-importing numpy fix this?

I'm trying to understand the behavior of import in python.
I used del numpy.dtype, and it throws an error when I type numpy.dtype. But when I try to re-inport numpy, it doesn't help.
I tried to follow the answer from Python: "de-import", "re-import", "reset import"? by doing del sys.modules['numpy'], but I get an error when I try to import numpy again afterwards.
The weird part about all of this is that I can still use numpy without any issue, except that when I type numpy.dtype, I get the error:
AttributeError: 'module' object has no attribute 'dtype'
Can someone explain what is happening and how I can restore:
numpy.dtype
the ability to import numpy without error
I'm using Python2.7 in a Spyder interactive IPython console.
A module will only get imported once in Python, so your subsequent imports do nothing. You can use importlib's reload method to reload a library, which will bring dtype back, after you have deleted it.
import importlib
importlib.reload(numpy)
# numpy.dtype accessible again
You mentioned you are using Python 2.7, and the importlib included in 2.7 is mainly for ease of updating, and does not contain a reload method. You should really update to Python 3, as 2020 is approaching fast, but you can use the builtin reload method if you insist.

python 3.4.2 urlib no attribute 'pathname2url'

Why is the next code not working? I cant find why.
import mimetypes
import glob, urllib
for file in glob.glob("C:\\Users\\joey\\Desktop\\school\\ICOMMH"):
url = urllib.pathname2url(file)
print(file, mimetypes.guess_type(url))
The error message I get is:
AttributeError: 'module' object has no attribute 'pathname2url'
I am trying display all file typs of a directory. Is this a good way? Or is there a better way. I dont want to use the module magic.
This function's location has changed in Python 3. It is now urllib.request.pathname2url.

Python "import random" Error

As you may know from my previous posts, I'm learning Python. And this time I have a small error which I think is with this build of Python itself. When using the following:
import random
number = random.randint(1,10000)
Python gives me this error:
File "C\Users\name\Documents\Python\random.py", line 5, in (module)
print random.random()
TypeError: 'module' object is not callable
Every time I try to run it. Me no understand. Any help would be much appreciated!
EDIT: The two lines of code I'm trying to run:
import random
print random.randint(1,100)
That's it. And it gives me the same error.
By naming your script random.py, you've created a naming conflict with the random standard library module.
When you try to run your script, the directory containing the script will be added to the start of the module import path. So when your script does import random, you're effectively running a second copy of the script as the random module.
When the random module runs import random, it means that random.random will also be a reference to your module. So when you attempt to call the random.random() standard library function, you're actually attempting to call the module object resulting in the error you got.
If you rename your script to something else, the problem should go away.
Even I faced the same problem. I have renamed my python file from random.py to shuffle.py. This didn't work out. Then I changed the version then it worked. This may help a bit.
Python version : 3.6.7
replace
import random;
to
import random2;
I am using pycharm and I had to take the additional step to import the methods from random. In my case:
import random
from random import choice
The simple answer: Change your filename from "random.py" to something else as it is conflicting with random library.

python uuid weird bug

I first tried with the interpreter to produce uuid's with python's uuid module. I did the following:
>>>import uuid
>>>uuid.uuid1()
UUID('d8904cf8-48ea-11e0-ac43-109add570b60')
So far so good. I create a simple little function to produce the uuid's.
import uuid
def get_guid():
return uuid.uuid1()
if __name__ == '__main__':
print get_guid()
and I get the following error:
AttributeError: 'module' object has no attribute 'uuid1'
Ok...hmm...go back to the interpreter and now it too is broken. I get the same error running the same code I used to test this. I am baffled. What makes uuid break like this? And what is wrong with my code?
I am using python 2.6
Your test file name is most likely named uuid.py
When you went back to the interpreter, you launched the interpreter from the same directory, which by default, will first look for the module name to import in your current working directory.
Just change your test file name to something else, i.e. uuid_test_snippet.py

Categories