How to use direnv with python venv? - python-3.x

I have this line in my .envrc
source python3-venv/bin/activate
I basically created a python3 venv inside the folder where the .envrc is located.
However even through the .evnrc is executed (e.g. all now env variables are set correctly when I enter this directoy), the venv is not activated.
When I do which python3, it shows /usr/local/bin/python3 instead of <path to my current folder>/python-venv/bin/python3
If I run the source line manually, the bash prompt will switch to (python3-venv) my-workstation:working-folder antkong$ as expected.
How can I get the python-venv correctly activated?
I am running it in MacOS Mojave.

I removed the venv then rebuild the venv
python3 -m venv python3-venv
It seems to fix the issue.

Related

venv activate has no effect

I want to use a virtualenv for my python3 project:
did virtualenv -p python3 venv to install it.
when doing . venv/bin/activateit doesn't activate the venv (I'm able to use all my lib's in standard path) also no (venv) before prompt. There is also no error, it just gives me a new line.
source venv/bin/activate gives me the same result.
Interestingly my VSCode detects when virtualenv -p python3 venv is run and asks if I want to change the environment.

Create Virtual Environment using Python Documentation

I am very new at command line usage. I am using python 3.7.2, Bash and VSCode Integrated Terminal. I am trying to create a virtual environment using venv and following python documentation:
https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments
The command to use is this one:
$ python3 -m venv test-env
and I get:
bash: python3: command not found
Later I have found a similar answer in an stackoverflow post:
How to create and activate virtual environment in windows 10 using bash command
And I use the command:
py -m virtualenv test-env
and I get this:
No module named virtualenv
I am very new using the command line so i don´t really know what is going on and how to work it around.
Hi i can see that you are using two different tools to create your environment.
Those are "venv" and "virtualenv".
Venv is a library that already comes with your python installation.
Virtualenv is an external one.
I had the same problem before and the solution is very simple.
I recommend you to stick with venv because it works pretty ok and you don´t need to do extra job installing external libraries.
So for solving your problem the Bash Shell is telling you that the command Python3 has not been found.
So try instead just:
python -m venv test-env
Sometimes Python documentation is not accurate enough and I know when you start using commands, accuracy in the sintax is extremely important.
Try this steps,it'll helped you:
First, make a directory :
mkdir testing
Then, moved to this directory named testing :
cd testing
When you type following command in this directory:
python3 -m venv env (OR, python -m venv env)
You got error like :
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari
Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)
Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.
After this, we can install anything that will be isolated from the rest of the system....

How to fix Error: [Errno 2] No such file or directory: 'C:\\Program Files\\Python37\\lib\\venv\\scripts\\nt\\python_d.exe'

I'm using the latest version of Python on Windows and I'm trying to use venv with the following code:
C:\envs> py -m venv test
Even if it actually creates the venv, it returns this error and I don't understand why.
Moreover I can't activate the venv, the code
C:\envs> C:\envs\test\Scripts\activate.bat
returns
"C:\envs\test\Scripts\activate.bat" is not recognized as an internal or external command, operable program or batch file.
I am using python version 3.7.3 it gave me error states that "No such file or directory: 'C:\python37\lib\venv\scripts\nt\python_d.exe'" at the time of creation of project in pycharm.
I copied following files from python location(C:\Program Files\Python37) to the (C:\Program Files\Python37\Lib\venv\scripts\nt) and it worked for me
1) python_d.exe
2) python_d.pdb
3) pythonw_d.exe
4) pythonw_d.pdb
As Villani mentioned in his own comment, it's a debug binaries problem.
It will be fixed in the upcoming 3.7.4 release. (Planned for 24/06)
Either downgrade to 3.7.2 or install without debug binaries.
In Anaconda prompt type these commands:
conda remove anaconda
conda update python
conda list --show-channel-urls | findstr python
python -m venv venv
for me what fixed the issue was copying the python_d.exe and python_d.pdb from C:\Program Files\Python37 to C:\Program Files\Python37\Lib\venv\scripts\nt
hope this solves your problem ;)
The problem is , your virtual environment could not find debugger resources. The venv module's resources dont have those resources (probably a bug). The easy solution is.
Fix the venv module resources first: Copy debugger symbols to venv resources. In your python installation directory copy python_d.pdb, python_d.exe, pythonw_d.pdb, pythonw_d.exe and past inside (installation directory) Lib>venv>scripts>nt. In this nt folder should also have (already present) python.exe , python.pdb , pythonw.exe, pythonw.pdb
Now, try to create virtual environment using
python -m venv py37dev
These steps worked for me
conda update --force conda
conda update python
conda list --show-channel-urls | findstr python
python -m venv venv
I update python and bug fixed
conda update --force conda
conda remove anaconda
conda update python
python -m venv venv
This will resolve it but your main Python interpreter will run from the conda environment & it will prompt the error below but it will work just fine.
"This Python interpreter is in a conda environment, but the environment has
not been activated."

How to change python env

I have a issue where if a python 3 script's shebang line is /usr/bin/env python3 then script will be interpreted with /usr/bin/local/python3
type -P python3
/usr/local/bin/python3
So PATH python3 is /usr/local/bin/python3 but this creates problems where some packages are not available for /usr/local/bin/python3 and I would instead like to use /usr/bin/python3 as default env python3.
Using a alias to set python3 to /usr/bin/python3 can be done but this does not solve the issue. Using Virtualenv if fine but one can not create a virtualenv for every single litte script out there.
type -a shows that there are two python3 defined on my system so there must be a way to change the prefered one:
type -a python3
python3 is aliased to `/usr/bin/python3'
python3 is /usr/local/bin/python3
python3 is /usr/bin/python3
How can I change so that env python 3 is /usr/bin/python3?
Best regards
I solved the issue by editing /etc/environment. The change that I did was to specify to read /usr/bin before /usr/local/bin as was configured on my computer. After that I restarted my PC and now /usr/bin/python3 is the default env python.

How to get virtualenv to run Python 3 instead of Python 2.7?

On Mac, if I simply open a new terminal window and run:
python --version
I get:
3.6
but if I do this:
virtualenv venv && source venv/bin/activate
and then, in that environment, I run :
python --version
I get:
2.7
I need virtualenv to run 3.6. How do I do that?
This :
/usr/bin/python
is 2.7 but this:
/usr/local/bin/python
is 3.6. The path for my normal user has /usr/local/bin come up before /usr/bin/. Is virtualenv running as someone else? How do I control its path?
I ran this:
virtualenv -p /usr/local//Cellar/python/3.6.5/bin/python3 venv
but then I do this:
virtualenv venv && source venv/bin/activate
and I'm running in an environment with 2.7.
On Python 3 you don't need virtualenv script anymore, you should just use the venv module included with standard lib:
python3 -m venv myvenv
But if you really want to keep using the old virtualenv script, you can - specify the interpreter explicitly with the -p option:
virtualenv -p /path/to/python3 myvenv
The easiest way is to change python globally to Python3 as I think you're using it more often than Python 2.7 (or hopefully always). To achieve this, add the following line of code at the end of your .bash_profile:
alias python='python3'
virtuanenv is using /usr/bin/python, hence it should work now.
If you don't want to change it globally, than you should use the following command to create your Python3.6 virtual environment:
python3 -m venv venv
or the explicit Python version if you have multiple Python3 versions installed:
python3.6 -m venv venv
On more suggestion at the end: I recommend you to read something about pipenv as it's the new recommended way to handle virtual environments and your whole package management at once. It's super easy and fixes a lot of common issues. Here's a nice article from realpython.com on that topic.
Hope I could help you. Have a nice day.

Resources