Problem with accessing virtual environment Python from R Markdown - python

Note: I'm on Windows using Git Bash.
So, I am trying to setup a dev environment for the work for my class. It is going to involve a combination of coding in R and Python.
I created virtual environments using pipenv and virtualenv and ran into the same problem with both. So, first, let's create a virtual environment for the project in a sub-folder dev_env:
cd project_folder/dev_env
pipenv --python 3.7
pipenv --py
Output
C:\Users\Ra Me\.virtualenvs\dev_env-5TUtSZI9\Scripts\python.exe
Now I'm going into my file.rmd and trying the reticulate package.
#install.packages("reticulate")
library(reticulate)
Next, I tried 2 methods:
Sys.setenv(RETICULATE_PYTHON = "C:/Users/Ra Me/.virtualenvs/dev_env-5TUtSZI9/Scripts")
or
use_virtualenv("C:/Users/Ra Me/.virtualenvs/dev_env-5TUtSZI9/", required = TRUE)
x = 1
if x:
print('Hello!')
Both of them produced the error
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
However, when I change the path to the Python environment that's installed for all users on my machine, it works.
Sys.setenv(RETICULATE_PYTHON = "C:/Program Files/Python37/")
This method also works. However, here we are not even using the reticulate project.
knitr::opts_chunk$set(engine.path = list(
python = "C:/Program Files/Python37/python.exe",
r = "C:/Program Files/R/R-3.6.1/bin/R.exe"
))

Related

"No Python at '"C:\Users" on two computers

I'm trying tu use two computers to use vscode. I created a venv on the first one, the venv is in my drive. I was able to activate the venv from the drive on the second computer but when running a script I got the above error message.
By doing research on internet I found that I should change the PATH in the pyvenv.cfg file and that's what I have done but after reloading VScode I still have the same error.
My pyvenv.cfg file :
# home = C:\Users\Caracole\AppData\Local\Programs\Python\Python311 # Caracole
home = C:\Users\damien.daubricourt\AppData\Local\Programs\Python\Python311 # Setec
include-system-site-packages = false
version = 3.11.1
# executable = C:\Users\Caracole\AppData\Local\Programs\Python\Python311\python.exe # Caracole
executable = C:\Users\damien.daubricourt\AppData\Local\Programs\Python\Python311\python.exe # Setec
# command = C:\Users\Caracole\AppData\Local\Programs\Python\Python311\python.exe -m venv C:\Users\Caracole\My Drive\Programmation\Python\.venv\ZDT # Caracole
command = C:\Users\damien.daubricourt\AppData\Local\Programs\Python\Python311\python.exe -m venv G:\Mon Drive\Programmation\Python\.venv\ZDT # Setec
The error code :
No Python at
'"C:\Users\Caracole\AppData\Local\Programs\Python\Python311 #
Caracole\python.exe'
I changed the path in pyvenv.cfg file and was expecting to solve the error.

Python not recognized in CMD

