Running Python Files from Pycharm Project Folder with Windows Command Line - python

I have watched numerous tutorials on how to run Python files using the windows command line (CMD.exe), but in all of the tutorials the video makers were accessing PythonXX from Program Files and also their Python files from Program Files. I use Pycharm and have my single Project Folder in my Documents library. If possible I would like to run those files from the project folder. If not, though, is there any way to relocate a Pycharm Project Folder and Pycharm will still recognize it as where I store my files?
Thanks!
EDIT:
YES, I am aware that I can run my Python Files in Pycharm, but I would like to use windows CMD for full screen and so I can watch over it while I code.

To get to a directory with cmd simply hold shift while right-clicking the Documents folder. You should get an option
Open command window here
To run python files, enter into cmd:
python filename.py
Modify this command to include arguments if necessary.

Related

How to convert tkinter script which uses multiple image resources to create a single executable

I want to create a single executable from my Python project which uses tkinter. A user should be able to download and run it without needing Python installed. What can I use to build a self-contained executable from a Python project?
my question is somewhat similar to this one but I didn't find what I was looking for there.
I have a folder which looks like this:
-project/
-script.py
-res/
-image1.jpg
-image2.jpg
-image3.jpg
In my script.py I am using the images from the res folder in my tkinter UI and I want to create an executable out of this script. what would be the best way to tackle this problem?
You can try using PyInstaller
PyInstaller - How to convert to .exe
1. Installation
The pyinstaller module makes you able to create a standalone exe file for pc from a python script, so that you can use it on other computers than do not have python or your python version.
To install pyinstaller: pip install
2. Create an exe
After you installed pyinstaller, go in the folder where your python script to be transformed in exe is and type in the command line (go in the address bar on top of the window and type cmd). Then type :
pyinstaller yourscript.py
where your script is the name of your file.
You will find a folder called “dist” where there is a folder called yourscript (if the name of your script was this) where there are many files and the exe file that will allow you to run the script also on other computer. You just have to copy the whole folder.
3. What to do if there are images
If you use images, that are in the same folder, for example, you can copy manually into the folder made by pyinstaller or you can run this command to make it do it for you:
pyinstaller –add-data “*.png;.” yourscript.py
This way you will see that in the folder where your exe is there are also the png files (all of them, if you want just one do not put the asterisc, but the whole name of the file.
External links - PyInstaller
Use a bundler like Pyinstaller.
Pyinstaller website
Include images in Pyinstaller
Note: If you are having issues with tkinter and pyinstaller take a look at this: Problems with Pyinstaller with tkinter app on python 3.5

Running all script in a directory python

If I have 5 python scripts and they are all in one directory how would I go about running all of them? I dont want to have to go and manually open them and then run them I would like to have python do this.
So i have a folder named Scripts and inside it has five scripts that all have different names with the .ipynb extension.
so the breakdown of the Tree looks like this,
-Scripts
-A.ipynb
-B.ipynb
-C.ipynb
-D.ipynb
-E.ipynb
How could I run each of the files in this directory without manually opening them and running them each individually?
The extension 'ipynb' isn't a python script, but, you can create a batch file which runs all of your .py files.
Example:
#ECHO OFF
Runs my Python Scripts
C:\path-to-python\Python.exe C:\path-to-scripts\A.py
ECHO Ran script A
(Repeat as needed)
PAUSE

bash on Ubuntu on windows Linux, folder recognition, and running Python scripts

I'm new to Linux. I recently downloaded Bash on Ubuntu on Windows 10 (after the Anniversary edition update to Windows 10). Since this update is relatively new, there is not much online regarding troubleshooting. There are two things I need help on:
(1) When I go to the home folder, which seems to be "C:\Users\user\AppData\Local\lxss\home\user" and I add a new folder through Windows, this folder does not show up in Linux with the "ls" command. But when I add a directory using "mkdir" in Linux, the "ls" command shows this folder. Why is it behaving like this? Am I limited to creating folders through "mkdir" when working in this folder?
(2) I have a Python script sitting in that same folder that I'm trying to run and again it is not being found by Linux or the Python interpreter started in Bash on Ubuntu on Windows. I have Python 3 installed (Anaconda) and I'm able to type commands directly in the Python interpreter and it's working. However, I would like to run scripts in files.
Please let me know if more information is needed. Thanks.
The reason why ls is not showing anything is that it shows the Linux directory structure. Try setting it to the Windows directory, in this example the c drive:
cd /mnt/c
Does ls show a folder structure now?
Looks like you are having permissions issues.
To see everything on your home folder try
ls -al
to change permissions check out the chmod command
How about using Python for Windows and NotePad++ to edit and run your Python scripts?
https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe
You can setup NotePad++ as described here.
How to Execute a Python File in Notepad ++?
(I ended up using Cloud9 https://c9.io/ for Python. It is independent of your local environment or OS)

How can I run my Python program like a normal program from the desktop?

I'm creating a program that I would like to use as a normal program as well as continue to code it on the side. To do this I first tried creating a shortcut of the .py file in my PyCharms project folder and sent it to desktop. When I double-clicked the shortcut the command prompt would open for a second and then shut. It's a PyQt4 program so I'm not sure if this has any bearing. The program has been coded in Python 3.4. I've noticed that when I open the command prompt and type 'python' it shows Python 3.5 for some reason so I'm not sure if this has any bearing on the situation.
If you've ever programmed in C# I'd like to be able to build a solution and then rebuild the solution when I've updated the code so that I can access the program as a normal program as well as continue to improve the code of it.
Thanks for any help.
Managed it thanks to the link above. Uninstalled Python 3.5 and set my PATH variable to C:\Python34. Downloaded pyinstaller and installed it using PIP. Then navigated to Python34/Scripts and dragged myFile.py (the one to be made an .exe) into it. Ran pyinstaller.exe --windowed myFile.py to create the exe which then went to my dist folder. Created a shortcut and it worked perfectly.
Go to your environmental variables (Right click on Computer > Properties > Advanced system settings > Environment Variables...). Find Path in System variables, select it, and click edit. Remove the Python 3.5 path and replace it with your python 3.4 or virtual environment folder that has python.exe in it.
Make a shortcut on your desktop that points to the .py file that you are editing.
If you have all of the dependencies right then double clicking the .py file's shortcut should run your program.
Other wise you can pip install cx_freeze and use cx_freeze like setuptools. Create a setup.py file and build the executable.
If you want to install this executable I suggest using Inno Setup. It is pretty straight forward on how to use and has an easy wizard that helps you build a basic installer.

Using pyinstaller to make standalone file on Mac

I'm trying to use pyinstaller with python version 2.7 to make a standalone file that will run a small app I made using tkinter, but I'm having trouble following the documentation. I found this, but it's for python 2.5 and windows
http://www.pyinstaller.org/export/v2.0/project/doc/Manual.html?format=raw#spec-files
I made two folders on my desktop, one for pyinstaller and one containing my files.
In terminal I changed the directory to the pyinstaller folder. Then I typed the following:
python pyinstaller.py /Users/.../Desktop/BuildFolder/menu.py
//buildfolder contains my files, menu.py is the file I run to start the app
When this runs, I get a message staing something along the lines of "The output directory and all of its contents will be removed. Proceed y/n?" Upon selecing yes, It then creates the directory
/Users/.../Desktop/pyinstaller-2.0/menu
Inside that menu folder, there's a .SPEC file. So, I run the following in terminal
python pyinstaller.py /Users/.../Desktop/BuildFolder/menu/menu.spec
After this runs, I found a .PKG file in that folder but it's not labeled anything like menu.py . I can't figure out what to do next, and I'm not sure if I'm supposed to run other files.
I am not sure ( haven't tried this yet ), but maybe you have to run Build.py while working with .SPEC file?

Categories

Resources