I've run into an issue i don't know how to deal with. I've written a Python script in Jupyter notebook. The script works properly in Jupyter, but I need to call the script from a commandline, so I've converted it to a .py file using nbconvert. When I run the script by explicitly calling python and the script with full paths, I get an error that a module couldn't be loaded properly. It obviously didn't have any problem inside Jupyter, so I don't know what the problem with the module could be. I saw another post that suggested updating all your modules, but that didn't change the behavior. It has a problem with the PIL module.
Any help would be appreciated. I only have one flavor of Python installed and I'm explicitly referencing the python.exe file in Anaconda folder.
Thanks for any help.
Vince
Error message
I figured this out. Python couldn't find the module file because it was locked. I had the script open in Jupyter. Not sure why it was bothered by this module and not the others I was importing, but that was the problem here.
Related
I am writing a script file in the Jupyter notebook & I need to call a function. I created another file called myfunctions.py and defined my function called give_ans() there. I will have many more functions here later. But I get an error in the main script file when I use the following command
from myfunctions import give_ans
The error is No module named 'myfunctions'. I use pwd to see their path and it is exactly the same which is 'C:\Users\my name\OneDrive\my_code'. So I don't know what is the cause of this error. Please advise. I have always been a Matlab coder and this is my first attempt here.
Update: I found the problem & fixed it. I gave the solution in my answer below.
I found out what was going wrong. Both of my Jupyter files are of the .ipynb extension whereas the myfunctions.ipynb script that I intend to import should be in .py form. To do so, I went to the command window (Anaconda one in my case), went to the same directory as mentioned in the question, and did the following.
pip install ipynb-py-convert
jupyter nbconvert --to python myfunctions.ipynb
This produced a .py file and then there was no error upon using the import statements as I wrote in the question.
Edit: Anyone reading this answer please also make sure to read the comments below.
I've run into a strange error I can't quite figure out involving a library installed via pip.
I ran pip3 install mcp9600 to install a library for a temperature sensor, and then verified that this worked as expected by running a python terminal, importing the library, and invoking the relevant class:
After that I tried to run the exact same thing from a .py file and got the error below:
I'm really not quite sure what is happening or how to fix it, since I've never had this issue before. What's going on here?
Your script is named mcp9600.py so the code import mcp9600 imports the script as a module. The script (imported as a module) doesn't have any MCP9600 hence AttributeError.
Rename the script to something else. Even mcp9600 (without .py) is ok. Take the lesson: never name your scripts the same name as Python libraries; for example, never name your scripts email.py or test.py — they overshadow Python's email and test.
your script file is also named mcp9600.py, just like the module. Python begins searching for modules in the same directory as the source file, so it finds the script itself instead of the module.
Try renaming your script.
I figured it out. Seems like I can't call the python script the same as the library name!
I'm new to this but embarrassed all the same!
I have a simple .ipynb that reads a value from one excel file, and prints it to a cell in another excel file. I use 'jupyter nbconvert --to script MyFile.ipynb to generate .py file. This works, no error. .py file is created.
In terminal:
C:\Users\me\Anaconda3\Scripts>jupyter nbconvert --to script MyFile.ipynb
Output:
[NbConvertApp] Converting notebook MyFile.ipynb to script
[NbConvertApp] Writing 1034 bytes to MyFile.py
I then go:
C:\Users\me\Anaconda3\Scripts>pyinstaller --onefile MyFile.py
I then get:
raise ImportError('Unable to load module attribute') from e
ImportError: Unable to load module attribute
This is my stop point now, and I can figure it out. I find information about adding paths etc., but I don't really understand what to do?
The history is like this, I first got 'maximum recursion depth exceeded error'. I tried to solve this by altering spec file with:
import sys
sys.setrecursionlimit(5000)
I then ran -C:\Users\me\Anaconda3\Scripts>pyinstaller --onefile MyFile.py' again, with no luck. Spec file was over-written.
I then ran 'pyinstaller MyFile.spec' this did not work.
I then found somewhere that the version of openpyxl could be the issue, and I uninstalled current version, and installed openpyxl 2.3.5. I then deleted my .py file and started over to be on the safe side. After doing this I got the above 'ImportError: Unable to load module attribute' error.
It seems like I keep moving towards getting an exe file in the end here, but it sure isn't as easy as some forums would like it to be.
What am I doing wrong? How can I improve my overall code/ setup to reach my goal of a functional exe file? I am using windows, and the users of the exe file will be windows based.
Thanks!
I was having a similar issue and for me the cause of my version of matplotlib. There appears to be a current known bug with matplotlib version 3.3.x. Downgrading to 3.1.2 fixed my issue. Also if you are using Anaconda Navigator I would recommend creating a dedicated Python environment solely for packaging your script.
This is somethings that has been bothering me since I started using PyCharm to program in Python. I have a two .py files, in the same directory, main.py and external.py. Inside main.py, I have import external at the top. PyCharm marks this as an error, but it runs fine both in the new zsh MacOS terminal and PyCharm itself, and I can use all the things declared in external.py as expected.
I've played around with it a bit, and (to my very limited knowledge) it seems that PyCharm detects imports like Python2. Thats a guess, though, as I am unfamiliar with that version.
Why does PyCharm do this, or am I the one to blame? If it's not my fault, how can I fix it?
My file structure is as follows:
Project-|
|-external.py
|-main.py
I want to use things from external.py in main.py, and I can, but PyCharm gives it a red underline.
From given description, it correctly imported your external module.
Did you create a folder inside your project folder?
When using subfolders for your main.py/external.py files, Pycharm might not by default correctly detect your import statement.
Pycharm should give you an error message for said import statement.
Maybe the error is not connected to the import statement but to your pycharm setup e.g. correctly setting up your python interpreter.
If you provide more information regarding your folder structure or the error message, that might help.
Please try to mark directory containing your python files as Sources Root, see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
I'm really confused. For some reason, I suddenly can't open IDLE 3.5. I have tried uninstalling it and reinstlling, but no luck. I was told to delete the folder .idlerc but it hasn't come back after constant tries. Was that a huge mistake? I have no idea what's going on.
Is it just the IDE? Or the python install itself?
When having problems using IDLE as an IDE I would recommend using PyCharm. It offers imho the best options for Python developing.
A common cause of failure of idle startup is a user-written file with the same name as a standard library module, such as random.py and tkinter.py.
First make sure you dont have a file named random.py and tkinter.py in the current folder.
All the startup failures of idle are described here.
I had the same problem. Turned out I had a python program named 'random.py'. Idle would not run until I renamed that program!