I am working in Windows 11, Python 3.10, Mu 1.1.0. I followed the instructions on ATBS to create a batch file to run a script from the WIN + R launcher. However in CMD the error A(below) pops up. I tried following instructions from the documentation on python.org, and in Appendix B of ATBS to edit environment variables. I added the following to PATH env variables: C:\Users\19139\MyPythonScript; C:\Users\19139\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Python 3.10 (64-bit).lnk; and C:\Users\19139\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Python 3.10 (64-bit).lnk\Scripts. Is there some issue with the fact that the PATH includes start menu? All I am able to call from CMD is py --version. When I try and directly call a python script I get Error B. I disabled the "App Installer"(s) under App Aliases for Python.exe and Python3.exe but no change in error. I can call py --version, but I am beyond the scope of my ability to figure out what to do. I am unsure of next possible step to problem solve, suggestions welcome.
Error A:
`enter code here`Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'C:\Users\19139\AppData\Local\Programs\Python\Python310\python.exe'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python.exe'
sys.base_prefix = ''
sys.base_exec_prefix = ''
sys.platlibdir = 'lib'
sys.executable = 'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python.exe'
sys.prefix = ''
sys.exec_prefix = ''
sys.path = [
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\Lib\\',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\DLLs\\',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
Traceback (most recent call last):
File "C:\Users\19139\AppData\Local\Programs\Python\Python310\Lib\encodings\__init__.py", line 31, in <module>
ModuleNotFoundError: No module named 'codecs'
Error B
C:\Users\19139>python "C:\Users\19139\MyPythonScript\mClip.py"
Python was not found; run without arguments to install from the Microsoft Store, or
disable this shortcut from Settings > Manage App Execution Aliases.
First of all, unlike ubuntu/ linux Windows doesn't detect python on its own. That's why we need to specify the path manually, to make it aware of python.
Is there some issue with the fact that the PATH includes start menu?
Yes, there is issue there. PATH for python doesn't include Start Menu generally.
You are looking at the wrong place or have installed python package at the wrong location. Find the correct python package location.
Python PATH looks something like this:
C:\Users\user_name\python_installerpkg_name
For example, if you have installed python using a python installer pkg like anaconda/ mini-conda, you can use its command prompt to find the location via where python. Then use the location to set the PATH in environment variables. the PATH would look like: C:\Users\user_name\anaconda
Error B
Until you configure the correct path in the environment variables you won't be able to use the command python filename.py from the command line.

Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Mac

I've set up a virtual environment on MacOS BigSur 11.6 using python 3.9.7 version.
Environment name is "ambiente" and this is how the terminal looks like :
(ambiente) mac.fede#Ahoga PageDownloader %
I'm in PageDownloader directory.
Inside this folder there is my python project.
I can run the program using : python3 PageDownloader.py and all works fine.
I tried to install and run it using PyInstaller and the UNIX executable works fine.
Then using Platypus I bottled it to an application, when the icon is double clicked a shell script runs and launches the UNIX executable file obtained before.
When I double click the application this is the output :
​initializing script //this launches PageDownloader Executable file
Python path configuration:
PYTHONHOME = '/Users/mac.fede/Documents/GitHub/PageDownloader/PageDownloader.app/Contents/Resources'
PYTHONPATH = (not set)
program name = '/Users/mac.fede/Documents/GitHub/PageDownloader/PageDownloader.app/Contents/Resources/PageDownloader'
isolated = 0
environment = 0
user site = 0
import site = 0
sys._base_executable = '/Users/mac.fede/Documents/GitHub/PageDownloader/PageDownloader.app/Contents/Resources/PageDownloader'
sys.base_prefix = ''
sys.base_exec_prefix = ''
sys.platlibdir = 'lib'
sys.executable = '/Users/mac.fede/Documents/GitHub/PageDownloader/PageDownloader.app/Contents/Resources/PageDownloader'
sys.prefix = ''
sys.exec_prefix = ''
sys.path = [
'/Users/mac.fede/Documents/GitHub/PageDownloader/PageDownloader.app/Contents/Resources/base_library.zip',
'/Users/mac.fede/Documents/GitHub/PageDownloader/PageDownloader.app/Contents/Resources/lib-dynload',
'/Users/mac.fede/Documents/GitHub/PageDownloader/PageDownloader.app/Contents/Resources',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x000000010c0cfe00 (most recent call first):
<no Python frame>
Other posts on StackOverflow like this : Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding, when trying to start uwsgi
Suggests to remove the PYTHONHOME environment variable but I don't know how to do it. I can see that it's set and shouldn't. I did some researches on the internet but I didn't understand how to do this on Mac. I already tried :
unset PYTHONPATH
unset PYTHONHOME
Before launching the executable file but same error.
Now, I don't know if this is the right way to create an exportable application from python program on MacOS, I'm trying to figure it out so all this solution is "home made". If you have any suggestion I would be very happy to hear it.
With "exportable" I mean an application that I can share with my friends or other people and runnable with a double click.

How do I change pipenv/virtualenv location to work with Portable VS Code and project on thumb drive?

On Windows 10, I'm running VS Code 1.48.2 and Python 3, both installed on a thumb drive. I added an F:\Programs\VS Code\data folder with user-data andextensions. My project directory is F:\MyProject and it has a Pipfile. My project directory has a .env file and .venv\ so that pipenv will install to the local .venv\. What else do I need to get this environment working? Thanks!
.env file:
PYTHONPATH="F:\\Python\\Python38\\python.exe"
PYTHONHOME="F:\\MyProject\\.venv\\Scripts"
PIPENV_VENV_IN_PROJECT=True
F:\MyProject\.vscode\settings.json file:
{
"python.pythonPath": "f:\\MyProject\\.venv\\Scripts\\python.exe"
}
Although, when I used Preferences: Open Settings, it opened my F:\Programs\VS Code\data\user-data\settings.json file, though the paths look right (to me):
{
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"python.languageServer": "Microsoft",
"python.pythonPath": "${workspacefolder}/.venv/Scripts/python.exe",
"python.venvPath": "${workspacefolder}/.venv",
"python.venvFolders": [
".venv",
"${workspacefolder}/.venv"
]
}
Used the following steps to setup:
ps > cd F:\MyProject
ps > F:\Programs\VS Code\code.exe .
[In vscode integrated terminal]
PS F:\MyProject> py -m venv --system-site-packages .venv
PS F:\MyProject> pipenv shell
Loading .env environment variables…
Warning: Your Pipfile requires python_version 3.8, but you are using unknown (F:\M\.venv\S\python.exe).
$ pipenv --rm and rebuilding the virtual environment may resolve the issue.
$ pipenv check will surely fail.
Launching subshell in virtual environment…
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS F:\MyProject> ls .\.venv\Scripts\python.exe
Directory: F:\MyProject\.venv\Scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/6/20 3:04 PM 532040 python.exe
PS F:\MyProject> python where
Python path configuration:
PYTHONHOME = 'F:\MyProject\.venv\Scripts'
PYTHONPATH = 'F:\Python\Python38\python.exe'
program name = 'F:\Python\Python38\python.exe'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'F:\\Python\\Python38\\python.exe'
sys.base_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.base_exec_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.executable = 'F:\\MyProject\\.venv\\Scripts\\python.exe'
sys.prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.exec_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.path = [
'F:\\Python\\Python38\\python.exe',
'F:\\Python\\Python38\\python38.zip',
'F:\\MyProject\\.venv\\Scripts\\DLLs',
'F:\\MyProject\\.venv\\Scripts\\lib',
'F:\\Python\\Python38',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x000020d4 (most recent call first):
<no Python frame>
PS F:\MyProject>
According to your description, you could refer to the following steps to use the virtual environment:
Open this project in VSCode.
According to the information you provided, there is already a virtual environment ".venv" in your project, so there will be a ".venv" folder after opening. like this:
Click "select Python Interpreter" in the lower left corner to select the ".venv" virtual environment:
VSCode has selected the virtual environment, and then we open a new terminal console through the shortcut key "Ctrl+Shift+`", VSCode will automatically enter the current virtual environment:
Use pip to install the module "PySimpleGui": (pip install PySimpleGui)
When we run the python script, VSCode showed that the module PySimpleGui could not be found. We found "Lib\site-packages" in the ".venv" folder of the project and changed "PySimpleGUI" to "PySimpleGui":
The python script runs successfully in the virtual environment:

Installation of tensorflow terminated on RStudio Cloud

Similarly to posts here and here, I am having more trouble when I try to install TensorFlow in a new RStudio Cloud project. I know I need to set up both Miniconda and a virtual environment locally in /cloud/project/ so the Python dependencies stay with copies of the cloud project. Previous versions of the following setup script worked.
install.packages(c("keras", "rstudioapi", "tensorflow"))
lines <- c(
paste0("RETICULATE_CONDA=", file.path(getwd(), "miniconda", "bin", "conda")),
paste0("RETICULATE_PYTHON=", file.path(getwd(), "miniconda", "bin", "python")),
paste0("WORKON_HOME=", file.path(getwd(), "virtualenvs"))
)
writeLines(lines, ".Renviron")
rstudioapi::restartSession()
reticulate::install_miniconda("miniconda")
reticulate::virtualenv_create(
envname = "r-tensorflow",
python = Sys.getenv("RETICULATE_PYTHON")
)
keras::install_keras(
method = "virtualenv",
conda = Sys.getenv("RETICULATE_CONDA"),
envname = "r-tensorflow"
)
But I get an error on Cloud when I try to install Python's TensorFlow and Keras:
keras::install_keras(
+ method = "virtualenv",
+ conda = Sys.getenv("RETICULATE_CONDA"),
+ envname = "r-tensorflow"
+ )
Using virtual environment 'r-tensorflow' ...
Collecting tensorflow==2.2.0
Downloading tensorflow-2.2.0-cp38-cp38-manylinux2010_x86_64.whl (516.3 MB)
Killed
Error: Error installing package(s): 'tensorflow==2.2.0', 'keras', 'tensorflow-hub', 'h5py', 'pyyaml==3.12', 'requests', 'Pillow', 'scipy'
The same script on my local Ubuntu machine appears to succeed, but it ignores my local virtual environment even though I set WORKON_HOME.
> tensorflow::tf_config()
Installation of TensorFlow not found.
Python environments searched for 'tensorflow' package:
/home/landau/projects/targets-tutorial/miniconda/bin/python3.8
You can install TensorFlow using the install_tensorflow() function.
Example project that uses this general approach: https://github.com/wlandau/targets-keras.

Categories