python virtual environment activated but python command not available - python

I have created a virtual environment on Ubuntu with
python3 -m venv myenv
Python version is 3.8.10.
Then I have activated the environment with
source myenv/bin/activate
(myenv) c#pc:~/myvenvs$
However, when I run which python I get nothing, and when I run which python3, I get my system Python at /usr/bin/python3.
Something is off here, and I do not know how to fix it.

Related

The virtual env doesn't works as expected (env created from python installed from source)

I installed python 3.9.5 from source.
mkdir /projects/python
cd /tmp/Python3.9.5
./configure --prefix=/projects/python && make
make install
From it I created a virtual environment.
virtualenv --python=/projects/python <path/to/new/virtualenv/>
Virtual environment can be activated, running python from environment with builtins works, but packages are installed on the OS python.
I believe the virtual environment is activated because I run ./activate in environment folder and the prompt is changed (name_virtual_env)normal_prompt
which python and pip version returns:
usr/bin/python
usr/bin/pip
not the versions from the virtual environment.
virtualenv you used may be for the previous python installed.
try :
/projects/python/bin/python3 -m pip install virtualenv
/projects/python/bin/virtualenv <path/to/new/virtualenv/>

How to create a Python virtual environment independent of OS and Python version

I am trying to create a virtual environment to run a script which requires Python 3.6. I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed. I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.
Virtual environments are not portable, they depend on the Python installation you have.
You can't share/distribute virtual environment with others, because you can't control which version of Python others are using.
If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller. It is far from perfect and little bit hacky. Also it generates a package which is specific to operating system.
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
There is also a detailed step-by-step guide on how to use PyInstaller.
https://realpython.com/pyinstaller-python/
This is step-by-step how I use Python virtual environment and share it with co-workers.
To check python and virtualenv presence, run following commands:
which python3
python3 -m pip list | grep env
which virtualenv
Install a python virtual environment builder:
python3 -m pip install virtualenv
Create a virtual environment named venv inside the project's directory: virtualenv venv
To activate this environment use this command inside project's directory: source venv/bin/activate
Install python modules dependencies listed in a requirements.txt:
python3 -m pip install -r requirements.txt
You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory. When you need to deactivate the virtual environment do it using deactivate command.
To deactivate environment simply run: deactivate

How to create virtualenv with specific downloaded python version

I know there are some similar questions,but it is really hard for me to finish it.
I'm trying to create a virtualenv with python 3.7.7 in windows.
I have a downloaded python 3.7.7
C:\Users\willi\AppData\Local\Programs\Python\python-3.7.8-embed-amd64
Since I can built a virtualenv using:
python3 -m venv myenv
So I tried to modify it ,so that it can match specific python version:
python3 -m C:\Users\willi\AppData\Local\Programs\Python\python-3.7.8-embed-amd64\python.exe myenv
But it failed:
ModuleNotFoundError: No module named 'C:\\Users\\willi\\AppData\\Local\\Programs\\Python\\python-3')
Any friends can teach me how to build a virtualenv with python 3.7.7?
I think you haven't installed virtual environment in your local python
pip install virtualenv
and follow your steps. Stil, find the error. Try doing below methods, I think you will get your answer-
in command prompt
pip install virtualenv
go to the location, where you want to create your environment
cd location
virtualenv project_env_name
Now, you will find a python env in the desired location, Then go to scripts
cd project_env_name/scripts
activate
You will enter the environment you created. while leaving the environment, do
deactivate
To leave the environment.
This method works if you want to create the same python version environment as python version in your machine.
If you want to create an environment of the different version, you need to install the python of that version.

Cannot activate correct Python in remote computer

I have moved a virtual environment onto a remote computer. I am trying to activate python 3.8 but not having much luck.
The virtual environment I'm using was created using venv. But I created it on my Mac and then moved it onto the target Linux computer. The following demonstrates that the Linux computer has python 3.8
kylefoley#kfoley76:~/byu_corpus_small/venv_byu/bin$ ls
activate activate_this.py pip pip3.8 python3
activate.csh easy_install pip2 python python-config
activate.fish easy_install-2.7 pip2.7 python2 wheel
Activate.ps1 easy_install-3.8 pip3 python2.7
I activated the virtual environment with the following commands:
kylefoley#kfoley76:~/byu_corpus_small$ source venv_byu/bin/activate
However, the following command shows that python 2.7 was activated
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python --version
Python 2.7.13
Further, when I ran one of my programs I got a syntax error that only python 2.7 would throw:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python3 fix_mistakes.py
File "fix_mistakes.py", line 113
p = print
p (f"{round(c - b,0)} seconds")
SyntaxError: invalid syntax
Even when I run the command python3, it activated python 3.5 as demonstrated by the following:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
I even think that the computer is not even using my working environment but the default python interpreter due to the following:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ which python
/usr/bin/python
I would think that the intepreter would be in my virtual environment which would be:
/venv_byu/bin/python3
#####UPDATE
I was able to install venv on the linux but I'm still activating python 3.5.3
kylefoley#kfoley76:~/byu_corpus_small$ source venv_byu/bin/activate
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small$ which python
/home/kylefoley/byu_corpus_small/venv_byu/bin/python
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small$ python --version
Python 3.5.3
Apparently the environment I downloaded did not have 3.8
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/venv_byu/bin$ ls
activate activate.fish easy_install-3.5 pip3 python
activate.csh easy_install pip pip3.5 python3
Now, I just need to figure out how to get 3.8
Virtual environments are not portable. You should create a new virtual environment on the destination computer and populate it with the packages you need. Listing them with pip freeze on your local computer or manually enumerating them in requirements.txt are two common approaches.
The standard virtual environment shipped with Python actually hard-codes the path of the virtual environment, so you can't even rename the directory locally, much less copy it to a different directory structure on a different computer.
The activate command needs to be run with source for various reasons, but that also means that it is not very robust against failures. For example, it can throw an error or fail silently, but still update your prompt so that it appears as if the virtual environment was successfully activated.
To create a new virtual environment on a computer where you have Python 3.8 installed as /usr/local/bin/python3.8, you can run
/usr/local/bin/python3.8 -m venv venv_byu
You then need to activate this environment, and pip install or otherwise populate it with the libraries you need.
pip freeze will list the exact versions of all installed packages, so it is more precise in getting exactly the same version of everything. If you manually list packages in requirements.txt, you don't have to specify a precise version of anything, and you can leave out packages which are pulled in as dependencies of the packages you actually specifically depend on.

How to use a Python virtual env in my Ubuntu bash?

I am trying to package my project with a virtual env so that it is easier to implement.
I am trying to do this in a Ubuntu bash.
I have succesfully created a Python venv using the Python virtualenv library.
I do manage to activate it using source venv_name/bin/activate.
I can indeed see (venv_name) at the beginning of my command line.
However, I do not manage to actually use this virtual environment.
I have for proof that when I type which python3 I get my root python3; and I have tried to update a package in the virtualenv but it has been updated in the root python.
What should I do to actually use my virtual env ? For now I am trying:
python3 myscript.py
And it is working but I suspect it's running with my root python3.
I think you have two versions of python (2 and 3). You create virtualenv with python 2. Recreate virtualenv with correct python version
You have to make next steps to use python3 virtual environment in Ubutnu:
1. Install virtual environment lib for python3 with command:
pip3 install virtualenv
2. Create your virtual environment:
python3 -m venv venv
3. Activate it:
source venv/bin/activate
Works correct for me in Ubutnu 16.04

Categories