I am trying to run the example programs that come with Visual Python ("VPython") like bounce.py, orbit.py, etc through bash.
When I'm in the folder which contains these examples (C:\Python27\Lib\site-packages\visual\examples), I can right-click on any of the examples and choose "open with" and then select "python.exe" from the list of given programs. This method is able to run the programs just fine and it generates the visual representation of a ball bouncing in a room, orbiting balls, etc.
On the other hand, when I use bash to navigate to this folder and then try to run python bounce.py, I get the error message:
Traceback (most recent call last): File "bounce.py", line 1, in
from visual import * ImportError: No module named visual
I was under the impression that calling python bounce.py was effectively the same as opening the file with the python executable but the behavior is different. I'm not sure why the "open with python.exe" method would allow the example to find the visual module but opening the example through bash is unable to find the same module. Any advice or help would be appreciated.
(Side note, I am running Bash on Ubuntu on Windows through the new beta https://msdn.microsoft.com/en-us/commandline/wsl/about)
The Linux environment (Bash on Windows) includes a builtin python, which does not have the package visual.
If you double-click the file, right-click and then open with python.exe, or execute the script via cmd or powershell, it will use the Python installed on the Windows machine. If you execute the script in bash, it will use the Python in Linux subsystem. That's it.
Related
I am on a Mac OS. Why are there two of them "Python" and the other one on top is 'Python launcher’.
image - (file:///Users/Buddhikawijegunarathna/Desktop/Screenshot%202022-03-28%20at%203.22.27%20PM.png)
What is the difference?
"Python launcher" is in Applications/python 3.10/Python launcher
(the current version I use of python is 3.11)
"python" is in the Macintosh HD/library/frameworks/python.frameworks/resources/python app
I can't run python files that use modules using 'Python launcher' but can using the ‘Python' app.
And I can run a python file from anywhere, maybe desktop or in a folder or anything by using 'Python launcher’, but in ‘Python' app it either works in the desktop or a specific place, and strictly not inside folders. (if I run, it's displaying an error as the directory cannot be found.)
Python is a computer language, also means an overall of the whole Python system;
While Python Launcher is an executable file which launches Python compiler & functions when you use Python.
I am trying to create a python script to automatically run unit tests in Visual Studio. One path that I am exploring would involve using shells, in particular Developer Powershell. I am running into a roadblock with accessing this shell, though. Here's what I have so far:
First off, the Visual Studio solution has all of the necessary files; I just need to build and run, which I can do in Developer Powershell using the following lines
> MSBuild.exe .\project.sln
> VSTest.Console.exe .\x64\Debug\project.dll /Logger:trx
To get to Developer Powershell, I can type the following into a normal Powershell:
> C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 64375397}"
And if I need to start from CMD for some reason, I can get to Developer Powershell by simply typing this:
> %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"
My issue is trying to put all of this in a python script. I can open Developer Powershell using os.system, but then I can't use os.system again until I exit Developer Powershell. Is there a way to let the python know that I am in a new shell and to start interacting with that one?
Okay, I figured it out, but I'll just leave this all up in case anyone has a similar question:
MSBuild and VSTest.Console are both just .exe files, and as such just have their own locations that you can target in a shell (links on where to find the are below).
MSBuild
VSTest
If you just run > & "C:\<The_path>\MSBuild.exe", it'll work fine with the arguments above. No Developer Powershell needed.
You can even use this stack exchange question to generally find the .exe files on any Windows system.
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.
Yes I want to create a run configuration in PyCharm to run Pyinstaller and get my executable. According to the Pyinstaller documentation you should be able to locate an python script called pyinstaller-folder/pyinstaller.py after the installation, but it wasn't there. Then I look carefully and found this other one named pyinstaller-folder/__main__.py which should be the same <--(me wild guessing), so I set up my running configuration like this:
After running it, is giving me this error:
/usr/local/Cellar/python3/3.4.3/bin/python3.4 /usr/local/lib/python3.4/sit
e-packages/PyInstaller/__main__.py --onefile --nowindow --osx-bundle-identifier=jg.optimizer -F --name=genoptimizer optimizer/manage.py
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/PyInstaller/__main__.py", line 26, in <module>
from . import __version__
SystemError: Parent module '' not loaded, cannot perform relative import
Process finished with exit code 1
It seems to require a parent module to run but, how would that look like?
After more than two years, perhaps there is a better option.
In the PyCharm menu go to File -> Settings.
In the Settings dialog find Tools -> External tools and use the green + to add a new external tool.
For example:
Then, the IDE will allow you to run it on any python script. Right click on the file and the context menu will show External tools -> PyInstaller.
The PyInstaller package is a runnable module and can be run using python -m PyInstaller. To configure it as a run target in PyCharm, leave the "Script" field blank, write -m PyInstaller in the "Interpreter Options" field, and put the PyInstaller parameters into the "Script Parameters" field.
For example:
PyCharm will complain that the "Script" field is empty, but it will let you run the configuration anyway.
The run command has changed a bit since the accepted answer. You can now select a module to launch rather than editing the interpreter options.
As of writing this answer, here is how it looks like:
Notes:
This solution requires to install PyInstaller in the virtual environment of the project.
I am using PyCharm pro 2020.1
Old solution should still work
Using external tool is still a possibility. Which solution you choose is a matter of personal preference.
Install pyinstaller in pycharm, then open the pycharm terminal and write python -m PyInstaller.
So as Pycharm has newly updates, my case was a bit different as I installed pyinstaller from the interpreter settings as the following picture shows:
For Linux Users:
You could install it in both Python 2.7 or Python 3.7+. Make sure to get the path of where pyinstaller was stored.Then in the Settings option, try to find Tools -> External tools and add a new external tool as the following picture shows:
For Windows users:
If you are using Pycharm or any virtual environment. Unfortunatelly Pycharm creates its local vertual environment in venvpath once you indicate the interpreter. So, you should set the external tool (pyinstaller) to the real path of your python 3.7 .exe as the picture shows
For those of us on Windows with Anaconda trying to figure this out, I found it easiest to just set up a Bash Configuration (I believe you need the BashSupport plugin for this), and set:
Script: pyinstaller (assuming pyinstaller is in your path, if not, the full path)
Interpreter path: C:\Windows\system32\cmd.exe (yes, a bash configuration can just use the standard command program)
Interpreter options: /C
Program arguments: script_name.py (along with any other pyinstaller arguments)
Working Directory: C:\Path\To\Script
I want to create a .bat file to run my .py file on Windows without Python installed, mainly so I can send my programs to friends. So in my .bat file I would like to:
Change to the current working directory. (So it runs wherever the file is)
Change directory to a folder inside that directory.
Run a python file without my friends having to install python themselves.
Run a .py file without Python installed using .bat
You can't run a Python script using a batch command without Python being installed. You can compile an executable with Py2exe (which bundles an entire Python interpreter with your script), or convert a subset of Python to C++ with shedskin, which can then be compiled to an executable. You could also issue a shell command to install Python if it's not already installed and the user has internet.
But doing exactly what you asked is impossible.
Take a look at http://www.py2exe.org/ which will convert you python code to a executable windows program that you can send to your friends.