Getting Error can't find '__main__' module in 'X' - python

I made a simple hello program using Sublime Text. Run it using windows powershell but everytime I am getting error like :
C:\Python27\python.exe: can't find '__main__' module in 'X'
X is the name of folder. I have Python 2.7.4 Installed. How to fix it?

Maybe there's something wrong with your C:\Python27\python.exe.
See if there are two python under the folder C:\Python27:
python.exe
python2.7.exe.
If so, chose the python2.7.exe
Also possible if your module X is in anaconda or something similar, then you should adjust your Sublime environment to include anaconda (or whatever you're using).
Tools -> Build System -> New Build System

I know nothing about Python, other than running other people's Python apps on Windows occasionally. I see that this is an old thread, but still ran into this just now--the app threw this error and wouldn't run at all unless I included the '.py' extension on the command line.

This error can be caused by file corruption -- check that your module 'X' was copied correctly to the destination folder -- try opening it in an editor and make sure it looks ok. You will get this same error if you try to run Python on a file which is not a text file or a precompiled python module.

This problem is the same what I get last night, if you are using PyCharm to code Python, you can re-check the existing configuration, make sure in the working directory section it is filled in, and in the script path section, it is the same as the file location and file name.
Actually this is a problem where you have to write down the correct storage place.

Related

Execute python at visual code but fail (error message: python path configuration.....)

When I execute the python at visual code, the error happen. Message like below:
I did the solutions like add PYTHONPATH and turn off python related exe at Manage app executions aliases.
Thanks!
I THINK THAT YOU HAVE NOT CREATED A PATH OF VS CODE IN ENVIRONMENTAL VARIABLES.
IF ITS DONE ONLY YOU CAN EXECUTE PROGRAMS IN VS CODE.
ELSE YOU HAVE TO DOWNLOAD LATEST VERSION OF PYTHON (3.9.6).
This is my sys.path:
['d:\\workPlace\\pythontest3.9\\hello',
'D:\\workPlace\\pythontest3.9',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python39\\site-packages\\win32',
'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python39\\site-packages\\win32\\lib',
'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python39\\site-packages\\Pythonwin',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
It is a little weird because I really can't make my sys.path contains .\\DLLs and
\\lib instead of the full path.
That's caused the problem as the interpreter could not find the encodings package under the Lib folder.
Could you try to create a new one?

My Python Script can't find a JSON file in the same directory

I am practicing some API work in python 3.7 using API star 0.5.X and my python script can't find the .json file that is in the same folder as the python file. I am working on and running the script with Atom editor and I am working in a venv, which is fairly new to me.
I am using a helper function to load in the JSON data using a "with open()" statement. I have tried using the relative and absolute file paths, and in both instances it is unable to locate the file. I have tried launching the file in Atom using terminal and the MacOS finder.
This is what I have so far:
import json
from typing import List
import os
from apistar import App, Route, types, validators
from apistar.http import JSONResponse
print(os.getcwd())
os.chdir('/Users/{myusernamehere}/100days/apistar')
print(os.getcwd())
#helpers
def _load_employee_data():
with open('employees.json') as f:
employees = json.loads(f.read())
return employees
the second print statement prints the correct file path, being the one that 'employees.json' and 'app.py' are located in.
Since the problem is specific to your setup, it's hard to reproduce or provide a solution in code. Your code itself looks to be fine, but there are two things that are likely to be the cause of your issues:
When your script is running, Python needs access to the appropriate source folders and installed packages; you should let something like virtualenv manage this through a virtual environment. From the terminal, you can load the appropriate virtual environment with:/path/to/your/venv/Scripts/activate.sh
If you do, you should expect your script to find the same libraries it did during development in that virtual environment. Make sure you include something like a requirements.txt in your project to allow easy reinstalling of the same modules on a different machine, in a new virtual environment.
Your script, when run by Python, has a 'working directory'. This is the directory that Python is started from and your script not being able to find the file (even though it may be in the same folder as the script itself) is probably due to Python being started from a different directory.
This was a problem due to how the Atom editor works. It was solved by switching to vim.
I only partially understand but apparently this had something to do with Atom having a separate temp directory for working files, or something of that nature. When using vim to edit the script, and then calling it in the terminal the problem was resolved.
Okay, So i had the same issue with i.c.w. VScode :
file = open('file.txt')
print(file.name)
resulted in
FileNotFoundError
file.txt was 100% in the same folder... According to finder on my Mac, ánd the folder column in VS code!
i was pulling out my hair. Switched a lot of interpreters, local python and Conda, to Python 3.8 instead of 3.9, back to python 2.8.
Nothing mattered.
Till I changed :
file = open('file.txt') to: file = open('file.txt', 'a')
It didn't suddenly work, but I saw immediately in the "folder column" of VScode a new file.txt file popping up. In an entirely different folder then where the pythonfile.py was located. So after that; I pushed all local repo's to their remotes; deleted the whole caboodle, and installed them one by one in a new folder through git clone. I opened a new workspace added those project folders and since then it works like a charm.
What exactly went wrong ; im sorry, I have no idea. But for me, a fresh install of the repo's and VScode workspace is what did the trick.
I recently had the same error, on Visual Studio Code, I managed to solve it by instead of clicking the Run Python button, I used the terminal to cd into the project directory and run the python script like that, and no problems!

Python 3 install error. This file does not have a program associated

I have encountered an error while try to run a python 3.6.4 script after upgrading from 2.7.
I had initial installed both python 2.7 and 3.6 but them deleted 2.7 because I had no use for it and i am not getting this error.
I have tried:
Reinstalling python 3.6.4
changing the install location of said install
Repairing the python 3.6 install
Error:
A Python script by itself is not an executable program. So if you want to run the script Evosim.py by just entering Evosim.py in the command line, the .py file ending needs to be associated with the Python interpreter. It seems that currently in your system this is not the case.
There are two ways to deal with your situation:
Start your script by entering python Evosim.py instead of only Evosim.py in the command line.
Fix the broken association of the .py file ending with the Python interpreter. The answers to this question migth help you with that.
Solution #1
Go to your registry by searching regedit.
Open category HKEY_CLASSES_ROOT. Select .py. If .py don't exists then make it by yourself.
Double click the Default. It will make a popup. Change its value to Python.IDLE and click OK. Make a new String key in .py naming Content Type with value text/plain.
Solution #2
If the above method don't works then just go ahead and reinstall your python (also uninstall Python Launcher). Your modules and files will be at their place.
You can now open .py files.
Running command prompt as an administrator resolved my issue, and able to execute my python script
1). Right click any python file
2). Click Open with then Choose another app
3). Click on More apps then Look for another app on this PC. It should open up file explorer
4). Select python.exe wherever it is located.
A sample path is C:\Users\<user>\AppData\Local\Programs\Python\Python38\python.exe for a user installation. A way to check this path is by typing where python into cmd; it will list all installations of Python.
Now the python.exe you chose is associated with all .py files and typing file.py should not give an error.

