I've tried searching for why this keeps happening as I think I've seen others mention this problem.
Whenever I run manage.py runserver from the command line, it just opens the manage.py file from the project directory in notepad. If I include python in the beginning of the command, it says that 'python' is not recognized as an internal or external command, operable program or batch file.
I'm using windows now. I was running linux before and it was working fine...I just transferred all the files that I was working on onto windows and that's when this problem started happening.
To reiterate, the only thing that happens when I run the runserver command is that the manage.py file opens in notepad and it contains the following:
#!/usr/bin/env python
from django.core.management import execute_manager
import imp
try:
imp.find_module('settings') # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
sys.exit(1)
import settings
if __name__ == "__main__":
execute_manager(settings)
Your file associations are messed up.
For some reason .py files are associated with notepad.
They aren't associated with Python.
If you know something about windows, you can change the file associations.
if you don't know much about windows, delete and reinstall Python. This time, however, allow it to setup the file associations for you.
Open control panel > Control panel home > Default Programs > set Associations
Search for .py
Select python.exe
Worked for me
On windows I right clicked on my manage.py file in the project directory and then selected open with, then chose another app, then python IDLE, then set the check mark at the bottom of the window to always use this app to open .py files and this was resolved for me. Hope it works for others as well!
I would suggest you create a new virtual environment and then reinstall python and django. That should fix the problem.
Related
I seem to be having a vscode related issue. I am doing the open() function but no matter what I ask it to do it gives me a directory error. The file that I want the python script to interact with is in the same folder so it should work but when I do "import os" and "os.getcwd()" the directory it says I am in is Desktop. (the script and file are both in the "/Desktop/Python/File Handling" directory)
It seems the script is stuck at the Desktop directory when I try to run it from vscode. If I run it by doing python3 "name of script" command in the kali linux terminal it works fine and if I check my directory again with os.getcwd() it says the correct one (/Desktop/Python/File Handling).
So I believe it's something with vscode as it literally just randomly happened one day. Yesterday my scripts were working fine and now all the ones I run from vscode, that are supposed to interact with the files in their respective folders, don't work. The vscode terminal gives me this code and as you can see it does the cd command at the start, which I believe might be the issue for why it always looks at files in the Desktop directory but I do not know how to make it stop doing that.
$ cd /home/kali/Desktop ; /usr/bin/env /bin/python /home/kali/.vscode/extensions/ms-python.python-2022.18.2/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 41017 -- /home/kali/Desktop/Python/File\ Handling/File\ Handling\ 2.py
Traceback (most recent call last):
File "/home/kali/Desktop/Python/File Handling/File Handling 2.py", line 3, in <module>
f = open("apple.jpeg", "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'apple.jpeg'
Lastly, I know about the os.chdir(r"/home/kali/Desktop/Python/File Handling") function and it fixes the issue but I do not want to have to write this command at the top of every script that is supposed to interact with the files in the folder it is in by default and because this issue just randomly came up out of nowhere the next day I opened vscode and ran my script from yesterday (without changing any code or vscode settings.)
P.S. I am using a VM as well if that helps.
This is caused by vscode using workspace as root floder.
This will lead to a problem. When you use the os.getcwd() method in the deep directory of the workspace, you will still get the workspace directory.
You can open your settings and search Python > Terminal: Execute In File Dir then check it.
You can also use debug mode and add the following to your launch.json:
"cwd": "${fileDirname}"
I am setting up a PC running Ubuntu 20.04.3 with a touchscreen and no keyboard/mouse. Due to this, I am putting desktop icons that will run the software needed for data taking/analysis. I was able to set one up with no problem first using a .sh file that executes with a single click, then by using a .desktop file.
The problem I am having is that this will not work with a python3 file that runs tkinter and ROOT. I started off by writing the shebang #!/usr/bin/env python3 at the beginning of the .py file and the .sh file that executes the python file. When I click on the .sh file, nothing happens. If I run the .sh file on the terminal by typing ./Bash_File.sh, it executes with no issues. Also, I did make the .py executable, so I can also run it by going to the directory and typing ./Python_File.py.
I then decided to try with a .desktop file, similar to the first program I got to work. I tried executing both, the .sh file and the .py file, from the .desktop file and I get the same error: ModuleNotFoundError: No Module named 'ROOT'. In the .desktop file, I execute the .py file using Exec=/filepath/Python_File.py. Again, I am able to run this file through the terminal, but not having any luck using the icons to run it. I've looked everywhere online for an answer, but no luck. Thanks for the help
Disclaimer: Generic answer as I know nothing about the ROOT module.
This is a python error, it can't find the ROOT module, mostly like a specific file named ROOT.py. The cause is that a Bash login session is different than a Bash system session.
Run this script from a desktop launcher and in the shell:
#!/usr/bin/env bash
echo $PATH | tr : \\n >my_path-`date +%s`
If the two output files are different, figure out which directory contains 'ROOT.py' and add this to your launcher script:
#!/usr/bin/env python3
import sys
sys.path.append('/the/missing/directory')
If this doesn't set you up right away, there are many articles on managing the Linux system path, the difference between a regular Bash shell and a Bash login shell, and how to manage your Python path. HTH
I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave
I made a simple python script which creates a text file. The contents of the script are
f = open("New", "w")
f.close
Now I moved the script to the desktop (I'm on a mac) and ran it, and nothing shows up. No file is created on the desktop that is visible for me.
I actually made this little script because one of my other scripts involves opening/creating a text file and reading from it. I entered the information wrong while in my program forever, and now the entire thing is broken and I cant fix it because I have no idea where it's created the darn text file. I was under the impression that just opening a file with giving it an absolute path would create it in the same directory as the script. I seem to have been mistaken.
I've been launching the script from the terminal with the command
python3 /Users/me/Desktop/script.py
Because of that, I feel like its creating the file somewhere in the python3 install location or within wherever the python3 unix exec is located. I think. Can't check.
Are any of you guys willing to help out?
-pipsqueaker117
EDIT: Here's a link to the big program which broke.
It'll be created in the current working directory, which is the directory from which you called the script.
Your file will be created in the current directory (most probably in /Users/me/ if you just opened the terminal)
Try:
cd /Users/me/Desktop/
python3 /Users/me/Desktop/script.py
You should modify your program to change the working directory (before you write the file) to the place where you want files to show up.
import os
os.chdir('/Users/me/Desktop') # or whatever
This should be a directory where you have permission to write files.
You can always ask:
import os
cwd=os.getcwd()
print(cwd)
That will print the directory the file (without a path) will be opened in.
You can change to a specific directory (in Python) this way:
import os
try:
os.chdir('/Users/me/Desktop')
except OSError as e:
print e
I have purchased PyCharm and am trying to get things to work however I am encountering this issue..
Once I start a project everything works great...
Now if I want a standalone app.. let's say at /users/me/djangoApps I understand I have to add this directory to the python path.. I am trying to do so by creating a file sitecustomize.py at lib/python/2.6/site-packages/
However once I create an app and try to import it I keep getting non excistance errors (yes I have reloaded the python interpreter in pycharm)
I reckon I am adding my locations to the python path in the wrong way.. Also I might not have my project location setup correctly (currently /users/me/djangoProjects)
Thanks,
Novice django'r
In pycharm open the settings "cmd" + "," and then to "Project Structure" click on "Sources" to include any modules.
Don't add that file to your python site-packages, then your django project is gonna be included for all future projects down the road.
If you wanna debug, within PyCharm, click the Run tab up top and choose Edit configurations. Choose the project you are working with and make sure you add the directory where your manage.py and settings.pr file are to the "Working Directory". So I assume it might look something like this:
Working Directory: /users/me/djangoProjects/{Project Name}
If there is something else that you need to add to the Python Path, you can add it it by going to File-Settings-Python Interpreter and then add a new path in the bottom window (but once again this will be used by any project you run in PyCharm
But if you are not debugging in PyCharm and just wanna run the app, I find it easier to run it from the command line. I assume you are on Mac by your path, open the Terminal and go to the directory where your project is (same directory as the manage.py file) and type:
python manange.py runserver
If you want to give it a specific port add it to the end
python mange.py runserver 9000
This way you can edit your code in PyCharm and it will get reinterpreted when you save the file. If you are debugging in PyCharm, you need to stop the debugger and run it again to pull in your changes