When I run a Pygame script through an editor such as VSC or IDLE, the script works perfectly fine. When opened with Python through the file explorer, nothing appears. This only happens when I add import pygame to the script.
For example,
import pygame
i = 0
while i < 9999:
print(i)
would work only on VSC.
I downloaded and used pip to install pygame, and when I convert to a .exe, it still doesn't run.
As the comment says you likely have multiple versions of python installed.
Probably one you downloaded from python's website or through VSC.
Microsoft Store Python is probably located here:
cd C:\Users\<your name>\AppData\Local\Microsoft\WindowsApps
Uninstall that and check your path to python in VSC. Or delete all versions of python, download it anew, and check the PATH box when installing. This will handle the PATH variable for you, and VSC will ask if that's the path you want to use next time you launch it.
Also, you might want to look into using virtual environments. Python3 has this built in and you can create it via the command
python -m venv venv
then type cd (for powershell/cmd) or source(for bash and Terminal) venv/Script/activate to activate the virtual environment. This way you can avoid installing dependencies globally. When inside the virtual environment you will still use pip to install pygame and other dependencies without breaking you program due to updates.
I'm not sure what causes your .exe issues.
More about virtual environments here.
Related
I have a Python Project that has a bunch of dependencies (in my VirtualEnv). I need to run my project in my school computer for Demonstration. My School computer does not have python installed, and lets assume also wont have an Internet connection to install it. I have written the program in Windows 10, and the school computer runs Windows 7.
I have looked at these solutions so far, and also here is why I think they may not work.
Copy and pasting my virtual Env - Doesnt work because venv's have their own structures and has my username in its paths which it will look for in the other system.
Using Py2Exe. I have an Exe file, that I can now run on other systems running Windows 10 without them having python or any of my packages. But I am not sure the VC++ dependencies will be present in windows 7. It may also have some other weird issue that I cant risk.
Docker. I am not familiar with Docker, but can do it if this happens to be the only way.
How can I run the python file in that computer?
Also note that I will not have the time to mess around in the other system. Ideally I must plug in my USB and Open the file to run it. If you think there isn't a solution to this, please let me know by telling so.
Thanks!
I see two options.
Use an online IDE and Python Interpreter (assuming you did not have internet for downloading Python, but do have internet in general). I suggest replit.
Use a portable version of Python. Those are available in the official website and are called "Windows embeddable package". You can test downloading it to a usb, and running it in some computer without Python; it should work.
You can use PyInstaller to generate an exe file from your code. It runs without installation.
Or you have a look at the WinPython distribution which is portable and comes with several tools and packages pre-installed.
Note that Windows 7 only supports Python up to version 3.8. 3.9 is only supported on Windows 10 and will silently fail to run without giving you any hint.
Try python as a portable version.
Download the python Windows embeddable package(zip package) and extract it to your flash drive.
https://www.python.org/downloads/windows/
In extracted python folder, press the shift key + right click and select open command window(windows 7) / open powershell window here(windows 10) option.
Type './python' and hit the enter key.
Convert that python file to a .exe file using auto-py-to-exe. This would convert your .py to a .exe file which you can run anywhere.
To use auto-py-to-exe, just execute the following command on terminal pip install auto-py-to-exe.
Now on the terminal write auto-py-to-exe and press enter. Select the python file you wanna execute anywhere and click on Convert .py to .exe and you would get the folder containing the .exe file. Transfer this folder to any computer and just by clicking the .exe file that is there inside the folder, the program would start executing normally no matter if the computer does not have python or pycharm installed.
This is how I cloned a Windows Python project from a source machine to a target machine without internet connection where Python isn't installed.
Thanks to conda-pack tool (https://conda.github.io/conda-pack/).
On the source machine
Install Anaconda (https://www.anaconda.com/products/individual).
Then from Anaconda prompt type the following commands.
conda activate
conda update -c defaults conda
conda install conda-pack
conda create -n <my_env_name> python=<python_version_number>
conda activate <my_env_name>
# if using Python Windows extensions:
conda install pywin32
Now install the packages you need for your Python project using conda or pip (https://www.anaconda.com/blog/using-pip-in-a-conda-environment).
For instance "conda install <package_name>" or "pip install <package_name>".
And finally export everything to a zip file:
# Pack Python environment my_env_name into my_env.zip
conda pack -n <my_env_name> -o my_env.zip
On the target machine
The OS of the source machine must match the OS of the target. This means that environments built on Windows can’t be relocated to Linux.
Unpack my_env.zip and then execute the following commands from Command Prompt.
call Scripts\activate.bat
conda-unpack
# At this point the Python environment is exactly as if you installed it here directly
I use https://colab.research.google.com. It will work on any computer, but some codes can't be submitted there.
Edit: It's working fine now, it seems there was just something weird going on with VSCode
I am writing a discord bot in Python using Gitpod (which has VSCode Browser as its editor). I have installed Pycord 2.0 in the workspace using pip (obviously) but VSCode doesn't seem to be recognising it. I also have the Python VSCode extension installed which adds a 'Run' to the editor when a python file is selected. It used to work just fine but since a month ago it stopped working and the 'Run Code' button is running some sort of Python installation where none of the modules I installed are there. It also doesn't show any code hints for the modules.
I have tried pip show py-cord and changing the selected VSCode python interpreter accordingly. I've also tried changing th interpreter and installing Pycord into that interpreter but none of that worked.
I am running Gitpod on an iPad 6 (model A1893, iPadOS 15.6.1) in Safari
Does anyone know what might be wrong?
Thanks :)
~ R2509
Many developers have multiple python versions installed, and multiple environments with the same python versions, but different libraries installed. Therefore, we need to specify which python compiler VSCode needs to use.
Check on the bottom right of your VSCode, there should be a number down there representing an installation of python.
Upon clicking on it, VSCode will open a window on the top part, allowing you to select which of the many pythons you have installed you want to use. Find the one where you installed your libraries, and you should be good to go.
Method one:
Use pip show Pycord to see where you installed the package Pycord
then choose that environment
Method Two:
Choose a python version you want to use ( Ctrl+Shift+P --> Python:Select Interpreter )
Create a new terminal activation environment
Install the package using pip install pycord in the new terminal
As a suggestion:
you can use a virtual environment to better manage python versions and various packages.
# Create a virtual environment named .venv
python -m venv .venv
# Activate this virtual environment
.venv\scripts\activate
I have a Python Project that has a bunch of dependencies (in my VirtualEnv). I need to run my project in my school computer for Demonstration. My School computer does not have python installed, and lets assume also wont have an Internet connection to install it. I have written the program in Windows 10, and the school computer runs Windows 7.
I have looked at these solutions so far, and also here is why I think they may not work.
Copy and pasting my virtual Env - Doesnt work because venv's have their own structures and has my username in its paths which it will look for in the other system.
Using Py2Exe. I have an Exe file, that I can now run on other systems running Windows 10 without them having python or any of my packages. But I am not sure the VC++ dependencies will be present in windows 7. It may also have some other weird issue that I cant risk.
Docker. I am not familiar with Docker, but can do it if this happens to be the only way.
How can I run the python file in that computer?
Also note that I will not have the time to mess around in the other system. Ideally I must plug in my USB and Open the file to run it. If you think there isn't a solution to this, please let me know by telling so.
Thanks!
I see two options.
Use an online IDE and Python Interpreter (assuming you did not have internet for downloading Python, but do have internet in general). I suggest replit.
Use a portable version of Python. Those are available in the official website and are called "Windows embeddable package". You can test downloading it to a usb, and running it in some computer without Python; it should work.
You can use PyInstaller to generate an exe file from your code. It runs without installation.
Or you have a look at the WinPython distribution which is portable and comes with several tools and packages pre-installed.
Note that Windows 7 only supports Python up to version 3.8. 3.9 is only supported on Windows 10 and will silently fail to run without giving you any hint.
Try python as a portable version.
Download the python Windows embeddable package(zip package) and extract it to your flash drive.
https://www.python.org/downloads/windows/
In extracted python folder, press the shift key + right click and select open command window(windows 7) / open powershell window here(windows 10) option.
Type './python' and hit the enter key.
Convert that python file to a .exe file using auto-py-to-exe. This would convert your .py to a .exe file which you can run anywhere.
To use auto-py-to-exe, just execute the following command on terminal pip install auto-py-to-exe.
Now on the terminal write auto-py-to-exe and press enter. Select the python file you wanna execute anywhere and click on Convert .py to .exe and you would get the folder containing the .exe file. Transfer this folder to any computer and just by clicking the .exe file that is there inside the folder, the program would start executing normally no matter if the computer does not have python or pycharm installed.
This is how I cloned a Windows Python project from a source machine to a target machine without internet connection where Python isn't installed.
Thanks to conda-pack tool (https://conda.github.io/conda-pack/).
On the source machine
Install Anaconda (https://www.anaconda.com/products/individual).
Then from Anaconda prompt type the following commands.
conda activate
conda update -c defaults conda
conda install conda-pack
conda create -n <my_env_name> python=<python_version_number>
conda activate <my_env_name>
# if using Python Windows extensions:
conda install pywin32
Now install the packages you need for your Python project using conda or pip (https://www.anaconda.com/blog/using-pip-in-a-conda-environment).
For instance "conda install <package_name>" or "pip install <package_name>".
And finally export everything to a zip file:
# Pack Python environment my_env_name into my_env.zip
conda pack -n <my_env_name> -o my_env.zip
On the target machine
The OS of the source machine must match the OS of the target. This means that environments built on Windows can’t be relocated to Linux.
Unpack my_env.zip and then execute the following commands from Command Prompt.
call Scripts\activate.bat
conda-unpack
# At this point the Python environment is exactly as if you installed it here directly
I use https://colab.research.google.com. It will work on any computer, but some codes can't be submitted there.
I have python installed through windows store and I can install programs using pip, but when I try to run said programs, they fail to execute in powershell.
How can I make sure that the necessary "scripts" folder is in my path? I never faced these problems when installing from executable.
For example, "pip install ntfy" runs successfully in Powershell.
The command "ntfy send test" fails telling me the term is not part of a cmdlet, function, etc. etc.
The 'ntfy' program is located here /mnt/c/Users/vlouvet/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0/LocalCache/local-packages/Python37/Scripts/ntfy.exe
What is the recommended way of editing my path so that programs installed via pip are available across windows store updates of the Python language?
In advance
I highly recommend you not to use python installed from the Windows Store, because you'll face such errors, and even more nasty ones.
The easy solution
Create a virtual environment on a more accessible folder, for example in C:\Users\<user>\python. To do so, do the following:
Using PowerShell, go to your user folder, using cd(Note that usually PowerShell already starts inside your user folder. This is an important setting to have, and if not, you should change your powershell starting point to this folder for the future.);
Now that you're in your user folder, type in the PowerShell mkdir python; cd python;
Now, to create a virtual environment, type python -m venv venv;
(You can verify that your virtual enviroment has been created by listing the folders, with the command ls);
You have created a virtual environment. Now, you need to activate it. To activate, run the following: ./venv/Scripts/activate;
Now, you have fully created and activated a virtual environment for your current PowerShell session. You can now install any packages / programs using pip.
After that, the only thing that you need to do is to add C:\Users\<user>\python\venv\Scripts to your Path, and you're good to go.
Caveats
By adding this folder to your Path, you may be using an outdated python version in the future, since the Scripts folder inside your virtual environment also adds a python executable that will be enabled in the path.
The recommended solution
As I stated before, I do not recommend to have the Microsoft Store version of python installed on your machine. That said, you're probably using it for the conveniences of having the latest Python version installed as soon as they're released. To alleviate this need while also getting rid of your MS Store Python. I recommend you using Chocolatey to install python (and pretty much any other programs for development).
What is Chocolatey?
Chocolatey is a package manager for Windows, pretty much like apt-get for Ubuntu Linux or HomeBrew for MacOS. By using a package manager, you get rid of the hassle of always having to execute the (mostly annoying) install wizards on windows.
To install Chocolatey:
Go to chocolatey.org/install and follow the install instructions;
(Recommended: Take a look on their documentation later to see what Chocolatey is capable of);
With Chocolatey installed, take a test drive and see if it is working properly by running choco -v in PowerShell;
By having Chocolatey installed, you can now run choco install python -y. Let's break down this command:
choco install -> The package installer of chocolatey
python -> the name of the package you want to install
-y -> This tells the installer to skip install verification by saying "Yes to All" scripts that will be executed in order to install a package.
With python installed from chocolatey, you can also see that Python is already added to your path - This means that any python package or executable installed globally will be now available on your machine!
Hope I could help you!
The above answer is good but I managed to get it to work by doing the following.
Find your installation under C:\Users\"your user"\AppData\Local\Packages it will be named something like PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
Open your windows settings in the start menu
In search type Environment variables. Edit environment variables for your account should pop up. Click it
In the top box find Path, click it
On the right Click new and enter C:\Users\"your user"\AppData\Local\Packages\"python install directory name from 1. here"\LocalCache\local-packages\Python37\Scripts inside the little box under the last item in the list
open a new cmd prompt and type the script you wanted it should work.
On Windows you can find the user base binary directory by running
python -m site --user-site
and replacing site-packages with Scripts.
For example, this could return
C:\Users\Username\AppData\Roaming\Python36\site-packages
so you would need to set your PATH to include
C:\Users\Username\AppData\Roaming\Python36\Scripts
You can set your user PATH permanently in the Control Panel. You may need to log out for the PATH changes to take effect.
I've been using python in both my office pc and my personal laptop and on both machines typing in powershell:
pip install somepackage
works normally.
Strangely enough, when I attempt to do it on my personal desktop, powershell remains on halt infinitely.
I found a solution for this by doing:
python -m pip install somepackage
The above is fine, and I can live with typing an extra 8 characters whenever I want to install a new package but I'm curious to understand what is happening.
Thanks :)
PS: I have added C:/Python27/Scripts to Path in the system variables already and it didn't solve the issue.
pip.exe is located in C:\Python27\Scripts. You need to add that folder to your PATH environment variable. If you added it to the system environment you need to start a new PowerShell console to make the change become effective.
If you want the path to become available in the current console you need to add it to the PATH variable in the current console as well (the console gets a copy of the environment when it starts, it doesn't pick up changes to the system or user environment at runtime):
$env:Path += ';C:\Python27\Scripts'
pip install somepackage
However, C:\Python27\Scripts not being included in the PATH should only result in an error message that PowerShell doesn't recognize the command. It shouldn't freeze the console. Perhaps there's some other cmdlet/command/function/alias named pip that gets executed instead. Try running Get-Command pip to verify that.