"Cannot start process, the working directory ... does not exist" error in PyCharm

When I run my Main.py script, PyCharm keeps telling me Cannot start process, the working directory /home/myname/PyCharmProjects/MyProjectName/mypackage does not exist.
This error occurred after creating a package (mypackage) for test purposes, moving files to it (including Main.py), and moving the files back to the root folder afterwards.
The package mypackage was empty after that, but PyCharm still thought that Main.py is located in that package. I could still run the program until I deleted the empty package, but path errors occurred. After deleting the package, I was unable to start it.
I can still run the other files that used to be in mypackage and are now in my root directory again.
I can still create and run new files in my root directory.
It happens because when you create a file it automatically assigns the working directory to it's configuration, which of course is the one where you created it.
You can change that by going into Run -> Edit Configurations. Click on the folder icon in Script path: and correct the path to the file. Click OK to save and you should be able to Run the file again.
After testing for a bit, I've found a solution (but not an answer to why this error occurs in PyCharm):
Delete the file and create it again. (Or rename or move it and create a new file with its old name, both should work.)
Set the working directory correctly
1. File-> Settings
2. Build, Execution, Deployment -> Console -> Python Console
3. Working directory: [The path to the directory where the file you're currently working on resides.]
I had the same problem, mine is probably related to the explaination gave by the others, it comes from the dir .idea, files *.xml contain the variable $DIR_PROJECT$.
Therefore, as the attribution of a new path didn't work, I just deleted my .idea, that is automatically loaded each time I open my project's directory.
It automatically regenerated the .idea, asked for the script path... And it worked perfectly
CAREFUL => You will automatically lose your project settings, you are deleting the "settings file"
I was getting this same error, and the path in "edit configurations" was correct.
However, this is what eventually got my code working again.
1) I commented out all of the code in my file ("ctrl" + "a" + "ctrl" + "/")
2) I commented something I knew would compile in the file. (my list of imports)
3) I ran the python file.
This time, it actually completely compiled and after that I was able to uncomment the rest of my code and everything worked again.
The issue kept popping up over and over in PyCharm. So I created a new project and loaded the needed script. Then I provided the directory to path and assigned the default Python version that I wanted to use... and it worked. Then I was able to finally use "execute line in console" once again.
I had this problem because I renamed my project, it was "xx" I renamed it to "yy", what I did was I went through the directory of .idea in the "yy", in any of those files (all XML files) if there were the name "xx", I replaced it with "yy"
I experienced this problem after moving my project to a different root directory. None of the above solutions worked for me.
I solved it by opening my entire project folder, instead of just the python file I was trying to run. And then running the file I wanted, while the entire project was loaded into PyCharm.
open the qtdesigner work dictionary setting and choose your project path then click OK, don't not use the mysterious work dictionary path by default
In my case Run -> Edit Configuration didn't help.
I've solved it changing the value of "WORKING_DIRECTORY" attribute in .idea -> workspace.xml
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/your/correct/path/here" />
I am a beginner so appreciated this 6 year and 6 month discussion. I couldn't add a comment. I setup PyCharm 2022.1.1 and a Python 3.10.4 virtual environment with pyside2 and pyside6 on Ubuntu 22.04. I tried all the suggestions above and could get none of them to work for me. Following these instructions: https://pythonpyqt.com/how-to-install-pyqt5-in-pycharm/ both pyside2-designer (Qt5Designer) and pyside6-designer from my virtual environment were added as external tools. pyside2-designer failed with the working directory error and pyside6-designer worked without any error. Looking closer at pyside2-designer selecting the working directory with the browse button, I still could not create file - exit code was 127 - but the Qt5 Designer was available for existing ui files. I used pyside2 because PyQt5-tools could not be pip installed in my python3.10.4 virtual env.
This worked for me on PyCharm 2021.3.2:

Spyder Exception hyperlinks not working with modules located in subfolders of global working directory

The hyperlinks given by Spyder when an Exception is raised seem to only work if their target is the file is an installed module, a module inside the global working directory, or the module that I originally ran.
For a specific example:
I have my 'global working directory' set as:
"C:\Users\Andy\Google Drive\Development\Spyder-workspace"
I'm running a python script located in a subfolder:
"C:\Users\Andy\Google Drive\Development\Spyder-workspace\treetool"
This script at some point imports and calls a function from another python module located in the same (treetool) folder. This imported function raises an Exception.
The hyperlink Spyder outputs to the line of code where the Exception occurred does not work - it just takes me back to the editor, not even to the correct module.
I tried putting the subfolder (treetool) in my Path and also my PYTHONPATH (user) environmental variables, but it didn't help. This kind of surprised me since hyperlinks to installed modules work fine... does Spyder use another environmental variable for this?
Anyone know what I need to do to make this work?
I can change my global working directory, but that's kind of an annoying workaround.
(Spyder dev here) I think you have found a bug :-) If you want this fixed, please: 1) Open an issue in our issue tracker (else I'll forget it for sure), and 2) Upload there a simple but reproducible example I can use to test and debug what's happening.

Categories

Resources