Can't name python file data_loading - python

I created a Python file named data_loading in PyCharm and it showed up as a regular text file without the ability to run.
Then I switched the name to data_load and it worked.
I'm not sure why it couldn't run when it was named data_loading.
Is there a reason for this? I'm new to Python.
Proof here: https://i.stack.imgur.com/rZl9S.png

There's nothing inherent in Python that prevents a script named data_loading from running. Either it's something in your environment that's preventing it from running, or the file was created differently in PyCharm for some reason. Either way, we don't have enough information to diagnose the issue with certainty.
I am certain that Python isn't the issue.

Related

PyCharm doesn't run files calles "main.py"? [duplicate]

This question already has an answer here:
How to run Main.py automatically in PyCharm
(1 answer)
Closed 1 year ago.
Sorry, this is driving me crazy. I just started using PyCharm, and have gone through the normal steps to create a Python project. PyCharm creates a default file called "main.py". However, PyCharm does not seem to know that this is a Python file, despite the ".py" extension in its name.
The text formatting color highlights in the text editor don't work.
There is no option to Run this file.
However - when I add a new Python file manually to this project with a different name, everything works fine. However, when I create a file called "main.py", nothing works, as described above. When I create a new Python file with a different name, and then rename it to "main.py", it doesn't work again.
Why is this happening? I've searched for this problem but haven't found an applicable solution. Why does PyCharm doing something weird to files called "main.py" specifically?
Thank you.
Try Ctrl+Shift+F10,
should automatically edit the configuration.
to add to the previous answer: you can also right click on the file and choose "Run 'main'", which automatically creates a configuration with the project interpreter for that file.
Another thing you can check is your interpreter settings and ideally make sure its using venv, this way packages you install don't go into global and are kept on a project basis.

Python module loads in Jupyter but not in .py

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.

Inconsitent Python error - Class works fine in terminal, but not in script

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!

Can opening a .py file in PyCharm be harmful/malicious?

I've just started learning Python and have downloaded PyCharm and understand this is likely to be a very basic question but wanted to ask before I set off with a false sense of security.
I'm interested to know whether:
a) Simply opening but not running a .py file as a project in PyCharm IDE could be malicious.
b) Opening and running a .py file in the PyCharm IDE can be malicious
If not either, why not? Would the file need to be a .exe to do anything without PyCharm being involved?
Again, appreciate any help/insight you can provide.
So just opening a file in PyCharm will not harm any computer in any way.
But running code can do that, so I would be careful were you get the code which you want to execute. And it would be best if you understood the basic of the code you are running. Therefore you would be able to spot strange lines in your code.
A Python source file (like any other programming language) is just a plain text file, so until the instructions that are written in the file are evaluated (by the python interpreter in this case) the file itself cannot be in any way harmful.
Only once you run python myfile.py in a shell or by using an IDE like PyCharm the code is actually executed... at that point what happens depends on what is written in the file (which you can check before executing it).

IDLE 3.5 won't open

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!

Categories