I think I am close to solving my issues but need a little help.
Computer: Mac
My situation: wrote a script in Jupyter Notebooks (.ipynb) and I wanted to run it from IDLE. So I downloaded the .ipynb as a .py file. When I open up the file it opens in Idle and when I go to run the file it says I do not have the module/packages installed.
At the top of the .py file it put #!/usr/bin/env python
When I run which python in my terminal I get /opt/anaconda3/bin/python.
So I am thinking IDLE is running the script through a different environment of python. How do I change it so IDLE will run the script through the python Anaconda also uses?
Thank you!
It is possible that you have more than one python in your OS. Just check the versions in prompt with --version. If you get different python version you need to delete one that you dont use. It is generally the other than anaconda one.
Related
I've been studying Python for a month now and normally I run all my programs in Sublime Text 3.
Today I learn to run Python programs in the terminal window as introduced in this section of the Automate the Boring Stuff with Python book following this video. Basically, I followed the instruction in the video and created the hello.py file as below:
#! python3
print('Hello, World!')
Then I opened the Command Prompt to run the file with the command: py.exe c:\users\danh\mypythonscripts\hello.py,
an error pops-up and states that "This app can't run on your PC" and a line says that Access is denied. I spent the whole day trying to fix this problem but still I couldn't get it running.
One thing is when I change the directory of the Command Prompt to run the file to C:Windows\system32 (or run the Command Prompt as Administrator) and then run the command py.exe c:\users\danh\mypythonscripts\hello.py, it runs the file without any problem as in this image.
Does anyone know how to fix this problem? Any help is appreciated. Thanks!
I solved the problem.
When I looked into my user directory at C:\Users\<Username>, it appears that there is a py.exe file that has 0 bytes.
I was told in this thread that the py.exe file shouldn't be in my user directory so I removed that file and it fixed the problem.
I still don't know how the py.exe file got into my user directory and why it has 0 bytes so I'm not sure this solution could help others. For now, I will accept my own answer because it solves the problem in my case.
It looks like you're trying to use Microsoft's new Windows 10 Metro-based auto-installing version of Python. It's included by default but, as you've found, it doesn't work very well.
Try installing the version from the Python website.
If you've got a 32-bit copy of Windows, make sure to install the 32-bit version; Windows isn't very good at running 64-bit programs from a 32-bit kernel. You can check by looking in your C: drive; if you haven't got a Program Files (x86) folder, install the 32-bit version.
python.exe inside my env\Scripts\ became 0kb for some reason. So I created another virtual-env and copied python.exe from there to this folder. then it started working.
I wrote a program that use pyperclip module and it would work from Pycharm and python IDLE, would work as well if starting from Powershell but if I try to start the program from WIN+R, when launched, the program returns an error saying that pyperclip module is not installed. The same problem appears when I run it from the Anaconda Powershell Prompt.
PLEASE NOTICE:
The program was working perfectly before I installed Anaconda and Jupyterlab.
The error occurs when I run the program from the cmd using WIN+R AND when I run it from the Anaconda Command Prompt but it's fine when run from IDLE, Powershell, Pycharm.
I always used python 3 and only yesterday I installed Anaconda.
Thanks for the help!
I just spent couple of hours trying to solve exactly the same problem. What I found out is that, as couple of members have already pointed out, the main problem is the mismatch between the version of the python that runs in cmd and the versions of the python used in scripts/batch files.
The first line in the code, known as "shebang", in .py file indicates the version of the python that the script should use when it is executed. So, it must match the version run by default with cmd (or when executed with win+R). In my case, I also have a batch file (.bat) that calls specified version of the python, which should be the same version used with .py file.
The problem was that both of my files (.py and .bat) were calling python 3.8 while the cmd is running version 3.7. Initially, I used shebang #! python3 in my .py file, and command #py path/to/python/file.py %* (and I also tried #py.exe path/to/python/file.py %*) in my .bat file, and that did not work.
To solve the problem, I updated these two files to link to python version 3.7 with following:
changed shebang in .py file to #! python
changed command in .bat file to #python path/to/python/file.py %*
With these changes the system runs the program with win+R.
whew, taking course on python and instructor had py.exe instead of python.exe in .bat call. despite trying all of the other installation methods others have mentioned, just changing this to python.exe did that trick.
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.
I wrote a script on my Windows machine using python 3.6.0. I wanted to run it on Linux machines as well so I transferred the .py script onto my Ubuntu VM. The problem is, I tried to install python 3.6.0 in every way possible but it doesn't really work. Python3 works when I want to execute a .py file, but since I am trying to use pyisntaller to convert it into an executable for linux, I need the default version as 3.6.0. python --version gives me Python 2.7.2. How can I fix this so when I do python --version it shows 3.6.0?
There is no point in changing your default python version, Ubuntu relies heavily on older versions of python. And what do you mean by convert it into an executable for Linux? Almost every file on your file system is an executable. If you want to make your program run as a command in the terminal you can do this though:
text_editor_of_your_choice .bashrc
This should open the hidden .bashrc file, and scroll to the bottom of it. Below is what the file looks like...
.bashrc
After that you can create a function for the python command you want to run.
eg. function ghst {python3.6 /home/user/example_script/example.py $#}
Start a terminal session and then try running it. Now that's if you want to run it inside the terminal. If it you want to create this "executable file" you speak of you can create a simple shell script.
First go in the terminal and run touch file_name_of_your_choice.sh
This will create a blank file named file_name_of_your_choice with the file extension "sh". After that open the text file in whatever directory you created it in, if you are unsure do pwd.
In your first line make sure that the user is inside the directory of the .py file by doing
cd /home/user/folder/ On the next line you should then execute the .py file. You can do this by typing this: python3 file.py. This will execute the program inside a terminal window. After you have done all of this make sure that the python file and the script file are executable. Do this by doing chmod +x file.py and chmod file.sh.
This is what the end result looks like
example script
You mentioned inside your question that you want your program to be able to run on Linux machines as well. Whether this is for personal or a public project of some sort it comes in handy by packaging all the required files in a .zip format this makes it easy to unpack everything when moving to separate machines. If there are any problems let me know.
I seem to have problem launching python from command line. I tried various things with no success.
Problem: When trying to run python from the command line, there is no response i.e. I do not get message about 'command not found' and console does not launch. Only option to open python console is to run C:\Python34\python.exe directly. Running using python command does not work even when in the python directory but python.exe launches. Issue with the launching this way is that python console is launched in new window. This whole problem is present only on one machine while on my other machine I am able to run python correctly and console launches in the command prompt window from which the python command was executed.
PATH is correctly set to
C:\Python34\;C:\Python34\Scripts;...
and where python correctly returns C:\Python34\python.exe. I verified that running other commands imported through PATH (such as javac) run correctly.
Things I tried:
Completely re-installing python both with x86 and x64 python installations with no success.
Copy installation from my second machine and manually set the path variables - again no success.
Can anyone hint how to resolve this behavior?
(Additional info: Win 8.1 x64, python 3.4.2)
Issue resolved. Since no feasible solution was found in 2 days, I decided to wipe all keys containing 'python' from registry as well as some files that were not parts of other programs. This resolved the issue after re-installing python.
If anyone finds the true cause of this misbehavior and other - less brutal - solution, please write it here for future reference.
Recent Python installer has option to add PATH.
If you didn't use it, you can register directory where python.exe is to PATH environment variable.
But I prefer py launcher. It may be installed via Python 3.3 or 3.4.
With it, you can start Python via py or py -3.4.
See https://docs.python.org/3/using/windows.html#python-launcher-for-windows