I have a problem with Linux in PC. I installed python3.8. I want work with python 3. When I create a virtualenv file it gets created with python2, but I don't want work with python2. How can I enable python2?
You can set a python version while creating a new virtual environment using the -p flag.
virtualenv -p python3.8 my-env-name
Perhaps you can try to locate the location of the python version you want to use the interpreter of (eg. get its path through which python3). Once obtained the path you can create the enviroment specifying the location of the interpreter you want to use for that virtual environment virtualenv -p /usr/bin/python3.6 venv
Related
I am trying to create a virtual enviroment in my ubuntu OS using virtualenv
The command I am using is
virtualenv -p /usr/bin/python3.8.13 py3.8.13_env
The error shown is
FileNotFoundError:[Errno 2]No such file or directory:'/usr/bin/python3.8.13'
I have tried several other python versions but I get the same error
You can see what versions of Python you have by:
ls -l /usr/bin/python*
If you don't provide one then virtualenv will use a default of /usr/bin/python3. On Ubuntu this will be a symlink to a specific version. e.g.
/usr/bin/python3 -> python3.10
So just calling virtualenv like:
virtualenv py3.10_venv
Would create a virtualenv called "py3.10_venv" (a folder) in your current working directory, using Python 3.10 in this example.
If you have other versions (shown by the ls command above) then you can use those specifically as you are trying to do in your question above.
Once I try to run python on my conda environment it blcok like this and nothing change:
(python3) user todoapp
$ python
Knowing that python was intalled in my conda env using conda create -n python3 python=3 and I have runned my env using source activate python3. What suprising me is once I run ipythonthis work normally but python no :(.
I have searched in the net but nothing solved my issue. Is theire any option?
You're naming your virtual environment python3?? That's the same alias for actual Python 3 binary file in many operating systems, don't do that.
Create another virtual environment with another name (or clone that env to another name) and move your project files to it. Then remove your python3 env.
See https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html on the related commands and paths.
As I understood, I have two versions of python 2.7 installed on my machine. One is located in /usr/bin and another one is in /usr/local/bin. When I type python in the shell, it calls one in /usr/local/bin, and it doesn't have access to all the packages installed using apt-get and pip. I have tried to set up an alias, but when I type sudo python it still calls one in /usr/local/bin. I want to always use one in /usr/bin, since I have all the packages there. How do I do that?
From what I understood,
You have two version of python. One is in /usr/local/bin/python
and another is in /usr/bin/python.
In your current configuration default python ->
/usr/local/bin/python
You want to use the one that is in /usr/bin.
Update your ~/.bashrc and append this line at the end
alias python=/usr/bin/python
Then open a new terminal. Or do source ~/.bashrc in the current terminal
Run which python to see the location of the python executable. It will show you /usr/bin/python
Also, if you want to get packages in your current python (i.e. /usr/local/bin/python) you can use pip with that particular python version.
Find pip location using which pip
Assuming pip location is /usr/local/bin/pip
/usr/local/bin/python /usr/local/bin/pip install
you can easily have two python version in your machine.
But first I recommend to install the Anaconda package.
And then you can create an environment with python 3 version
conda create --name test_env python=3 numpy pandas
In order to activate it, you need to write in your terminal
source activate test_env
More info here:
https://conda.io/docs/using/envs.html
I've added the following lines to my bash, but mkproject keeps creating python 2.7 folders into the virtual env, therefore I still need to use -p python3, which I'd like to not have to do.
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
source /usr/local/bin/virtualenvwrapper_lazy.sh
virtualenvwrapper understands the VIRTUALENVWRAPPER_VIRTUALENV environment variable, you need to set it to the virtualenv appropriate to the python version you're using. For example:
export VIRTUALENVWRAPPER_VIRTUALENV=virtualenv3
This is needed because virtualenvwrapper executes virtualenv as a shell script (without adding python2 or python3 in the front of the command).
This way the virtualenv script is executed with the interpreter defined in its shebang (#!).
Most Linux distros provide two packages: virtualenv2 and virtualenv3 each containing one script:
virtualenv2:
#!/usr/bin/python2
import virtualenv
virtualenv.main()
virtualenv3:
#!/usr/bin/python3
import virtualenv
virtualenv.main()
On a Mac you use brew for the python installation. Therefore there is nothing wrong with copying the virtualenv script into two instances: vritualenv2 and virtualenv3 and change the shebang to the correct python version.
(You need to install the virtualenv eggs, through pip, for each python version.)
I defined an alias in the .bashrc file to overwrite the mkproject command to use python3 by default:
alias mkproject='mkproject --python=/usr/bin/python3'
I have a virtualenv created for Python 2.5 and want to "upgrade" it to Python 2.6.
Here is how it was originally set up:
virtualenv --no-site-packages -p python2.5 myenv
I now run virtualenv in the same directory to upgrade:
virtualenv --no-site-packages -p python2.6 myenv
...
Not overwriting existing python script myenv/bin/python (you must use myenv/bin/python2.6)
...
Overwriting myenv/bin/activate with new content
The default python is still 2.5, even though I can also specify 2.6. Is there any way to remove 2.5 entirely and have 'bin/python' point to 2.6 instead?
You can use the Python 2.6 virtualenv to "revirtual" the existing directory. You will have to reinstall all the modules you installed though. I often have a virtual directory for developing a module, and virtualenv the same directory with many versions of Python, and it works just fine. :)
In Python 3.3+ venv supports --upgrade flag
--upgrade Upgrade the environment directory to use this version
of Python, assuming Python has been upgraded in-place.
Usage:
python -m venv --upgrade YOUR_VENV_DIRECTORY
I just upgraded my venv from Python 3.7.x to 3.8 on several projects without any issue.
You should create a new virtualenv using python2.6 and then, after activating the new env, use its python2.6 and its easy_install to install new versions of any site packages you need. Beware that the path name to the virtualenv is hardwired into various files within the environment, so, when you are ready to switch over to it, either change your startup scripts et al to refer to the new virualenv path or be very careful about copying it over to the old directory and modifying the path names inside it.
Install a second Python on CentOS
download python
install to diff local
configure --prefix=/opt/virtualenv/python
make && make install
create virtual env using new python
virtualenv /opt/virtualenv --python=/opt/python276/bin/python
note: if needed it can be done with a different user
chown pyuser -R /opt/virtualenv
su - pyuser
source /opt/virtualenv/bin/activate
python -v
Create virtual env:
virtualenv /opt/virtualenv
su - infograficos
source bin/activate
Install pip with python 2.7 (inside virtualenv)
easy_install pip
If you're using OS X, try this if you want to upgrade Python to a minor-increased version (e.g. 2.7.6 to 2.7.8) while keeping third-party libraries work.
It work for me on 5 different virtual environments with Django installed.
You can simply do this by go to your venv file and change the python path and it's version from pyvenv.cfg like this:enter image description here