Portable VS Code + portable Python - python

I am new to learning Python and VS Code. Tried to make portable python version from embeddable version. Deleted the python._pth file, and tried to install python get-pip.py. But nothing happens.
I did it by analogy by downloading the full installation distribution. Installed, copied the folder to the desired drive, deleted the installed one. Python starts up. But at least with the help of VS Code, at least by itself, it installs the libraries to the user's folder on the system drive along the path C:\Users\username\AppData\Roaming\Python\Python39
Naturally, when I run Python on another computer, there are no libraries installed
In addition, if I do not set two directories in Windows PATH for Python itself and scripts, then many libraries also do not work. I need to go into the scripts folder and install the same pip from there.
Maybe you need to somehow prescribe where to put it during installation? But how? Something like #ScriptDir
I also write the following lines in settings.json:
"python.defaultInterpreterPath": "f:\\portable\\winpython3-9\\python.exe",
"python.defaultInterpreterPath": "d:\\portable\\winpython3-9\\python.exe",
Then Python works. Though libraries all the same puts in the directory of the user. But VS Code writes a warning that there are duplicate lines in the settings.json file. How can I replace these lines so that I don't have to repeat them for every drive?
And how to make the libraries installed in the directory with Python?
And what other ways are there to solve the problem of working together portable VS Code + portable Python

Solved this problem. I found a portable Python here at this link.
http://winpython.github.io/
Just downloaded the installation (ziped)file. Then in VS Code I specified the path to the interpreter and everything works.
Nothing needs to be written in settings.json

Related

Python inside a virtualenv is broken (Django)

TL;DR - Microsoft Store Apps are broken (0 bytes), hence the Python interpreter is unable to create process and run "Python" inside virtualenv, I failed to follow numerous explanations of how to change virtualenv path for Python.
Recently, without any changes to my computer/environment, an issue started occurring when executing (also tried python3, which brings the same):
python manage.py runserver
This brought back the following issue:
Unable to create process using
'"C:\Users\MyUser\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe"'
As I dug deeper, I realized that all of the apps installed under this folder are 0 bytes, hence they are completed broken, all of a sudden.
Therefore I figured it's not an issue with Django, rather the python itself.
I tried changing the virtualenv path for Python.exe, instead of using WindowsApps version, I installed the original Python from the original website.
I failed to do so, multiple times.
I tried: "https://stackoverflow.com/questions/4757178/how-do-you-set-your-pythonpath-in-an-already-created-virtualenv/47184788#47184788" - Adding the path inside "activate" and "activate.bat", as
set PYTHONPATH="C:\Users\MyUser\AppData\Local\Programs\Python\Python310\python.exe"
And the issue persists.
I tried every solution/article I found in regards to the issue I have. Many of them claim a simple result, whereas the rest claim a complete refactor is required.
Even downloading from Microsoft store is broken, it always fails.
Since I'm unable to remove the broken apps, and I wasn't able to find a way to change the virtualenv Python interpreter, I am here, asking for your assistance.
Thanks in advance,
Since you have installed python from python.org, ensure that it has been properly added to the PATH and you're not inadvertently using the windows store version.
Open command prompt
Execute where python to check which python your system is using.
If you're only seeing the WindowsApps version of python listed in the output of where command, then python installer apparently didn't add the location to PATH, so you'll need to manually add it yourself.
Also, if you're not restricted to use virtualenv, you can give conda a try.
As I began rebuilding, I figured it out.
When building a virtualenv, the python I used was directing to:
"C:\Users\MyUser\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe"'
Python interpreter is saved as the "PATH" to execute python.
Since my Python interpreter from Microsoft Store went broke, I was unable to execute python inside the virtual environment.
Venv configures a file called:
pyvenv.cfg
This is how the virtual environment knows what Python interpreter to use.
Inside it, the first line states:
home = C:\Users\MyUser\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\
Therefore, we just need to re-configure it to the new path of Python interpreter:
home = C:\Users\MyUser\AppData\Local\Programs\Python\Python310
And it worked for me.
Thanks for everyone's assistant, glad it's done.

Python standalone .py or external import of module? Is this really as hard as it seems?

