gpoore/pythontex

Is there a way to use existing virual environment (created by Pycharm)

bedlamzd opened this issue · 5 comments

This is similar to #138 but I cannot find a solution to my case. As in mentioned issue I can use python.exe in the environment, but cannot import libraries.

Now I simply use pythontex --interpreter $path/to/interpreter command, but I guess that is wrong way to do it.

actually, I've done same thing as in answer to #138. But it is not working as expected. My base python is 3.8 and environment should be 3.7.
When I output sys.version to latex it prints 3.8

Turned out my virtual environment somehow used 3.8 python interpreter, instead of 3.7
I created new environment (also removed python from system path to be sure), installed necessary modules (numpy and others) but when compiling it still gives an error ModuleNotFoundError: No module named 'numpy' when importing my code

Latex file
\documentclass[a4paper, 12pt]{article}

\usepackage{cmap}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\usepackage[gobble=auto]{pythontex}
\begin{pycode}
  import sys
  import utilities
\end{pycode}

\begin{document}
  \section*{py\LaTeX\ }
  \begin{pycode}
  	print(sys.version)
  \end{pycode}
\end{document}
Error from pythontex
This is PythonTeX 0.17

----  Messages for py:default:default  ----
Traceback (most recent call last):
* PythonTeX stderr - error on line 10:
  File "<outputdir>\py_default_default.py", line 51, in <module>
    import utilities
  File "D:\Repositories\vkr\tex\utilities.py", line 1, in <module>
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

--------------------------------------------------
PythonTeX:  main - 1 error(s), 0 warning(s)

Also, using pyconsole environment I can import packages, but not in pycode environment

When you run pythontex, pythontex itself will run under the version of Python for which pythontex is installed. That version of Python will apply to all pyconsole environments, because these are executed using the code module within the main pythontex process. However, all other Python code (pycode, etc.) is executed via subprocess, which defaults to running python but can be changed with --interpreter.

So depending on how things are configured, pyconsole and pycode can use different Python versions. An issue for this exists in #24; fixing requires some significant redesign. If you can set --interpreter python:<path_to_python_you_want> to the correct value, you should be able to get everything using the same Python version.

Thank you for your help!
I solved my problem (I've been using --interpreter wrong). But I've noticed weird behavior.
So my commands now are
"D:\Repositories\vkr\venv37\Scripts\activate.bat" && pythontex --interpreter python:D:\\Repositories\\vkr\\venv37\\Scripts\\python.exe %.tex
to activate environment and specify proper interpreter.
So, as I undestand, pyconsole now shold run under system interpreter and pycode under specified environment.
But if I do this

Latex file
\documentclass[a4paper, 12pt]{article}

\usepackage{cmap}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\usepackage[gobble=auto]{pythontex}

\begin{document}
    \section*{py\LaTeX\ }
    In pyconsole
    \begin{pyconsole}
    	import os
    	import sys
    	os.getcwd()
    	sys.version
    	sys.executable
    	import numpy as np
    	np.cos(np.pi)
    \end{pyconsole}
    In pycode\\
    \begin{pycode}
    	import sys
    	import os
    	print(os.getcwd().replace('\\', '/'), r'\\')
    	print(sys.version, r'\\')
    	print(sys.executable.replace('\\', '/'), r'\\')
    	import numpy as np
    	print(np.cos(np.pi))
    \end{pycode}
\end{document}

they both work and give following results

resulted pdf

image

and in the same time, interpreter specified by pyconsole actually does not have numpy installed

cmd numpy check
C:\Users\bedla>py -3.7
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'C:\\Users\\bedla\\AppData\\Local\\Programs\\Python\\Python37\\python.exe'
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>                                           

and all this while my base python interpreter actually 3.8 (3.7 not in the PATH)

cmd python version check
C:\Users\bedla>python -V
Python 3.8.2                                            

So, to conclude
Activation of the environment affects pyconsole as follows:

  1. changes interpreter to a system interpreter with the same version as in environment
  2. adds modules of the environment
  3. does not specify interpreter of the environment.

Activation of the environment affects pycode as follows:

  1. changes interpreter to a system interpreter with the same version as in environment
  2. does not add modules of the environment
  3. does not specify interpreter of the environment. (but it could be specified manually and this will add modules of the environment)