Creating virtualenv with python3 - python

I generally use python2.7 for projects.
For one project, I need to use python 3.5+.
I installed python3 on Mac.
Also installed virtualenv using pip3.
Now when I run the command
virtualenv -p python3 test
I get the following error:
Running virtualenv with interpreter /usr/bin/python3
Already using interpreter /Library/Developer/CommandLineTools/usr/bin/python3
Using base prefix '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7'
New python executable in /Users/sourabh/virtualenvs/test/bin/python3
Also creating executable in /Users/sourabh/virtualenvs/test/bin/python
Traceback (most recent call last):
File "/Library/Python/3.7/site-packages/virtualenv.py", line 2632, in <module>
main()
File "/Library/Python/3.7/site-packages/virtualenv.py", line 870, in main
symlink=options.symlink,
File "/Library/Python/3.7/site-packages/virtualenv.py", line 1156, in create_environment
install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages=site_packages, clear=clear, symlink=symlink)
File "/Library/Python/3.7/site-packages/virtualenv.py", line 1621, in install_python
shutil.copy(original_python, py_executable)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/shutil.py", line 245, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/shutil.py", line 103, in copyfile
if _samefile(src, dst):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/shutil.py", line 88, in _samefile
return os.path.samefile(src, dst)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/genericpath.py", line 96, in samefile
s1 = os.stat(f1)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

For making a virtual environment with Python 3
Use this for making env
python3 -m venv env
To activate env
source env/bin/activate

Improve upon answer of #ParthS007 (Wanted to add a comment to the existing answer but couldn't due to not enough reputation):
Corrected the virtual environment name to be the common used one venv so it's easier for beginners like me to use/understand in conjunction with other tutorials.
Added how to deactivate and remove virtual environment too.
For making a virtual environment with Python 3
Use this for making env
python3 -m venv venv
To activate env
source env/bin/activate
To deactivate env
deactivate
To remove env
rm -rf venv

Related

error with installing virtualenv with python 3

