I am trying to run a simple python virtual environment and for some reason, nothing happens when I run a command. I open a new folder, cd into it, and run: python3 -m venv tutorial-env followed by source tutorial-env/bin/activate. All that happens is a new line is returned, but I am missing the (tutorial-env) at the beginning, and I am not in the virtual environment. Does anyone have an idea of how to fix this?
Some helpful info:
I'm on a mac
python3 --version returns Python 3.9.5
which python returns usr/local/bin/python3
Related
python newbie here.
I am on a windows machine and used Windows Subsystem for Linux(WSL) to execute commands.
I created my venv(folder) by entering:
python3 -m venv venv
and it would give me a new venv folder as below:
and then activated the virtual environment using:
source venv/bin/activate
however, I am having an issue as I executed another command and it is looking for python3 which should be present as in this path: venv/bin/python3
I am curious how should python3 created in venv. Thanks!
i guess you are confused about the Python virtual environment
first, the virtual environment is designed to establish a command execution environment for each project。
then. the first your code python3 -m venv venv call the system python command and the python3 should in /usr/bin/python3 and linked with /usr/bin/python3.x
but, if you activate the virtual environment of Python, the virtualenv will and the {you_dir}/venv/bin to the system path and you will call python through /venv/bin/python
to execute you python code.
there is no need create an link of /ven/bin/python3. but if you want, you can do it!
Just recently upgraded to Mac OSX Catalina, and it seems to have broken all my Python venv's.
I have a venv that I previously created before upgrading to Catalina.
I can activate the venv without issue: source venv/bin/activate
When I type python or python3, it actually runs the system Python interpreters, instead of the expected Python interpreter in my venv.
With my venv activated...
>>> which python
/usr/bin/python
>>> which python3
/usr/local/bin/python3
I expect that to point to /Users/<username>/<path-to-venv>/venv/bin/python3.
If I try to run that Python interpreter directly:
>>> /Users/<username>/<path-to-venv>/venv/bin/python3
bash: /Users/<username>/<path-to-venv>/venv/bin/python3: No such file or directory
If I navigate to this venv directory in Finder, I can see that the python3 file exists, although it's actually an alias. If I double-click on it, I get an error message that pops up that reads: The operation can't be completed because the original item for "Python3" can't be found.
So I think the way these venv's work is that they are based on references to existing Python interpreter installations. Evidently running a venv invokes these references, and if the original installation is broken it fails. I know that Catalina jacked with the default Python settings and went so far as to change installation directories.
Has anyone else encountered this? Can anyone offer tips to fix this? This seems to affect all my venv virtual environments across my system.
You can try to create a new venv in the same folder or to update your existing venv. With all the following I assume you've created your virtual environments with the bundled venv module
For the creation it's the command you've probably already used
python3 -m venv <path_to_dir>
In order to update, the --upgrade parameter is what you're looking for.
You need to run it with the targeted Python version, for example in this case:
python3 -m venv --upgrade <path_to_dir>
for more details, see my answer here
When I try to create a new "virtualenv prod" it runs the below output in the cmd, and it runs for minutes with no indication that it will finish or stop. Needless to say no virtualenv gets created.
I'm using Python 3.7.4 on Windows 10, virtualenv 16.6.1. This was working at some stage not sure why it stopped working?
Output in cmd:
Running virtualenv with interpreter c:\python37\python.exe
This line gets output continuously......
Try to use one of the two approaches bellow when creating the virtual environment :
python3 -m venv ENV_NAME
or
python3 C:\Python36\Tools\scripts\pyvenv.py ENV_NAME
More information: https://docs.python.org/3/library/venv.html
I am installing Python 3.7.2 for the first time, and I'm using the VS Code python extension.
When I run python -V I get Python 2.7.10 which is not correct!
When I select the usr/local/bin/python3 interpreter in VS Code I get this error when running a script:
bash: /Users/erik/Work/Python/usr/local/bin/python3: No such file or directory
But when I look in usr/local/bin I can see that Python3 is there. I'm not sure why VS Code pastes the work directory in front of usr/local/bin ?
My first thought was that Python3 should be in the PATH variable so I ran the included Update Shell Profile command, which gives this feedback:
This script will update your shell profile when
the 'bin' directory of python is not early enough
of the PATH of your shell.
All right, you're a python lover already
Now, after rebooting VS Code I get a new option for selecting an interpreter:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Is that different from the Python in usr/local/bin ? When I select it, I get this error:
The script isort is installed in '/Users/erik/Library/Python/3.7/bin' which is not on PATH.
I also get this sideways related error:
You are using pip version 18.1. You should consider upgrading via the 'pip install --upgrade pip' command.
But, when following these instructions I get yet another error:
bash: pip: command not found
All in all, this process and the official documentation seem less than user-friendly? Apparently I'm required to dig deep through my mac's system files in the terminal before even writing one line of code.
Am I missing an essential step here?
I suggest that you use virtual environment for your project
first
pip install virtualenv
open cmd in your project directory that you open in VS-Code (it's important that vs-code sees this virtualenv folder that we will create)
mkvirtualenv my_env
and it will activate it automatically. if not run
my_env/bin/Scripts/activate or my_env/Scripts/activate
Then go open vs-code then select my_env for python interpreter
Well, if you want to change your default Python version for the whole system, it might break some applications that are depending on Python 2.
You can alias the commands by adding this line to your ~/.bash_profile:
$ alias python='python3'
The python command will now refer to python3.
If you want to execute the original Python (which refers to python2), you can escape the alias (so \python will launch python2 without touching the alias).
Btw.
$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.7 /usr/local/bin/python
could also be a workaround for you.
I have created a venv using Pycharm. On the PyCharm terminal, everything works fine -- I can activate the venv, and start python normally.
When I exit PyCharm and go to the normal linux terminal, it doesn't work.
First I go to my directory:
$ cd /home/me/PycharmProjects/virtualenvs/my_project/bin/
Then I activate the venv
$ source ./activate
I check my python file
(my_project) $ which python
/home/me/PycharmProjects/virtualenvs/my_project/bin/python
But when I try to invoke python, it fails
$ python
/home/me/PycharmProjects/virtualenvs/my_project/bin/python: No such file or directory
Am I missing something or is this a problem with PyCharm? This is on Linux Mint, recently installed with minimal changes.