Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I need help with something that should be basic. Using the example from Python.org I am encountering the following error
parser = argparse.ArgumentParser(description='Process some integers.')
AttributeError: 'module' object has no attribute 'ArgumentParser'
import argparse
parser = argparse.ArgumentParser()
A print argparse.__ file__ confirmed that it is calling the correct argparse.py file:
File "C:\Python27\lib\argparse.py", line 86, in
Also I made sure there are no nameclashes withother modules. Infact, just removing everything else from the script still calls this error.
Have tried reinstalling python 2.7.9 with no change.
Help this noob please!
It's because your file name is the same as the module name. Rename your file and try again.
so, I had:
from numpy import *
and I changed this to import numpy and it didn't work.
Then I copied all the test verbatim and made a new .py file and saved it out.
Now it works.
thank you Python. I think i'll stick to C!! :S
I think I know what's wrong. It may be unavailable in the release of Python 2.7 that you're using. Try installing Python 2.7.8 or 2.7.9.
When importing modules, python usually first looks for them in your current Path, and only if it can't find them there, it will then look in the other Python-paths (found in sys.path).
Like Daniel Roche said, You obviously named your script "argparse.py" (thinking this would be a good name for an example on how to use argparse, an intuitive and common mistake, I think) and then basically told your script to import itself (how rude).
... And since your script does not have an "ArgumentParser" method defined, this must of course yield an Error.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Okay so I am very new to Python. Been working on a project, but for whatever reason I get the
"FileNotFoundError: [Errno 2] No such file or directory: 'yt.txt'"
unless in Python I call the extension twice. I'll try to better describe it below.
def mp():
with open("yt.txt", "r") as my_file:
str = my_file.read()
print(str)
mp()
Above is the code. The script won't work unless it's named yt.txt.txt I've tried searching here and other forums but I can't seem to figure it out. The file IS in the same directory as the script, it works with the double extension just not with the single extension. Any help would be appreciated. If you need any additional info just let me know.
I take it you have "show known file extensions" in windows ON so you can confirm the obvious - that its not actually called yt.txt.txt?
[Not too proud to admit stupid stuff like that has tripped me before!]
I'm dumb, thank you to Amiga500 and Barmar for chiming in. The answer was I turned on the "show known file extensions" and it showed the file as yt.txt.txt. I just renamed it, I feel really goofy lol. Thanks all.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to make a script that can delete a file with Python. When I moved the file to my startup directory with my script, a lot of the code was changed, not by me. I've just assumed this was normal and continued trying to make it delete a file on startup. I later realized it wasn't working because one of the call methods kept getting an invalid syntax. Here's my code.
Python Version: 3.8.7
Error message is :
invalid syntax (<unknown>, line 7)
LOAD_CONST(0), LOAD_CONST(None), IMPORT_NAME(os), STORE_NAME(os)
LOAD_CONST(0), LOAD_CONST(None), IMPORT_NAME(shutil), STORE_NAME(shutil)
source = 'C:\\Users\\me\\OneDrive\\Desktop\\startup.py'
destination = 'C:\\Users\\me\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup'
LOAD_NAME(shutil), LOAD_METHOD(move), LOAD_NAME(source), LOAD_NAME(destination), CALL_METHOD[2], STORE_NAME(new_path)
print(new_path)
LOAD_NAME(os), LOAD_METHOD(remove), LOAD_CONST('C:/Users/me/OneDrive/Desktop/delete.txt'), CALL_METHOD[1], POP_TOP
print('File Removed!'), return None
From the code, it's difficult to understand what you are trying to do. If you want to delete a file, you can use the os module in python. For example:
import os
os.remove("/some/file/path/to/remove.txt")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
As per matplotlib documentation:
"You can create custom styles and use them by calling style.use with the path or URL to the style sheet."
I tried:
plt.style.use('/usr/share/mygraph/mystyle.mplstyle')
but it is returning:
ValueError: '/usr/share/mygraph/mystyle.mplstyle' not found in the style library and input is not a valid URL or path. See `style.available` for list of available styles.
Could someone please help me on how to set the path right?
Is there is any other way how to achieve this?
I would like to distribute my custom style in rpm package so it would be tricky to put it in user's home dir.
Setting "MPLCONFIGDIR" environment variable would also work but I would like to avoid this.
Thanks for your help!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EDIT:
In my impetuosity I forgot to copy the file there. The output error about not valid URL confused me so I thought the whole path is not valid and focused on that. Shame on me!
Maybe it can be useful to future readers to know that there is also the possibility of changing the style folder (you can get the current folder with matplotlib.get_configdir()). According to documentation:
You also can change the directory where Matplotlib looks for the
stylelib/ folder by setting the MPLCONFIGDIR environment variable, see
matplotlib configuration and cache directory locations.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I am completely new to any programming language and really want to learn it this time (multiple attempts) and these things aren't making it any better. I must be doing something completely simple completely wrong. shows my example.
Taking code from codecademy and putting it directly into the IDE creates errors. I am using the 2.7 interpreter as that is what codecademy uses as well. I tried indents but nothing so far. Anyone care to blow my mind on how simple a fix this is?
You are using Python 3.4. Figured it out from the interpreter selected by Eclipse. Try changing the interpreter to 2.7. It should work.
As far as I can see from the screen shot, you are trying to do:
>>> from datetime import datetime
>>> now = datetime.now()
>>> print '%s/%s/%s' % (now.year, now.month, now.day)
2015/12/24
And it actually works on my client. (Python 2.7) I cannot see your error clearly but probably it is because you are (for some reason) trying to run it with Python 3.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to use threading in an upcoming project but I can't get it working. I've tried running the 2nd example on http://www.tutorialspoint.com/python/python_multithreading.htm, which I imagine is working code, but I get the error...
Traceback (most recent call last):
File "C:/Python27/threads/threading.py", line 3, in <module>
import threading2
File "C:\Python27\lib\site-packages\threading2\__init__.py", line 49, in <module>
from threading import _active,_DummyThread
File "C:\Python27\threads\threading.py", line 8, in <module>
class myThread (threading2.Thread):
AttributeError: 'module' object has no attribute 'Thread'
Whats going on? It seems like there is no module named threading. I've looked everywhere to find it. Does anyone know where I can find this module and how to install it?
You called your own file threading and now you're importing your own script and that's not what you want. Please do not use the names of existing packages/modules for your own scripts.
When the code in threading2.py tries to import threading, Python first looks for an already-imported module with that name. If that fails, it looks through each location in sys.path for a file named threading.py that it can load. Normally, it will find the one in the standard library. But if you have a file named threading.py in the same location as the script—or if it's the name of the script itself—Python will find that one first, and load it instead.
Also, don't save your own scripts in the folder where Python is installed.