Situation: I've installed a bunch of packages with pip. I've now written code using these packages.
I have a myscript.py
My friend is on windows.
has python installed.
He has no packages.
He cannot get any packages.
He has pip
He will never be able to use the internet to get more packages everything must be hand delivered.
In fact about 10 minutes after he runs whatever I give him, he formats his machine and it's gone.
How do I take myscript.py and give it to him on a USB stick so that he can copy the file myscript.py onto his computer and run it?
I thought Pipenv would do it but it looks like it just creates a LIST of packages to download from the internet. (a very well defined list... but a list not the actual files needed to run something. Do I understand it correctly?
Right now I'm giving him .exe made with py2exe. This isn't very elegant considering he has python already.
tl;dr how do I give a python script .py to an end user that doesn't have the internet?
You can package everything up in a virtual environment and give him the complete environment necessary to run the script. You can read about this at https://developer.mozilla.org/en-US/docs/Python/Virtualenv and an IDE like PyCharm will help you create such an environment easily.
It's actually a good thing to learn about and do anyway.

Unable to run python in CMD

I have recently started learning python using code academy and today I downloaded everything that I thought I would use. I downloaded Python and Atom. I have two separate drives on my computer. An SSD with not much storage and a hard drive with a lot of storage. My Windows is installed on the SSD, but I wanted to download python and atom on the hard drive, so I did so. When I installed Python I made sure to check add to PATH and the environmental variable thing. Now when I go to the command prompt, it shows "C:\Users\Gustavo>" but my python is installed on a different drive. Is there a way I could make this work? Thanks a lot.
There are two ways to install python:
Download directly from the website
Use a package manager
Case 1: Download directly from the website
Go to the python's website to download the version you would like to use.
Install the downloaded file (During the installation you can customize the installation directory)
Make sure to enable "Add python.exe to Path"
After installation has been completed, open your command prompt and type where python. Your python directory should be printed.
If all is good, then typing python should launch python in your command prompt. You can also run python by cd in the directory where your python is located and launch the .exe
You have mentioned about changing path and environmental variables, and that's probably for the purpose of keeping multiple versions of python. If that's the case, there's actually a quick fix for this:
Go to the folder where you installed Python.
Copy the python.exe file, and rename that copy in the same directory as python3.exe (If you installed version 2, then rename as python2.exe).
Now in command prompt type python2 or python3 and you should be able to launch either versions respectively.
Note: If you face issues regarding paths, then you should detail the error messages.
Case 2: Use a package manager
Choose a package manger: chocolatey, scoop, and others.
Check out these links for changing package manager's installation directory, installation method varies by the managers, so you should consult the developers should you experience problems:
chocolatey
scoop
Package managers will manage the versions for you, if you choose to install multiple versions. You should refer to the package manager's website for detailed information. However, you can quickly check the installed version by typing python --version. The python version number should be printed back to you, same applies to python3.
Double check your installation directory by which python
Type python or python3 to run your python of choice.
Lastly, you have mentioned atom. Atom is just a text editor: you can write python codes with it.
When you are done editing, you can open the command prompt and navigate to where your code resides, and type
python filename.py
This will run your code directly from the command prompt. There are many atom plugins available to make this process seamlessly integrated within atom. iPython and Jupyter plugins are first things that comes to my mind, you should specifically check out Hydrogen.

How to make python portable?

I want to make a portable app that would have some code and python executable that would run on any Windows even if python is not installed.
I would like it to be python 3.6 and so it has only pip and setup tools installed.
EDIT: concerning duplicate
not quite. I don't want to compile the code. I wanted to give them .py files but realize that Windows won't have python installed on default. I want something that can be carry on a flash drive but will run my code from source not binary.
Please correct me, if I understood it wrong. I think there are at least two ways to do it.
suppose you have one portable_run.py script you want to run everywhere on a flashdisk.
Make a exe file with pyinstaller for example. you can get a exe file like portable_run.exe. On target windows system what you need to do is to run the exe direcltly protable_run.exe
Use a portable python distribution like winpython or python-xy. you just need to copy this portable distribution on the flash disk together with your portable_run.py. To run it on target system flashdisk/path-of-winpython/python portable_run.py
Hopefully it could give you some idea.
I also encountered the same problem and managed to create a portable python with Python's official Windows embeddable package.
I wrote the steps into a ps1 script so I can easily repeat the process without going through the pain.
The steps:
Download the portablepy.ps1 from the repo :
https://github.com/Dreamsavior/portable-python-maker
Create a blank folder, put the portablepy.ps1 to that folder.
Execute the portablepy.ps1
The script will create a portable python 3.9.10 with pip in the current folder by default.
To install custom version of Python run the script with -source and -destination parameter
.\portablepy.ps1 -source "https://www.python.org/ftp/python/3.9.10/python-3.9.10-embed-amd64.zip" -destination "C:\SomeDir\PortablePython\"
Where the -source is the url of the Python's Windows embeddable package from this page: https://www.python.org/downloads/windows/
And -destination is the path of your folder (ended with backslash).

Run Python script in a custom Python folder

I have a problem running a python script on a system that doesn't have Python installed. I know what you're thinking...but hear me out.
Some applications like C4D and Maya come with their own versions of Python. Unfortunately, they often compile them incorrectly, so modules that should import on their version of Python (e.g. 2.6 for C4D) don't work at all. I don't know why they do this, I've asked, but it appears to be due to a lack of knowledge on their part.
To use a module that won't import, you have to use a separate python installation. But I don't want to force users to install python, so I include my own python folder (2.7.6) with the modules I want to use inside and launch my script inside my custom (non-installed) python folder like this:
cmd = [my_python_path, "-E", my_script.py]
p = subprocess.Popen(cmd, shell=False, bufsize=...etc.
This works fine as long as Python 2.7.6 is actually installed on the system, but if it isn't installed, then it doesn't work. My system above isn't targeting, or using the installed python. In fact, I've moved the installed Python folder, and renamed it to make sure it isn't being used somehow, and my script works fine. So I know it is executing with my python folder.
Question 1: Why won't the python.exe run inside my custom folder unless there is an installed version of python? Is this because of some path variable?
Question 2: How can I make my python.exe work on systems, both Mac and Win, without Python officially installed?
Thanks
Just running Python.exe from custom folder doesn't tell it the specific location of many files and folders. These stuff are fixed in Windows as System Variables.
If you are copying the whole python folder, why not install python instead? It'll take same space too? Or if you really wan't to create executable use:-
Py2Exe for windows!
py2app from Mac!
Pyinstaller for both
I prefer py2exe.

Categories