ive updated to python3 and downloaded virtualenv using:
sudo /usr/bin/easy_install virtualenv
when i go to start the virtualenv i got the following error message :
virtualenv project1
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 6, in <module>
from pkg_resources import load_entry_point
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 3241, in <module>
#_call_aside
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 3225, in _call_aside
f(*args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 3254, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 585, in _build_master
return cls._build_from_requirements(__requires__)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 598, in _build_from_requirements
dists = ws.resolve(reqs, Environment())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 786, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'zipp>=0.4' distribution was not found and is required by importlib-resources
i looked around and realised the that 'zipp' had not been installed so i installed that also. when i went to run the virtualenv again i got the same error message again as above. and for some reason it keeps referencing python 2.7 even though ive upgraded to python3.
please try this ..
you just follow Exactly the bellow Items :
Open your Command Prompt AS ADMINISTRATOR -> right click on CMD and RUN AS ADMINISTRATOR
go to which Folder to you want to create your VirtualENV : For example : > Cd C:\Users...\Documents\python\src
type it : PIP install Virtualenv
make sure your internet connection is Ok
4.type : python -m venv env
and then you can create your project inside this env folder..
all Comment code should be write in Command Prompt AS Administrator
i ended up using the built in python 3 venv package. it works just as well and it was straight forward and simple. thank you for all the help
start the virtual environment: python3 -m venv project1
activate the virtual environment: source project1/bin/activate
deactivate the virtual environment: deactivate
If you're using Python 3.3 or later, you can use the built-in venv [https://docs.python.org/3/library/venv.html] module. This will reduce the number of dependencies, making installation and troubleshooting a bit easier.
To double-check that you are invoking the correct Python interpreter, run python3 -V or python -V (depending on how it's aliased).
Just try out.
virtualenv venv
It should create a venv directory in same directory if python 3.x is being used.

Virtualenv failing with "does not start with prefix"

I have just downloaded python 3.6.1 from github and built it in a local dir. Now I am trying to make a virtualenv with that version of python but I keep getting:
Running virtualenv with interpreter /home/giorgio/tools/cpython-3.6.1/python
Using base prefix '/usr/local'
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/virtualenv.py", line 2462, in <module>
main()
File "/usr/lib/python2.7/site-packages/virtualenv.py", line 762, in main
symlink=options.symlink,
File "/usr/lib/python2.7/site-packages/virtualenv.py", line 998, in create_environment
install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages=site_packages, clear=clear, symlink=symlink)
File "/usr/lib/python2.7/site-packages/virtualenv.py", line 1219, in install_python
copy_required_modules(home_dir, symlink)
File "/usr/lib/python2.7/site-packages/virtualenv.py", line 1147, in copy_required_modules
dst_filename = change_prefix(filename, dst_prefix)
File "/usr/lib/python2.7/site-packages/virtualenv.py", line 1111, in change_prefix
assert False, "Filename {} does not start with any of these prefixes: {}".format(filename, prefixes)
AssertionError: Filename /home/giorgio/tools/cpython-3.6.1/Lib/os.py does not start with any of these prefixes: ['/usr/local', '/usr/local']
I don't want to install this version of python globally on the system. Is there another way to make virtualenv work with it?
I think Python 3.6 introduced a new way of creating virtual environments that don't depend on routing through usr/local.
python3 -m venv /path/to/new/virtual/environment
Here is documentation.
I ran into this error attempting to use my brew-installed version of Python 3.9.5 to create a new virtual environment on macOS. (mkvirtualenv with virtualenvwrapper)
My command format is typically:
mkvirtualenv -p python3 [env-name]
Not sure what caused this to get messed up, but ultimately I got it working again by running:
python3.9 -m venv ~/.virtualenvs/myenvname
This puts the env in your normal spot, and you can still run workon myenvname
The right way to achieve this is to set the prefix when you configure the build options to be a local directory and then perform make install. So if the directory you want to install things at is $HOME/pythons then in the build directory run:
./configure --prefix="${HOME}/pythons"
make
make install

Installed virtualenv drops some errors

Trying to run virtualenv with my project, but it shows some errors. Couldn't find a right solution, unfortunately. Maybe someone has dealt with the same issue before.
(base) Organic:djangoproject organic$ mkvirtualenv py1
Using base prefix '/anaconda3'
New python executable in /Users/organic/.virtualenvs/py1/bin/python3
Traceback (most recent call last):
File "/anaconda3/bin/virtualenv", line 11, in <module>
load_entry_point('virtualenv==16.1.0.dev0', 'console_scripts', 'virtualenv')()
File "/anaconda3/lib/python3.6/site-packages/virtualenv.py", line 712, in main
symlink=options.symlink)
File "/anaconda3/lib/python3.6/site-packages/virtualenv.py", line 928, in create_environment
site_packages=site_packages, clear=clear, symlink=symlink))
File "/anaconda3/lib/python3.6/site-packages/virtualenv.py", line 1234, in install_python
shutil.copyfile(executable, py_executable)
File "/anaconda3/lib/python3.6/shutil.py", line 121, in copyfile
with open(dst, 'wb') as fdst:
OSError: [Errno 62] Too many levels of symbolic links: '/Users/organic/.virtualenvs/py1/bin/python3'
Thank you guys I have resolved this issue by figuring out how to switch onto a proper virtualenv throughout Anaconda on VS Code. So, for now, I'm using only (base) virtualenv that was already set up by default to conda. And all my last projects seem to be working with no side issues so far.
To whom that doesn't know on VS Code on MAC OS use shift+command+p -> Python: Select Interpreter, then select Anaconda interpreter Python 3.6.6 64-bit ('base': conda) anaconda3/bin/python. Notice that your virtualenv can have a different name instead 'base'.
After, again click shift+command+p choose Python: Create Terminal and VSC will run a terminal down below where your virtualenv will be turned on like in my situation (base) Organic:django_project organic$.

PyCharm's Python console fails when PyPy and iPython co-exist in a virtualenv created project

Some version info:
OS: Mac OS X 10.8.4
Python: 2.7.2 (that came with the Mac OS X)
PyPy: 2.0.2
iPython: 0.13.2
PyCharm: 2.5.1
PyCharm's Python console fails when PyPy and iPython co-exist in a virtualenv created project.
I created three virtual environment projects. I did not activate any of the virtual environments. So, the system python still exists as it is.
Env - 1:
$ virtualenv -p /path/to/system/installed/python /path/to/sample_1/virtualenv/project
$ /path/to/sample_1/virtualenv/project/bin/pip install ipython
Now, when I run the python console in PyCharm it works fine. (Interpreter in PyCharm is set to /path/to/sample_1/virtualenv/project/bin/python)
Env - 2:
$ virtualenv -p /path/to/pypy /path/to/sample_2/virtualenv/project
Now, when I run the python console in PyCharm it works fine. (Interpreter in PyCharm is set to /path/to/sample_2/virtualenv/project/bin/python)
Env - 3:
$ virtualenv -p /path/to/pypy /path/to/sample_3/virtualenv/project
$ /path/to/sample_3/virtualenv/project/bin/pip install ipython
Now, when I run the python console in PyCharm it fails with the following error. (Interpreter in PyCharm is set to /path/to/sample_3/virtualenv/project/bin/python)
/path/to/sample_3/virtualenv/project/bin/pypy -u /Applications/PyCharm.app/helpers/pydev/pydevconsole.py 60355 60356
PyDev console: using IPython 0.13.2
Traceback (most recent call last):
File "app_main.py", line 72, in run_toplevel
File "/Applications/PyCharm.app/helpers/pydev/pydevconsole.py", line 361, in <module>
StartServer(pydev_localhost.get_localhost(), int(port), int(client_port))
File "/Applications/PyCharm.app/helpers/pydev/pydevconsole.py", line 288, in StartServer
interpreter = InterpreterInterface(host, client_port, threading.currentThread())
File "/Applications/PyCharm.app/helpers/pydev/pydev_ipython_console.py", line 37, in __init__
self.interpreter = PyDevFrontEnd()
File "/Applications/PyCharm.app/helpers/pydev/pydev_ipython_console_011.py", line 35, in __init__
shell = TerminalInteractiveShell.instance()
File "/path/to/sample_3/virtualenv/project/site-packages/IPython/config/configurable.py", line 318, in instance
inst = cls(*args, **kwargs)
File "/path/to/sample_3/virtualenv/project/site-packages/IPython/frontend/terminal/interactiveshell.py", line 360, in __init__
user_module=user_module, custom_exceptions=custom_exceptions
File "/path/to/sample_3/virtualenv/project/site-packages/IPython/core/interactiveshell.py", line 454, in __init__
self.init_readline()
File "/path/to/sample_3/virtualenv/project/site-packages/IPython/core/interactiveshell.py", line 1843, in init_readline
self.refill_readline_hist()
File "/path/to/sample_3/virtualenv/project/site-packages/IPython/core/interactiveshell.py", line 1851, in refill_readline_hist
self.readline.clear_history()
File "/tmp/pypy-2.0.2/lib_pypy/pyrepl/readline.py", line 284, in clear_history
del self.get_reader().history[:]
File "/tmp/pypy-2.0.2/lib_pypy/pyrepl/readline.py", line 188, in get_reader
console = UnixConsole(self.f_in, self.f_out, encoding=ENCODING)
File "/tmp/pypy-2.0.2/lib_pypy/pyrepl/unix_console.py", line 98, in __init__
curses.setupterm(term, self.output_fd)
error: setupterm: could not find terminfo database
Process finished with exit code 1
Please be mindful that bin/python in any of the virtual environments points to the installed interpreter. For example, /path/to/sample_3/virtualenv/project/bin/python points to pypy and /path/to/sample_1/virtualenv/project/bin/python points to system installed python.
Seems like this is related to this. But, I am not sure.
Please help.
Looking at the actual Exception message, this may be a potential solution:
https://support.enthought.com/entries/22438744-Linux-IPython-in-terminal-error-setupterm-could-not-find-terminfo-database-
Meanwhile, you should be able to work around the problem by setting
export TERM="xterm-256color" in the shell from which you execute ipython,
or in your~/.bashrc file, which you then source.

Installing virtualenvwrapper on mac osx 10.7.3

Let me start by saying I'm new at programming, mac osx, and bash.
I'm having a tough time getting virtualenvwrapper setup on my Mac osx 10.7.3.
Its most certainly a path settings issue but I'm just having trouble getting it working.
Setup of virtualenvwrapper in .bashrc
export WORKON_HOME=$HOME/.Apps
source /usr/local/bin/virtualenvwrapper.sh
Some terminal output:
which easy_install
/usr/bin/easy_install
python -V
Python 2.7.1
When I run "source .bashrc" in the terminal I get no output.
When I run mkvirtualenv in the terminal I get:
Error [Errno 2] No such file or directory while executing command install_name_tool -change /System/Library/Fram.../Versions/2.7/Python #executable_path/../.Python 1/bin/python
Could not call install_name_tool -- you must have Apple's development tools installed
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 9, in <module>
load_entry_point('virtualenv==1.7.1.2', 'console_scripts', 'virtualenv')()
File "/Library/Python/2.7/site-packages/virtualenv-1.7.1.2-py2.7.egg/virtualenv.py", line 928, in main
never_download=options.never_download)
File "/Library/Python/2.7/site-packages/virtualenv-1.7.1.2-py2.7.egg/virtualenv.py", line 1029, in create_environment
site_packages=site_packages, clear=clear))
File "/Library/Python/2.7/site-packages/virtualenv-1.7.1.2-py2.7.egg/virtualenv.py", line 1347, in install_python
py_executable])
File "/Library/Python/2.7/site-packages/virtualenv-1.7.1.2-py2.7.egg/virtualenv.py", line 966, in call_subprocess
cwd=cwd, env=env)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child
OSError: [Errno 2] No such file or directory
You probably just need to install XCode. It should be a free (if large) download/install from the App Store.
For future reference, I highly recommend using a package manager and pip (instead of easy_install) for python development on OS X. My preferred package manager is Homebrew, but MacPorts and Fink are also good. Among other things, pip lets you uninstall stuff, which easy_install cannot simply do.

Categories